在Github上启用PAT

命令行下使用git push不能再直接使用用户名密码, 在输入密码的地方需要使用PAT来代替. 具体的创建步骤为

https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token

如果只是用于提交代码, 勾选repo部分即可, 对于大部分场景, 可以加上这几个权限gist, read:org, repo, workflow

Windows 用户

控制面板 => Credential Manager => Windows Credentials

  • 找到 git:https://github.com => 编辑 => 将 Password 替换成在 GitHub 上生成的 Personal Access Token => OK
  • 如果没有 git:https://github.com , 点击 Add a generic credential => Internet 地址填写 git:https://github.com , 输入用户名和 Personal Access Token => OK

之后在git中做如下设置

git config --local credential.helper wincred

Linux 用户

首先给git配置上 username 和 email

$ git config --global user.name "your_github_username"
$ git config --global user.email "your_github_email"
$ git config -l

然后用git连接 GitHub. 例如:

$ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
> Cloning into `Spoon-Knife`...
$ Username for 'https://github.com' : username
$ Password for 'https://github.com' : 这里输入 personal access token

然后用下面的命令, 将这个token加入缓存

$ git config --global credential.helper cache

你可以随时使用下面的命令删除缓存

$ git config --global --unset credential.helper
$ git config --system --unset credential.helper

可以用-v参数验证一下

$ git pull -v

Linux/Debian可以用下面的命令Clone:

git clone https://<tokenhere>@github.com/<user>/<repo>.git

开启双因子认证后命令行登入Github

在GitHub开启双因子后, 以前的登录方式不行了

mote-pad$ git pull
Username for 'https://github.com': someone
Password for 'https://someone@github.com':
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/someone/mote-pad.git/'

搜了下, 命令行登录需要通过GitHub CLi 或者 Git Credential Manager, 当前环境还没安装

mote-pad$ git credential-manager -h
git: 'credential-manager' is not a git command. See 'git --help'.

安装步骤

https://github.com/git-ecosystem/git-credential-manager/releases 下载 .deb 包(我用的Ubuntu环境)然后安装

# download
wget https://github.com/git-ecosystem/git-credential-manager/releases/download/v2.4.1/gcm-linux_amd64.2.4.1.deb
# install
sudo dpkg -i gcm-linux_amd64.2.4.1.deb
# configurate
git-credential-manager configure
# check
git credential-manager -h

设置存储方式

plaintext 明文方式, 默认存到 /home/[username]/.gcm/store/git/https/github.com/someone.credential, 这种方式不够安全

# cd到项目目录下, 用--local设为局部设置
git config --local credential.credentialStore plaintext

这时候就可以用github账号登录了

# 查看保存的github账号, 当前为空
git credential-manager github list
# 登录, 不需要用户名, 直接用 PAT Token
git credential-manager github login
Select an authentication method for 'https://github.com/':
1. Device code (default)
2. Personal access token
option (enter for default): 2
Enter GitHub personal access token for 'https://github.com/'...
Token:
# 检查
git credential-manager github list
someone

换成gpg存储

参考 https://github.com/git-ecosystem/git-credential-manager/blob/main/docs/credstores.md

初始化 gpg key, 会让输入name和email, name 就是 user id, 可以用于后面的pass init

$ gpg --gen-key
...
Real name: someone
Email address: someone@outlook.com
You selected this USER-ID:
"someone <someone@outlook.com>" Change (N)ame, (E)mail, or (O)kay/(Q)uit? o
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
gpg: key 2EDDD89402CB8758 marked as ultimately trusted
gpg: directory '/home/someone/.gnupg/openpgp-revocs.d' created
gpg: revocation certificate stored as '/home/someone/.gnupg/openpgp-revocs.d/771DB7B3382BA257BE64DB6F2EDDD89402CB8758.rev'
public and secret key created and signed. pub rsa3072 2023-11-09 [SC] [expires: 2025-11-08]
771DB7B3382BA257BE64DB6F2EDDD89402CB8758
uid someone <someone@outlook.com>
sub rsa3072 2023-11-09 [E] [expires: 2025-11-08]

中间会创建一个口令, 用于日常验证

用这个user id创建pass

pass init someone
mkdir: created directory '/home/someone/.password-store/'
Password store initialized for someone

git config 换成 gpg 存储

git config --local credential.credentialStore gpg

之后第一次使用, 需要用APT登录, 后续登录, 需要使用之前创建的口令, 和sudo一样有缓存不需要每次验证

