Wait the light to fall

一个客户端设置多个 github 账号

焉知非鱼

github

本文是一篇笔记, 内容经过一些修改, 很抱歉原文链接找不到了。如有侵权请告知删除。这是其中一处链接 一个客户端设置多个github账号如何同一台电脑配置多个git或github账号

一个客户端设置两个 github 账号 #

需求 #

需要在一台电脑上同时使用两个不同的 github 账号,负责不同的用途。(例如一个用于技术, 一个用于非技术)

前期工作 #

至少有两个 github 账号 (假设有两个账号 一个为 ohmyone ,另一个为 ohmytwo ) 取消 git 全局设置

git config --global --unset user.name
git config --global --unset user.email

SSH 配置 #

生成 id_rsa 私钥 和 id_rsa.pub 公钥。 #

ohmyone 可以直接回车,默认生成 id_rsaid_rsa.pub

ssh-keygen -t rsa -C "ohmyone@sina.com"

但是 ohmytwo 会出现提示输入文件名,输入与默认配置不一样的文件名,比如: id_rsa_ohmytwo

cd ~/.ssh
ssh-keygen -t rsa -C "ohmytwo@outlook.com"  # 输入文件名, 但是别输密码

github 添加公钥 id_rsa.pub 和 id_rsa_two.pub。 分别登陆 ohmyone , ohmytwo 的账号,在 Account Settings 的 SSH Keys 里,点 Add SSH Keys ,将公钥(.pub文件)中的内容粘贴到”Key”中,并输入”Title”.

添加 ssh key #

Windows 上推荐使用 GitBash 命令行工具:

eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_ohmytwo

可以在添加前使用下面命令删除所有的 key

ssh-add -D

最后可以通过下面命令,查看key的设置

ssh-add -l

修改 ssh config 文件 #

cd ~/.ssh/touch config

打开 .ssh 文件夹下的 config 文件,进行配置

# default
Host github.com
HostName github.com
User ohmyone
IdentityFile ~/.ssh/id_rsa

# ohmytwo
Host ohmytwo.github.com  # 前缀名可以任意设置
HostName github.com
User ohmytwo
IdentityFile ~/.ssh/id_rsa_two

这里必须采用这样的方式设置,否则 push 时会出现以下错误:

ERROR: Permission to ohmytwo/ohmytwo.github.com.git denied to ohmyone.

简单分析下原因,我们可以发现 ssh 客户端是通过类似:

git@github.com:ohmyone/ohmyone.github.com.git 

这样的 git 地址中的 UserHost 来识别使用哪个本地私钥的。 很明显,如果 User 和 Host 始终为 git 和 github.com,那么就只能使用一个私钥。 所以需要上面的方式配置,每个账号使用了自己的 Host,每个 Host 的域名做 CNAME 解析到 github.com,这样 ssh 在连接时就可以区别不同的账号了。

测试 SSH 连接 #

ssh -T git@github.com            # 测试 ohmyone ssh 连接
#Hi ohmyone! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T git@ohmytwo.github.com    # 测试 ohmytwo ssh 连接
#Hi ohmytwo! You've successfully authenticated, but GitHub does not provide shell access.

但是这样还没有完,下面还有关联的设置。

用户名和邮箱的局部配置 #

新建 git 项目或者 clone 已有的项目。可以用 git init 或者 git clone 创建本地项目。 分别在 ohmyone 和 ohmytwo 的git项目目录下,使用下面的命令设置账号关联

git config user.name  ohmyone
git config user.email  ohmyone@sina.com

git config user.name  ohmytwo
git config user.email  ohmytwo@outlook.com

即在各自的 git 项目文件夹中设置局部 user 和 email, 执行上面的 git 命令。

查看git项目的配置

git config --list

查看 ohmyone 的remote.origin.url= git@github.com:ohmyone/ohmyone.github.com.git 查看 ohmytwo 的remote.origin.url= git@github.com:ohmytwo/ohmytwo.github.com.git

修改远端仓库 #

由于 ohmyone 使用的是默认的 Host,所以不需要修改,但是 ohmytwo 使用的 Host 是 ohmytwo.github.com ,则需要进行修改

git remote rm origin
git remote add origin git@ohmytwo.github.com:ohmytwo/ohmytwo.github.com.git

上传更改。上面所有的设置无误后,可以修改代码,然后上传了。

提交 #

git add -A
git commit -m "your comments"
git push

如果遇到warning

warning: push.default is unset; its implicit value is changing in Git 2.0 from ‘matching’ to ‘simple’. To squelch this messageand maintain the current behavior after the default changes, use…

推荐使用下面命令设置。

git config --global push.default simple

参考

http://blog.csdn.net/wzy_1988/article/details/19967465 http://testerhome.com/topics/752 http://hily.me/blog/2013/05/github-multiple-account-and-multiple-repository/