一台机器使用不一样的Git帐号
场景
在平常使用 git
做为仓库使用的时候,有时可能会遇到这样的一些状况:html
1. 有两个 `github` 帐号,一台电脑怎么同时链接这两个帐号进行维护呢? 2. 本身用一个 `github` 帐号,平时用来更新本身的一些资料;公司使用的 `gitlab`(也是 `git` 的衍生产品)
SSH Key
的配置
1.Windows
下打开 Git Bash
,建立 SSH Key
,按提示输入密码,能够不填密码一路回车git
$ ssh-keygen -t rsa -C "注册邮箱"
而后用户主目录 /.ssh/
下有两个文件,id_rsa
是私钥,id_rsa.pub
是公钥github
2.获取 key
,打开 .ssh
下的 id_rsa.pub
文件,里面的内容就是 key
的内容shell
$ start ~/.ssh/id_rsa.pub
3.登陆 GitHub
,打开SSH Keys
页面,快捷地址vim
4.测试 ssh key
是否成功,使用命令windows
$ ssh -T git@github.com
若是出现 You’ve successfully authenticated, but GitHub does not provide shell access
。这就表示已成功连上 github
。bash
Gitlab 和 Github 不一样帐号配置
通常开发用户应该都配置过一个git的帐号,想在添加一个帐号。服务器
清除 git
的全局设置
使用 git config --list 查看当前配置app
若是你以前在设置本地仓库和 github
链接的时候设置过 user.name
和 user.email
, 那么你必须首先清楚掉该设置,由于不清楚掉该设置,两个帐号在提交资料的时候,验证确定冲突(只能设置一个全局的user.name
和 user.email
,而你如今有两个帐号就对应两个不一样的)。ssh
1.取消global git config --global --unset user.name git config --global --unset user.email 2.设置每一个项目repo的本身的user.email git config user.email "xxxx@xx.com" git config user.name "suzie"
或者直接直接编辑电脑.gitconfig
文件
$ cd ~// 进入根目录 $ vim .gitconfig // 把 `name` 和 `email` 都去掉
生成另一个帐号新的SSH keys
- 用
ssh-keygen
命令生成一组新的id_rsa_new
和id_rsa_new.pub
$ ssh-keygen -t rsa -C "new email"
平时咱们都是直接回车,默认生成 id_rsa
和 id_rsa.pub
。这里特别须要注意,出现提示输入文件名的时候(Enter file in which to save the key (~/.ssh/id_rsa): id_rsa_new
)要输入与默认配置不同的文件名,好比:我这里填的是 id_rsa_new
.
注:windows
用户和 mac
用户的区别就是,mac
中 .ssh
文件夹在根目录下,因此表示成 ~/.ssh/
,而 windwos
用户是 C:\Users\Administrator\.ssh
。
- 执行
ssh-agent
让ssh
识别新的私钥,由于默认只读取id_rsa
,为了让SSH
识别新的私钥,需将其添加到SSH agent
中:
$ ssh-add ~/.ssh/id_rsa_work
若是出现 Could not open a connection to your authentication agent 的错误,就试着用如下命令:
$ ssh-agent bash $ ssh-add ~/.ssh/id_rsa_work
- 配置
~/.ssh/config
文件
前面咱们在~/.ssh
目录下面,使用ssh-keygen -C “your_email” -t rsa
生成公私秘钥,当有多个github
帐号的时候,能够生成多组rsa
的公司密钥。而后配置~/.ssh/config
文件(若是没有的话请从新建立一个)。
$ touch config # 建立config文件
而后修改以下:
个人 config
配置以下:
# 该文件用于配置私钥对应的服务器 # Default github user(609514766@qq.com) Host git@github.com HostName github.com User jawil IdentityFile ~/.ssh/id_rsa_github # gitlab user(yenian.ll@alibaba-inc.com) # 建一个gitlab别名,新建的账号使用这个别名作克隆和更新 Host git@gitlab.alibaba-inc.com HostName gitlab.alibaba-inc.com User yenian.ll IdentityFile ~/.ssh/id_rsa
若是存在 config
文件的话,其实就是往这个 config
中添加一个 Host
。
同一个电脑 两个 github 帐号配置
然而事情并无到此结束,装完逼还想跑那是不行的。小猪往后发现本身是一个念旧的人。他想要同时使用两个 github
。
那好吧,What a big deal~
这个时候咱们须要先找到 ~/.ssh/config
文件,而后往里面添加以下配置
Host jawil.github.com HostName github.com IdentityFile ~/.ssh/github_rsa
咱们指定 pig.github.com 这个"做用域"下的ssh链接统一指向 github.com
,而且使用以前生成好的 github_rsa
这个密钥加密。
那么默认的就是使用 ~/.ssh/id_rsa
这个密钥加密咯。 而且 HostName
与“做用域”相同。这个就不须要咱们管了。
这个时候咱们测试一下呗。
$ ssh -T git@jawil.github.com Hi jawil! You've successfully authenticated, but GitHub does not provide shell access. $ ssh -T git@github.com Hi yenian! You've successfully authenticated, but GitHub does not provide shell access.
: ) , orz , 这时候还差最后一步,就是修改一下咱们在 jawil
克隆下来的项目的 remote
“做用域” 。
$ git remote rm origin $ git remote add origin git@jawil.github.com:jawil/demo.git // 注意是 `jawil.github.com` 噢?把这个理解为一个“做用域”吧。 $ git push origin master Everything up-to-date
上述步骤不成功解决方案
- 笨方法:用到哪一个就设置那个的全局参数
git config --global user.name Levi git config --global user.email Levi@email.com
- 注意
# 密钥文件的命名为id_isa,改成其余名字可能会有错误 # 参考: I found the solution to the problem in the gitlab README (http://doc.gitlab.com/ce/ssh/README.html) . I used a different filename for my key instead of id_rsa, so the key wasn't working. When I created a config file in my .ssh directory and configured the settings appropriately, "git clone" worked without a hitch.