在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. 电子科技大学--ARM实验Keil uvison5 安装以及S3C2440A芯片包下载

    1.简介 本教程主要服务于电子科技大学软工嵌入式新生,节省一些查找资料的时间 2.过程 请按如下顺序安装: 1,安装mdk_514.exe (主体文件) 2,安装Keil.STM32F1xx_DFP. ...

  2. 问题--缺少 cryptography 包

    1.问题 raise RuntimeError( RuntimeError: 'cryptography' package is required for sha256_password or cac ...

  3. Laravel - 配置 数据库

  4. [转帖]clickhouse 超底层原理& 高可用集群 实操(史上最全)

    https://www.cnblogs.com/crazymakercircle/p/16718469.html 文章很长,而且持续更新,建议收藏起来,慢慢读!疯狂创客圈总目录 博客园版 为您奉上珍贵 ...

  5. 有趣的Shell脚本学习

    有趣的Shell脚本学习 倒计时脚本 #!/bin/bash echo 20秒倒计时开始: tput sc # 循环40秒 for count in `seq 0 20` do tput rc tpu ...

  6. [转帖]s3fs - 使用S3FS存储桶目录允许其他用户使用权限

    https://www.coder.work/article/6661505   我在使用S3FS时遇到问题.我正在使用 ubuntu@ip-x-x-x-x:~$ /usr/bin/s3fs --ve ...

  7. [转帖]大模型训练,英伟达Turing、Ampere和Hopper算力分析

    https://www.eet-china.com/mp/a219195.html 大 GPU 优势在于通过并行计算实现大量重复性计算.GPGPU即通用GPU,能够帮助 CPU 进行非图形相关程序的运 ...

  8. 【转帖】【奇技淫巧】Linux | 统计网络-netstat

    theme: condensed-night-purple 小知识,大挑战!本文正在参与"程序员必备小知识"创作活动. 在构建生产服务器时,我们有的时候需要统计网络接口状况,比如T ...

  9. [转帖]BF16 与 FP16 在模型上哪个精度更高呢

    https://zhuanlan.zhihu.com/p/449345588 BF16 是对FP32单精度浮点数截断数据,即用8bit 表示指数,7bit 表示小数. FP16半精度浮点数,用5bit ...

  10. [转帖]SpringBoot配置SSL 坑点总结【密码验证失败、连接不安全】

    文章目录 前言 1.证书绑定问题 2.证书和密码不匹配 3.yaml配置文件问题 3.1 解密类型和证书类型是相关的 3.2 配置文件参数混淆 后记 前言 在SpringBoot服务中配置ssl,无非 ...