1 前言
由于有两个github账号,要在同一台电脑上同步代码,需要给每一个账号添加一个SSH public key,此时推送时git push origin,不知道是哪个账号的远程仓库名称,所以需要配置一下~/.ssh下面的config信息,否则会自动使用.ssh/id_rsa.pub所对应的账户进行登陆并操作。
若没配置,提示的错误信息如下:
$ git push ERROR: Permission to B/test2.git denied to A. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
2 步骤
解决方案(假设你已经拥有私有账号且已经OK,现在想使用另一个工作用账号):
2.1 为工作账号生成SSH Key(先切换到 ~/.ssh下),具体参照Gitlab的SSH配置
ssh-keygen -t rsa -C your.email@example.com -b 4096
存储key的时候,不要覆盖现有的id_rsa,使用一个新的名字,比如id_rsa_work
2.2 配置.ssh/config
$ vi .ssh/config # 加上以下内容 #default github Host github.com HostName github.com IdentityFile ~/.ssh/id_rsa Host github_work HostName github.com IdentityFile ~/.ssh/id_rsa_work
2.3 git push remote add origin2
#push到github上去 $ git remote add origin2 git@github_work:B/test2.git
2.4 id_rsa_work.pub加到你的work账号上
$ git remote -v origin git@github.com:B/test2.git (fetch) origin git@github.com:B/test2.git (push) origin2 git@github_work:B/test2.git (fetch) origin2 git@github_work:B/test2.git (push)
这样就完成配置了。
2.5 git push origin2
git push origin2 master
你就可以通过使用github.com别名github_work来明确说你要是使用id_rsa_work的SSH key来连接github,即使用工作账号进行操作。
3 参考: