Git多用户配置

来源:互联网 发布:淘宝订单在线生成器 编辑:程序博客网 时间:2024/05/12 08:40

在Git使用中经常会碰到多用户问题,例如:你在公司里有一个git账户,在github上有一个账户,并且你想在一台电脑上同时对这两个git账户进行操作,此时就需要进行git多用户配置。 
    首先配置不同的SSH KEY,使用ssh-keygen命令产生两个不同的SSH KEY,进入.ssh目录:

#切换到.ssh目录cd ~/.ssh  #使用自己的企业邮箱产生SSH KEYssh-keygen -t rsa -C "mywork@email.com"  #企业的可以使用id_rsa,也可以自己起名,例如:id_rsa_workEnter file in which to save the key (/Users/ltc/.ssh/id_rsa): id_rsa #将ssh key添加到SSH agent中ssh-add ~/.ssh/id_rsa 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

同理,配置自己的github账户,再有其他账户类似:

#切换到.ssh目录cd ~/.ssh  #使用自己github的注册邮箱产生SSH KEYssh-keygen -t rsa -C "mygithub@email.com"  #github的SSH KEYEnter file in which to save the key (/Users/ltc/.ssh/id_rsa): id_rsa_github#将ssh key添加到SSH agent中 ssh-add ~/.ssh/id_rsa_github
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在生成ssh key之后,需要分别在github的profile中和公司git的profile中编辑SSH KEY,以github为例: 
image 
在图中添加Title,可以随便写: 
将.ssh目录下对应的id_rsa_github.pub中的内容拷到Key中,点击Add SSH key按钮即可。公司的git类似。 
然后在.ssh目录下配置config文件:

 #切换到.ssh目录cd ~/.ssh#创建并编辑config文件vim config # 粘贴到config文件中#公司的git地址Host git.***.com     User git   Hostname git.***.com  #公司的git地址   IdentityFile ~/.ssh/id_rsa  #访问公司git的SSH KEY   Port   ***  #公司的git端口Host github.com   User git   Hostname github.com #github的地址   IdentityFile ~/.ssh/id_rsa_github  #访问github的SSH KEY
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

测试配置是否成功

#github的地址ssh -T git@github.com #出现如下内容,表示成功链接github,***为你的github账户的用户名Hi ***! You've successfully authenticated, but GitHub does not provide shell access.#公司的git地址ssh -T git@git.***.com #出现如下内容,表示成功链接github,***为公司git账户的用户名Hi ***! You've successfully authenticated, but GitHub does not provide shell access.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

此时,就可以分别访问公司的git和github了。

文章作者:Tyan 
博客:noahsnail.com 
下载:本文PDF

原创粉丝点击