问题描述
$ git push
remote: Permission to xxx/testrepo.git denied to yyy
$ git config user.name
xxx
$ git config --global user.name
xxx
明明用户就是 xxx,但是这里提交确实尝试用 yyy 用户进行提交,然后就被拒绝了。
此外,去 github 上检查ssh key,也是已经加好了的(重新加一遍也不行)
$ cat ~/.ssh/id_rsa.pub
那么这里就引出来几个问题:
- 如何使用不同账号往不同的代码平台,如 github / gitlab / bitbucket / gitee 上推送代码?
- 如何使用不同账号往同一个代码平台,如 github 的多个 repo 上推送代码?
根本原因
原因是 ~/.ssh/
目录下生成了多个ssh key:
$ ls ~/.ssh/
id_ed id_ed.pub known_hosts
config id_rsa id_rsa.pub
git push
在提交时,错误得使用了其中一个公钥 id_ed.pub,也就是对应了用户 yyy
Solution
方法一
删除其中一个 ssh key
方法二
修改config文件:
Host github1
HostName github.com
User git
IdentityFile ~/.ssh/user1_rsa
IdentitiesOnly yes
Host danielsunzhongyuan.github.com
HostName github.com
User git
IdentityFile ~/.ssh/user2_rsa
IdentitiesOnly yes
说明:
- 这里的用户指的就是 git,也就是你的 repo 的 remote url 里 @ 符号前面的内容(能不能修改还没试过)
- Host 指的是 remote url 里 @ 符号后面的内容
$ git remote -v
origin git@danielsunzhongyuan.github.com:danielsunzhongyuan/puruan-training.git (fetch)
origin git@danielsunzhongyuan.github.com:danielsunzhongyuan/puruan-training.git (push)
- 这样也就要求咱们使用默认 git clone 之后,需要修改 remote url,把它设置成 username.github.com 的形式,从而让它去使用 user2_rsa
Reference
- https://www.cnblogs.com/v5captain/p/6590991.html
- https://hanpanpan200.github.io/2019/10/14/setup-multiple-git-accounts-by-ssh-config/