Git多账号登陆

    最近工作上遇到了使用git+repo的情况,需要用公司的邮箱和账号名重新申请ssh公私密钥,而我本身在github上也有一些开源项目,这里就是记录一下我是如何实现git多账号登陆的。
 

取消git全局设置

    很多同学照着网上的教程,都会对git进行全局设置,例如:
[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. git config --global user.name "your_name"  
  2. git config --global user.email  "your_email"  
    如果你多参与的项目都允许你用同一个用户名和邮箱,这样设置当然没问题,但是,一旦你进入公司,估计是没有自主选择权利的,公司都会配置相应的域账号和邮箱,因此我们首先需要取消git的全局设置。
[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. git config --global --unset user.name  
  2. git config --global --unset user.email  
    针对每个项目,单独设置用户名和邮箱,设置方法如下:
[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. mkdir ~/test // git检出目录  
  2. cd ~/test  
  3. git init  
  4. git config user.name "your_name"  
  5. git config user.email "your_email"  
    说白了,也就是进入到你的git项目相对根目录下,然后执行git config来设置user.name和user.email。
 

SSH配置

    我看了很多中文博客,发现讲的都不太清楚,还是在stackoverflow上,找了一个问题解决我的疑惑: http://stackoverflow.com/questions/14689788/multiple-github-accounts-what-values-for-host-in-ssh-config 。解决方法总结如下:
    1. 我现在有两个git项目,使用的用户名分别是A和B,用的邮箱分别是C和D。
    2. 在~/.ssh目录下,使用 ssh-keygen -C "your_email" -t rsa 生成公私秘钥,命名分别为 id_rsa_first, id_rsa_second,公钥的内容需要分别上传到相应git项目的服务器上。
    3. 在~/.ssh目录下创建config文件,进行相应配置:
[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. #第一个git项目账号  
  2. Host first  
  3. HostName test.com #这里需要用真实的项目检出hostname,为了项目安全,我这里随意写的  
  4. User A  
  5. IdentityFile ~/.ssh/id_rsa_first  
  6.   
  7. #第二个git项目账号  
  8. Host second  
  9. HostName test2.com  
  10. User B  
  11. IdentityFile ~/.ssh/id_rsa_second  
    4. 新建git项目检出目录,我发现很多同学出问题,在于git项目没有初始化
[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. mkdir project && cd project  
  2. git init  
  3. git config user.name "A"  
  4. git config user.email "C"  
    相应的第二个项目也参照上面的指令进行初始化设置。
    5. 检出服务端项目代码,这里需要注意,使用.ssh目录下的host代替真实的hostname,这样才能让git识别出来
[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. git remote add first $giturl  
    $giturl代表的是A项目的git clone的url地址。
    6. git pull 拉取A git项目代码。
[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. git pull first master  
    可以指定为master分支或者其他分支,但是first标识不能变,不能单纯的认为是original。
    7. push的时候,push到对应的Host即可。
[plain]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. git push first $branch  

 

 

 

 

  • 先假设我有两个账号,一个是github上的,一个是公司gitlab上面的。先为不同的账号生成不同的ssh-key

    ssh-keygen -t rsa -f ~/.ssh/id_rsa_work -C xxx@gmail.com

    然后根据提示连续回车即可在~/.ssh目录下得到id_rsa_work和id_rsa_work.pub两个文件,id_rsa_work.pub文件里存放的就是我们要使用的key

    ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C xxx@gmail.com

    然后根据提示连续回车即可在~/.ssh目录下得到id_rsa_github和id_rsa_github.pub两个文件,id_rsa_gthub.pub文件里存放的就是我们要使用的key

  • 把id_rsa_xxx.pub中的key添加到github或gitlab上,这一步在github或gitlab上都有帮助,不再赘述

  • 编辑 ~/.ssh/config,设定不同的git 服务器对应不同的key

1
2
3
4
5
6 7 8 9 10 11 12 
# Default github user(first@mail.com),注意User项直接填git,不用填在github的用户名
Host github.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_github  # second user(second@mail.com) # 建一个gitlab别名,新建的帐号使用这个别名做克隆和更新 Host 172.16.11.11  HostName 172.16.11.11  User work  IdentityFile ~/.ssh/id_rsa_work

编辑完成后可以使用命令 ssh -vT git@github.com 看看是不是采用了正确的id_rsa_github.pub文件

这样每次push的时候系统就会根据不同的仓库地址使用不同的账号提交了

  • 从上面一步可以看到,ssh区分账号,其实靠的是HostName这个字段,因此如果在github上有多个账号,很容易的可以把不同的账号映射到不同的HostName上就可以了。比如我有A和B两个账号, 先按照步骤一生成不同的key文件,再修改~/.ssh/config 内容应该是这样的。
1
2
3
4
5
6 7 8 9 10 11 12 
# Default github user(A@mail.com),注意User项直接填git,不用填在github的用户名
Host A.github.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_github_A  # second user(B@mail.com) # 建一个gitlab别名,新建的帐号使用这个别名做克隆和更新 Host A.github.com  HostName github.com  User git  IdentityFile ~/.ssh/id_rsa_github_B

同时你的github的repo ssh url就要做相应的修改了,比如根据上面的配置,原连接地址是:

git@github.com:testA/gopkg.git

那么根据上面的配置,就要把github.com换成A.github.com, 那么ssh解析的时候就会自动把testA.github.com 转换为 github.com,修改后就是

git@A.github.com:testA/gopkg.git

直接更改 repo/.git/config 里面的url即可

这样每次push的时候系统就会根据不同的仓库地址使用不同的账号提交了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值