在同一机器上对不同repo使用不同的github账号是个常见需求。举个例子,repo1托管在github账号x1下,而repo2托管在账号x2下,如何方便地在同一机器上使用不同账号自动git push到对应的远端?比较直接的做法是在不同repo目录下使用git config配置用户名。除此之外我们可以借用SSH config文件来把不同github账号与repo联系起来。在SSH config中定义多个不同的host项即可,然后在访问github时,使用一个虚拟host作为别名代替真正的主机名github.com即可。

一、git config配置

git配置级别

在git中,我们使用git config 命令用来配置git的配置文件,git配置级别主要有以下3类:

  1. 仓库级别 local 【优先级最高】,git 仓库级别对应的配置文件是当前仓库下的.git/config
  2. 用户级别 global【优先级次之】,git 用户级别对应的配置文件是用户家目录下的~/.gitconfig
  3. 系统级别 system【优先级最低】,git系统级别对应的配置文件是git安装目录下的 /etc/gitconfig

配置认证

这里以一个新的仓库配置为例,具体如下:

1# 生成名为 mykey 的 SSH 密钥对,公钥配置到github仓库上
2ssh-keygen -f mykey
3
4# 配置仓库级别的认证
5git config --local user.name "Your Name"
6git config --local user.email "your@email.com"
7git config --local ssh.hostname github.com
8git config --local ssh.identity mykey

这里如果是进行global、system级别的配置,只需要把上面的local做下更换就可以了。配置完成后,可以通过如下两个方式查看

 1# 直接查看对应的配置文件
 2yangbk@yangbk:~/site/blog$ cat .git/config
 3[core]
 4        repositoryformatversion = 0
 5        filemode = true
 6        bare = false
 7        logallrefupdates = true
 8[remote "test"]
 9        url = git@github.com:361way/test.git
10        fetch = +refs/heads/*:refs/remotes/361way/*
11[ssh]
12        identity = /home/yangbk/.ssh/id_rsa
13
14# 命令方式查看
15yangbk@yangbk:~/site/blog$ git config --local -l
16core.repositoryformatversion=0
17core.filemode=true
18core.bare=false
19core.logallrefupdates=true
20remote.test.url=git@github.com:361way/test.git
21remote.test.fetch=+refs/heads/*:refs/remotes/361way/*
22ssh.identity=/home/yangbk/.ssh/id_rsa
23yangbk@yangbk:~/site/blog$ cd content/

push提交

在github上创建完对应的仓库后,可以通过如下命令配置首次push

1git init 
2git add *
3git commit -m 'test'
4git remote add test git@github.com:361way/test.git
5git push test

编辑配置

可以使用git config -e 编辑配置文件,会打开对应的配置文件,默认使用nano进行编辑,命令如下:

1git config --local -e 编辑仓库级别配置文件
2git config --global -e 编辑用户级别配置文件
3git config --system -e 编辑系统级别配置文件

二、ssh config配置方式

生成SSH Key

直接按步骤来即可: # Generating a new SSH key and adding it to the ssh-agent

 1$ ssh-keygen -t ed25519 -C "your_email@example.com"
 2
 3Generating public/private ed25519 key pair.
 4Enter file in which to save the key (/home/user/.ssh/id_ed25519):
 5Enter passphrase (empty for no passphrase):
 6Enter same passphrase again:
 7Your identification has been saved in /home/user/.ssh/id_ed25519
 8Your public key has been saved in /home/user/.ssh/id_ed25519.pub
 9The key fingerprint is:
10SHA256: xxx your_email@example.com

Add your SSH private key to the ssh-agent:

1$ ssh-add ~/.ssh/id_ed25519
2Identity added: /home/user/.ssh/id_ed25519 (your_email@example.com)

按Tutorial步骤来,点点鼠标复制粘贴即可: # Adding a new SSH key to your GitHub account

拷贝公钥:

1$ cat ~/.ssh/id_ed25519.pub
2ssh-ed25519 xxxx your_email@example.com

然后把公钥通过GitHub配置页添加至账户: Profile Photo -> Settings -> SSH and GPG keys -> New SSH keys

编辑SSH Config

这是最重要的的部分,编辑 ~/.ssh/config (不存在则创建一个):

 1Host github.com
 2    HostName github.com
 3    User git
 4    IdentityFile ~/.ssh/id_rsa
 5
 6Host githubx1
 7    HostName github.com
 8    User git
 9    IdentityFile ~/.ssh/id_rsa_x1
10
11Host githubx2
12    HostName github.com
13    User git
14    IdentityFile ~/.ssh/id_rsa_x2

各字段的解释:

  • Host: 主机别名,也是每段定义项的名字,将来用这个别名来指代所用的用户名及私钥
  • HostName: 要登录的远端主机名 (对github来说就是github.com)
  • User: SSH连接所用的用户名 (对github来说就是git)
  • IdentityFile: 上面生成的私钥

假设上面示例文件中的私钥和账户对应关系如下:

  • id_rsa -> main@example.com
  • id_rsa_x1 -> x1@example.com
  • id_rsa_x2 -> x2@example.com

一个repo的远端url大概长这样:

1git@github.com:361way/python.git

对于无SSH config或上面的示例配置,使用上面url (github.com) 进行访问时,使用的是账户main@example.com。如果我们想用x1@example.com来访问此repo,需要将它的主机名改一下,与SSH config中的配置相对应:

1git@githubx1:361way/python.git

测试连接

验证上面配置是否生效也很简单,使用下面命令测试即可:

1$ ssh -T git@githubx1
2Hi x1! You've successfully authenticated, but GitHub does not provide shell access.
3
4$ ssh -T git@githubx2
5Hi x2! You've successfully authenticated, but GitHub does not provide shell access.
6
7$ ssh -T wrongusername@githubx2
8wrongusername@github.com: Permission denied (publickey).

如果登录成功,Hi之后会显示对应登录账户的GitHub用户名。