公钥和私钥
在Github里面没法一个ssh的公钥放在两个账号, 所以对于GHB的公钥, 你没办法用之前的. 再说, GHA的公钥包含的邮箱也不适合GHB使用. 所以要新创建一个公钥用于GHBssh-keygen -t rsa -b 4096 -C "GHB@163.com"
创建新公钥, 第一个提示会问保存的公钥地方, 默认是
~/.ssh/id_rsa
.如果你直接enter, 会再询问你是否覆盖. 此时会覆盖掉旧的私钥公钥组合! 这里例如创建新私钥为~/.ssh/id_rsa2
,对应公钥就是~/.ssh/id_rsa2.pub
. 保存的私钥名也可以通过-f "~/.ssh/id_rsa2"
选项来指定. 随后将公钥2放到GHB的账号里. 这是第一步.设置仓库repository的user信息
默认情况, git提交时使用的是global的user.name
和user.email
信息. 显然, 这里两个账号不可能用同一个global信息 (global信息在~/.gitconfig
中有). 此时可以设置单独的仓库的信息. 单独的仓库信息在仓库文件夹的.git/config
文件内. 因为global信息对应GHA的, 所以我就不加设置了, 直接在GHB仓库内设置: git config --local user.name "GHB"; git config --local user.email "GHB@163.com"
. 这样就OK了. 查看设置信息git config --list
, 可能有两套user.*, 下面的(local)会覆盖上面的(global). --local
设置单独仓库信息, 比global优先. 不设定--global
使用的config默认是--local
的.有网上别的说法是, 每个仓库都单独设置, 因为我global信息对应了太多的仓库, 所以我就不这样做了, 有兴趣可以自己尝试:
# 取消global设置
git config --global --unset user.name
git config --global --unset user.email
# 设置每个项目repo的自己的user.email
cd ~/repo_A
git config user.email "GHA@GHA.com"
git config user.name "GHA"
cd ~/repo_B
git config user.email "GHB@GHB.com"
git config user.name "GHB"
交换通讯私钥
这是最暴力的方法.如果在上面生成公钥和设置仓库user信息都完成后, 进行提交, 会报错:
ERROR: Permission to GHB/GHBtest.git denied to GHA.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
#! /bin/bash
prefix="$1"
if [ -z $1 ];then
prefix="id_rsa2"
fi
mv ~/.ssh/$prefix ~/.ssh/${prefix}_tmp
mv ~/.ssh/${prefix}.pub ~/.ssh/${prefix}_tmp.pub
mv ~/.ssh/id_rsa ~/.ssh/$prefix
mv ~/.ssh/id_rsa.pub ~/.ssh/${prefix}.pub
mv ~/.ssh/${prefix}_tmp ~/.ssh/id_rsa
mv ~/.ssh/${prefix}_tmp.pub ~/.ssh/id_rsa.pub
切记: 使用完该脚本后, 再次运行一次, 将私钥和公钥换回默认的!!!
修改host信息
这个方法不需要经常交替私钥/公钥文件, 但操作比较麻烦. 原理就是修改新的host, 其hostname指向指定host(例如github.com), 而验证文件则自己指定.touch ~/.ssh/config #创建一个ssh时信息注册文件, host信息可以从里面解析. 其实有下面一步就不用touch了
echo "# default
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# two
Host two.github.com # 前缀名可以任意设置
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa2
" > ~/.ssh/config
# 将host信息写入到注册文件, 这里随你怎么搞啦
# 测试连接是否良好
ssh -T git@github.com # 测试GHA 私钥ssh连接
#Hi ***! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@two.github.com # 测试GHB 私钥ssh连接
#Hi ***! You've successfully authenticated, but GitHub does not provide shell access.
# 去到GHB仓库, 这个地址根据自己需要
cd ~/GHB
# 查看远程仓库信息
git remote -v
#origin git@github.com:GHB/GHBtest.git (fetch)
#origin git@github.com:GHB/GHBtest.git (push)
# 修改远程仓库地址!
# 就是修改上面origin的@github.com部分换成@two.github.com
git remote set-url origin git@two.github.com:GHB/GHBtest.git
.ssh/config
和git remote set-url origin 新地址
的两关键步骤了.Reference
一个客户端设置多个github账号
最近遇到了这样的需求,需要在一台电脑上同时使用两个github账号,负责不同的用途,所以搜索了一些文章,进行了一下实践。
1.前期工作
- 至少有两个github账号 (假设有两个账号 一个为 one ,另一个为 two)
- 取消git全局设置
2.SSH配置
- 生成 id_rsa 私钥 , id_rsa.pub 公钥
one可以直接回车,默认生成id_rsa和id_rsa.pub。
但是two会出现提示输入文件名,输入与默认配置不一样的文件名,比如: id_rsa_two。
github添加公钥 id_rsa.pub , id_rsa_two.pub
分别登陆one,two的账号,在Account Settings
的SSH Keys
里,点Add SSH Keys
,将公钥(.pub文件)中的内容粘贴到”Key”中,并输入”Title”.添加 ssh key
可以在添加前使用下面命令删除所有的key
最后可以通过下面命令,查看key的设置
修改ssh config文件
打开.ssh文件夹下的config文件,进行配置
这里必须采用这样的方式设置,否则push时会出现以下错误:
ERROR: Permission to two/two.github.com.git denied to one.
简单分析下原因,我们可以发现 ssh 客户端是通过类似:
git@github.com:one/one.github.com.git
这样的 git 地址中的 User 和 Host 来识别使用哪个本地私钥的。
很明显,如果 User 和 Host 始终为 git 和 github.com,那么就只能使用一个私钥。
所以需要上面的方式配置,每个账号使用了自己的 Host,每个 Host 的域名做 CNAME 解析到 github.com,这样 ssh 在连接时就可以区别不同的账号了。
但是这样还没有完,下面还有关联的设置。
新建git项目或者clone已有的项目
- 可以用 git init 或者 git clone 创建本地项目
- 分别在one和two的git项目目录下,使用下面的命令设置账号关联
查看git项目的配置
查看one的remote.origin.url=git@github.com:one/one.github.com.git
查看two的remote.origin.url=git@github.com:two/two.github.com.git
由于one使用的是默认的Host,所以不需要修改,但是two使用的是two.github.com,则需要进行修改
上传更改
- 上面所有的设置无误后,可以修改代码,然后上传了。
如果遇到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…
推荐使用下面命令设置。
参考
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/
一个客户端设置多个github账号
前期工作
两个GitHub账号(假设两个账号为one,two)
取消Git全局设置
$ git config --global --unset user.name
$ git config --global --unset user.email
SSH配置
生成id_rsa
私钥,id_rsa.pub
公钥。one可以直接回车,默认生成 id_rsa 和 id_rsa.pub 。
$ ssh-keygen -t rsa -C "one@xx.com"
添加two会出现提示输入文件名,输入与默认配置不一样的文件名,如:id_rsa_two。
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "two@126.com" # 之后会提示输入文件名
GitHub添加公钥 id_rsa.pub 、 id_rsa_two.pub,分别登陆one,two的账号,在 Account Settings 的 SSH Keys 里,点 Add SSH Keys
,将公钥(.pub文件)中的内容粘贴到 Key 中,并输入 Title。
添加 ssh Key
$ ssh-add ~/.ssh/id_rsa
$ ssh-add ~/.ssh/id_rsa_two
可以在添加前使用下面命令删除所有的 key
$ ssh-add -D
最后可以通过下面命令,查看 key 的设置
$ ssh-add -l
修改ssh config文件
$ cd ~/.ssh/
$ touch config
打开 .ssh 文件夹下的 config
文件,进行配置
# default
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# two
Host two.github.com # 前缀名可以任意设置
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_two
- 这里必须采用这样的方式设置,否则 push 时会出现以下错误:
ERROR: Permission to two/two.github.com.git denied to one.
简单分析下原因,我们可以发现 ssh 客户端是通过类似:
git@github.com:one/one.github.com.git
这样的 Git 地址中的 User 和 Host 来识别使用哪个本地私钥的。
很明显,如果 User 和 Host 始终为 git 和 github.com,那么就只能使用一个私钥。
所以需要上面的方式配置,每个账号使用了自己的 Host,每个 Host 的域名做 CNAME 解析到 github.com,这样 ssh 在连接时就可以区别不同的账号了。
$ ssh -T git@github.com # 测试one ssh连接
# Hi ***! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@two.github.com # 测试two ssh连接
# Hi ***! You've successfully authenticated, but GitHub does not provide shell access.
但是这样还没有完,下面还有关联的设置。
在Git项目中配置账号关联
可以用 git init
或者 git clone
创建本地项目
分别在one和two的git项目目录下,使用下面的命令设置名字和邮箱
$ git config user.name "__name__" # __name__ 例如 one
$ git config user.email "__email__" # __email__ 例如 one@126.com
注意:由于我不知道Hexo怎样配置 局部的config,所以,我将two的config使用全局,而工作目录配置局部。
$ git config --global user.name "__name__" # __name__ 例如 two
$ git config --global user.email "__email__" # __email__ 例如 two@126.com
查看git项目的配置
$ git config --list
查看 one 的 remote.origin.url=git@github.com:one/one.github.com.git
查看 two 的 remote.origin.url=git@github.com:two/two.github.com.git
由于 one 使用的是默认的 Host ,所以不需要修改,但是 two 使用的是 two.github.com ,则需要进行修改
$ git remote rm origin
$ git remote add origin git@two.github.com:two/two.github.com.git
我在Hexo中的配置(使用two账号)
deploy:
type: git
repo: git@two.github.com:two/two.github.io.git
branch: master
上传更改
上面所有的设置无误后,可以修改代码,然后上传了。
$ 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
No comments:
Post a Comment