[关闭]
@muyanfeixiang 2014-11-13T09:15:15.000000Z 字数 1303 阅读 1571

git多账户配置

git 多账户


一直以来git都是自己业余玩玩,虽然也在github上有个账户,也只是小打小闹,弄了若干个测试仓库。最近公司要从TFS迁移到Gerrit,也算是正式用git了。不过遇到的一个问题就是账户问题,一个个人账户,一个公司账户,个人账户是到github仓库的,公司账户是到公司自己搭建的仓库的。所就配置一下了。

当然首先就是生成SSH Key了,cd到.ssh目录下(该目录一般在当前用户的文件夹下[1],如果没有可以自己新建一个)。

  1. $ cd .ssh
  2. $ ssh-keygen -t rsa -C "youremail@email.com"
  3. $Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): github //可以重命名为github,方便区分
  4. $ ssh-keygen -t rsa -C "workemail@email.com"
  5. $Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): work

生成了两个ssh key一个重命名为github,一个重命名为work,来区分。
然后在.ssh目录下新建一个config文件。配置如下

  1. Host github.com //这个我就保持与HostName一致了
  2. HostName github.com //对应仓库的站点
  3. User name //用户名
  4. IdentityFile ~/.ssh/github //对应的ssh key 文件
  5. Host work.com
  6. HostName work.com
  7. User name
  8. IdentityFile ~/.ssh/work
这样如果我从github来clone项目,如
  1. git clone git@github.com:name/example.git

就能够知道是用github.com这个仓库站点对应的ssh key了,然后就能够clone或者push了。

不过暂时没搞懂的是,Host和HostName我这里要保持一致,本来Host是HostName的别名应该可以任意命名的,但是如果改动了就不行了。暂时不明就里。

最后一点就是关于config user.name和user.email的问题,我只是将公司里的账户设置成全局的,如下

  1. git config --global user.name "name"
  2. git config --global user.email "email"

但是发现没有什么影响,即使从github上拉出的仓库,没有配置单独的user.name和user.email

  1. git config user.name "name"
  2. git config user.email "email"

发现也没有什么影响,可能是从config文件中已经读取到User以及通过ssh key就知道了对应邮箱了吧,纯属猜测。

PS,折腾了好几个小时,才在机器上搞定两个账户,记下来备忘。
同时熟悉了若干命令,如解除 user.name或user.email的配置

  1. git config --unset --global user.name "name"

[1] 在windows下
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注