Github 使用PAT(Personal Access Token)后的命令行登录的更多相关文章

  1. rpm方式安装MySQL后在命令行登录报错:ERROR 1045 (28000): Unknown error 1045

    1.通过https://mirrors.tuna.tsinghua.edu.cn镜像源安装了MySQL5.7.22 rpm -ivh --force --nodeps https://mirrors. ...

  2. github & personal access token

    github & personal access token OAuth https://github.com/xgqfrms/webtrc-in-action/issues/1#issuec ...

  3. [2018-05-27]配置VSTS认证方式使用Personal Access Token

    本文介绍下如何配置VSTS(visual studio team service,其实就是微软SaaS版的TFS)通过Personal Access Token访问其下的Git代码库. 问题 使用gi ...

  4. gitlab克隆报错:remote: HTTP Basic: Access denied;remote: You must use a personal access token with ‘api’ scope for Git over HTTP.

    错误: remote: HTTP Basic: Access denied remote: You must use a personal access token with ‘api’ scope ...

  5. 安装了nodejs后在命令行运行npm报错

    安装了nodejs后在命令行运行npm报错:Error: Cannot find module 'internal/util/types' 解决方法:删除目录“C:\Users\mengxiaobo\ ...

  6. 安装zsh后出现命令行无法识别已安装的node

    安装zsh之后,在命令行输入 node -v,会出现 command not found: node. 出现原因是:使用bash输入终端指令识别得是~/.bash_profile,而安装zsh则无法识 ...

  7. Linux普通用户登录后,命令行提示:-bash-4.1$ ,原因分析及解决

    原文 有时候在使用用户登陆Linux系统时会发现,命令行提示符成了:-bash-4.1$,不显示用户名,路径信息. 原因:用户家目录里面与环境变量有关的文件被删除所导致的 也就是这俩文件:.bash_ ...

  8. 配置github的SSH key及GitHub项目上传方式一——使用终端命令行

    GitHub是一个开源的大仓库,我们经常从github上下载项目进行学习和研究,下面是一个完整的步骤——往GitHub上传一个新项目. 一.注册GitHub账号 1.注册GitHub账号,地址:htt ...

  9. win10系统 安装好composer后 cmd 命令行下输入composer提示不是内部或外部的命令,也不是可执行的程序或批处理文件

    在 windows CMD 下运行composer 出现错误提示: 不是内部或外部命令,也不是可运行的程序或批处理文件,这是因为没有配置 PATH 环境变量. 以 win10 为例,找到此电脑右击选择 ...

  10. macOS 升级后重装命令行工具的问题

    问题背景 最近升级个人macbook 从 10.13 到 10.14 在终端输入 git 不能用了,发现是重装操作系统后原来的 Command Line Tools 被自动卸载了, 采用 xcode- ...

随机推荐

  1. 【rt-thread】board.h 文件中的内存大小配置如何决定

    确认RAM种类及性质 使用STM32F429IGT6芯片,根据数据手册RAM大小是256KB,常规RAM是 256 - 64 在board.h中配置内存大小 在board.h中配置256则会出错在接口 ...

  2. 【TouchGFX】Widgets and Containers

    Widgets and Containers 是 TouchGFX 应用最基础的东西,他们贯穿于 UI 的整个开发,属于 TouchGFX 的预制组件,同时也支持自定义实现 Widgets Widge ...

  3. SpringMVC05——SSM整合

    整合SSM 需求:熟练掌握MySQL数据库,Spring,JavaWeb及MyBatis知识,简单的前端知识 CREATE DATABASE `ssmbuild`; USE `ssmbuild`; D ...

  4. 通过宿主机查看K8S或者是容器内的Java程序的简单方法

    通过宿主机查看K8S或者是容器内的Java程序的简单方法 背景 最近一个项目的环境出现了 cannot create native process 的错误提示 出现这个错误提示时, docker ex ...

  5. [转帖]How to Resolve ORA-3136 Inbound Connection Timed Out

    https://logic.edchen.org/how-to-resolve-ora-3136-inbound-connection-timed-out/#:~:text=ORA-03136%3A% ...

  6. [转帖]一次ORA-3136的处理

    https://oracleblog.org/working-case/deal-with-ora3136/ 最近收到一个告警,用户说数据库无法连接,但是从监控上看,oracle的后台进程已经侦听进程 ...

  7. [转帖]oracle中Rman增量备份下各级别level的区别

    RMAN备份分为全备和增量备份两部分 增量备份:分为0 1 2级 ORACLE官方解释:  A level 1 incremental backup can be either of the foll ...

  8. [转帖]9.2 TiFlash 架构与原理

    9.2 TiFlash 架构与原理 相比于行存,TiFlash 根据强 Schema 按列式存储结构化数据,借助 ClickHouse 的向量化计算引擎,带来读取和计算双重性能优势.相较于普通列存,T ...

  9. [转帖]深入理解mysql-第十二章 mysql查询优化-Explain 详解(下)

    我们前面两章详解了Explain的各个属性,我们看到的都是mysql已经生成的执行计划,那这个执行计划的是如何生成的?我们能看到一些过程指标数据吗?实际mysql贴心为我们提供了执行计划的各项成本评估 ...

  10. lldb3.9.0 安装攻略

    Study From https://github.com/dotnet/diagnostics/blob/master/documentation/lldb/centos7/build-instal ...