github-multi-account-confusion

粗暴解决多个github账号冲突

简单粗暴的方法:
设定某一个账号名和邮箱为全局.

1
2
git config --global user.name 'xxx'
git config --global user.email 'xxx'

优雅解决github多账号的问题解决方案

各账号SSH的配置

  • 配置相应账号的ssh key

配置参考:

官网ssh添加指南

博客-Github多账号SSH key添加

  • 取消全局的邮箱和名称配置
1
2
git config --global --unset user.name
git config --global --unset user.email
  • 各项目配置相应的账号邮箱和名称
1
2
git config user.name "xxx"
git config user.email "xxx@xxx.com"

各账号的使用

每个账号根据ssh中配置的Host名来指定。

例如:

账号1 host: githubLive

账号2 host: githubWork

账号1下clone的操作如下:

1
git clone githubLive:user/repo.git

账号2下clone的操作如下:

1
git clone githubWork:user/repo.git

其中, user 为github用户名, repo 为github仓库名。

此外,已经下载的repo可以通过修改相应项目的.git/config文件来进行操作,修改格式如下。

1
2
[remote "origin"]
url = githubLive:user/repo.git

后续的push和pull都轻松无压力了。

错误解决

  1. 提示无权限访问repo

    ERROR: Permission to user/repo.git denied to other user

解决方法:

确认user账号的ssh私钥是否添加到ssh agent中

1
ssh-add ~/.ssh/id_rsa_work

根据第一步中生成的私钥。

作者

潘绳杰

发布于

2016-09-03

更新于

2025-01-19

许可协议

评论