【git】本地终端git push到github repo

好几次将自己的github代码git clone到本地,在自己的电脑上编写修改代码之后再git push的时候,都会遇到access denied的问题。好不容易解决了,下次再遇到的时候又忘了怎么做,所以专门写一篇笔记从头到尾来记录一下解决方法。

操作系统:MacOS

git clone

clone http
git clone https://github.com/username/repo_name.git

clone SSH
git clone git@github.com:username/repo_name.git

本文一开始用的是http,但是解决未果,之后改为了SSH。

git push

现在你已经将你的代码拷贝到本地,你做了一些修改,打算远程push到你的github上,遇到了Authentication failed的问题

$ git push
Username for 'https://github.com': your_username
Password for 'https://your_username@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/...

github现在从本地远程git push已经取消账号密码认证的方法了,所以这里的password并不是你的github账号密码。详见cloning-with-https-urls

git clone http的认证

如果一开始你是使用http URL克隆repo,那么可以参考以下方法。
在这里插入图片描述

创建个人访问令牌

官网资料告诉我们要使用git clone, git fetch, git pull, git push等操作,需要通过创建并验证个人访问令牌,详见如何创建个人访问令牌(personal access token)

创建完personal access token后,记得保存你的token,一旦网页刷新之后就看不到token了。

再次尝试git push,password输入你刚创建的access token。如果能够完成push,代表认证成功。如果失败,可能会遇到报错The requested URL returned error: 403

接下来介绍第二种认证方式:SSH

git clone SSH的认证

  1. 使用SSH URL

如果一开始用的clone的是http URL,那么就需要将URL改成SSH URL:

$ git remote set-url origin git@github.com:username/repo.git
  1. 确认自己有无密钥
 $ ls -al ~/.ssh
 # 列出.ssh 目录里的文件

github支持的公钥文件名有:

  • id_rsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub

如果你已有生成好的ssh密钥,就可以跳到步骤5:上传密钥。

  1. 生成新的密钥
# 打开终端
# 下面的命令行以提供的电子邮件地址为标签创建新 SSH 密钥(电子邮件是注册github账号时所用的)
$ ssh-keygen -t ed25519 -C "your_email@example.com"

# 键入存储密钥的文档(选择默认文档直接回车)
> Enter a file in which to save the key (/Users/YOU/.ssh/id_ALGORITHM): [Press enter]

# 这里输入的密码就是之后git操作要求认证时输入的密码
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
  1. 将密钥加入ssh-agent

生成密钥之后还需要将密钥加入密钥代理(ssh-agent),根据环境的不同,输入的命令行有所不同。我这里使用的是MacOs

参考官网:如果你使用的是 macOS Sierra 10.12.2 或更高版本,则需要修改 ~/.ssh/config 文件以自动将密钥加载到 ssh-agent 中并在密钥链中存储密码。

$ eval "$(ssh-agent -s)"
Agent pid ...

$ open ~/.ssh/config
The file /Users/macbookp/.ssh/config does not exist.

# 如果文件不存在,则创建文件
$ touch ~/.ssh/config

创建~/.ssh/config文件之后,打开 ~/.ssh/config 文件并添加以下内容。

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

然后将 SSH 私钥添加到 ssh-agent 并将密码存储在密钥链中。

$ ssh-add --apple-use-keychain ~/.ssh/id_ed25519

# 这里输入的password就是前面创建密钥时的密码
Enter passphrase for /Users/macbookp/.ssh/id_ed25519: 
Identity added: ...
  1. 上传密钥

将生成或已有的密钥上传至github,首先拷贝你的公钥

# 此命令行拷贝公钥至剪贴板
$ pbcopy < ~/.ssh/id_ed25519.pub

然后点击头像->设置->SSH 和 GPG 密钥->New SSH key,输入Title,将公钥粘贴在Key下面的输入框,Add SSH key就完成了。

在这里插入图片描述
在这里插入图片描述
详情参见官网新增 SSH 密钥到 GitHub 帐户

push 成功

一番操作之后,我们再试一下git push

$ git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 570 bytes | 570.00 KiB/s, done.
...

大功告成,困扰我好几次的问题终于记下来了。如果你觉得有用记得点赞👍~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值