Github 使用PAT(Personal Access Token)后的命令行登录
在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)后的命令行登录的更多相关文章
- rpm方式安装MySQL后在命令行登录报错:ERROR 1045 (28000): Unknown error 1045
1.通过https://mirrors.tuna.tsinghua.edu.cn镜像源安装了MySQL5.7.22 rpm -ivh --force --nodeps https://mirrors. ...
- github & personal access token
github & personal access token OAuth https://github.com/xgqfrms/webtrc-in-action/issues/1#issuec ...
- [2018-05-27]配置VSTS认证方式使用Personal Access Token
本文介绍下如何配置VSTS(visual studio team service,其实就是微软SaaS版的TFS)通过Personal Access Token访问其下的Git代码库. 问题 使用gi ...
- 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 ...
- 安装了nodejs后在命令行运行npm报错
安装了nodejs后在命令行运行npm报错:Error: Cannot find module 'internal/util/types' 解决方法:删除目录“C:\Users\mengxiaobo\ ...
- 安装zsh后出现命令行无法识别已安装的node
安装zsh之后,在命令行输入 node -v,会出现 command not found: node. 出现原因是:使用bash输入终端指令识别得是~/.bash_profile,而安装zsh则无法识 ...
- Linux普通用户登录后,命令行提示:-bash-4.1$ ,原因分析及解决
原文 有时候在使用用户登陆Linux系统时会发现,命令行提示符成了:-bash-4.1$,不显示用户名,路径信息. 原因:用户家目录里面与环境变量有关的文件被删除所导致的 也就是这俩文件:.bash_ ...
- 配置github的SSH key及GitHub项目上传方式一——使用终端命令行
GitHub是一个开源的大仓库,我们经常从github上下载项目进行学习和研究,下面是一个完整的步骤——往GitHub上传一个新项目. 一.注册GitHub账号 1.注册GitHub账号,地址:htt ...
- win10系统 安装好composer后 cmd 命令行下输入composer提示不是内部或外部的命令,也不是可执行的程序或批处理文件
在 windows CMD 下运行composer 出现错误提示: 不是内部或外部命令,也不是可运行的程序或批处理文件,这是因为没有配置 PATH 环境变量. 以 win10 为例,找到此电脑右击选择 ...
- macOS 升级后重装命令行工具的问题
问题背景 最近升级个人macbook 从 10.13 到 10.14 在终端输入 git 不能用了,发现是重装操作系统后原来的 Command Line Tools 被自动卸载了, 采用 xcode- ...
随机推荐
- Angular系列教程之DOM操作
.markdown-body { line-height: 1.75; font-weight: 400; font-size: 16px; overflow-x: hidden; color: rg ...
- Keep English Level-04
firm -- 坚定的,坚固的;公司 share -- n 股份,份额 executive -- 执行官 There is no chance,no density,no fate,that can ...
- 【MicroPython】生成micropython版本头文件 - py\makeversionhdr.py
用法 $ python makeversionhdr.py mpversion.h 实现 带git仓 get_version_info_from_git 使用git指令: git describe ...
- 【ARM】重新定义低级库函数,以便能够直接使用 C 库中的高级库函数
Redefining low-level library functions to enable direct use of high-level library functions in the C ...
- JMS微服务开发示例(五)生成短token,实现用户无状态登录
用户token,也可以利用第三方框架生成,JMS也包含了自己的token服务器. 部署TokenServer 到这里下载 tokenserver.zip,然后部署运行TokenServer. 微服务中 ...
- SpringMVC05——SSM整合
整合SSM 需求:熟练掌握MySQL数据库,Spring,JavaWeb及MyBatis知识,简单的前端知识 CREATE DATABASE `ssmbuild`; USE `ssmbuild`; D ...
- [转帖]Linux系统硬链接和软链接具体实例讲解(超详细)
简介 在 Linux 中,元数据中的 inode 号(inode 是文件元数据的一部分但其并不包含文件名,inode 号即索引节点号)才是文件的唯一标识而非文件名.文件名仅是为了方便人们的记忆和使用, ...
- [转帖]「Linux性能调优」磁盘I/O队列调度策略
https://zhuanlan.zhihu.com/p/450329513 傻瓜化说明 简单地说,对于磁盘I/O,Linux提供了cfq, deadline和noop三种调度策略 cfq: 这个名字 ...
- 某环境私有云 rpm 包安装失败总结
1. 最近公司同事说一个项目搭建环境时 rpm 安装mysql数据库会报错 错误图片主要如下: 第一个错误提示是 提示 /etc/host.conf line 2: bad command `nosp ...
- Harbor 简要安装说明
Harbor 简要安装说明 下载最新的离线安装文件 链接:https://pan.baidu.com/s/1ZEjgnI3YmhsdVOm7h7SWcQ 提取码:GSNB 复制这段内容后打开百度网盘手 ...