同一台电脑管理多个 Git 账号
生成 SSH Key
需要手动输入 SSH Key
生成的文件名
$ ssh-keygen -t rsa -C "your email"
1
将多个公匙添加至相应的 Github 账号
在 .ssh 目录下新建 config 文件,写入以下内容
# 相当于 github.com 的别名,需要设置为 git remote url 中的 host
Host hotmail.github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_hotmail
Host gmail.github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_gmail
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
验证是否可以连接成功
$ ssh -T git@hotmail.github.com
$ ssh -T git@gmail.github.com
1
2
2
替换 Remote Url
现有的项目的 remote url
和之后克隆这两个账号下的仓库是都需要将 host
替换为 config
文件中设置的 host
eg:
git@github.com:zhaiyuxin103/vuepress.git -> git@hotmail.github.com:zhaiyuxin103/vuepress.git
git remote set-url origin git@hotmail.github.com:zhaiyuxin103/vuepress.git
git clone git@hotmail.github.com:zhaiyuxin103/vuepress.git
1
2
2
清除 Git 的全局设置
# 取消 global config
git config --global --unset user.name
git config --global --unset user.email
# 为每个 repo 设置自己的 user,email
git config user.email "zhaiyuxin103@hotmail.com"
git config user.name "zhaiyuxin103"
1
2
3
4
5
6
7
2
3
4
5
6
7