New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
git多帐号的SSH key切换 #28
Comments
有时也会出现每次重启电脑(windows)又要重新添加 |
对于 Mac,可能会遇到升级系统或者重启系统之后,SSH私钥失效的问题,这时候我们可以通过在~/.ssh/config文件中添加如下内容来解决: Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/github_id_rsa
IdentityFile ~/.ssh/gitlab_id_rsa 如果还不生效,那就可以通过自己编写 shell 脚本 (这里我使用的是 zsh,所以配置的是#! /bin/zsh,若使用的是系统自带的终端,可以修改为#! /bin/bash): vim .ssh/ssh_add_private_keys.sh
#! /bin/zsh
# 添加 github 公钥
ssh-add ~/.ssh/github_id_rsa
# 添加 公司 gitlab 公钥
ssh-add ~/.ssh/gitlab_id_rsa
// 赋予sh文件可运行权限
chmod +x .ssh/ssh_add_private_keys.sh 然后把它添加到开机启动项中:系统偏好设置>用户与群组>登录项。 |
按目录配置多用户 在~/目录下面存在三个配置文件, .gitconfig // 全局通用配置文件
.gitconfig-self // 个人工程配置文件
.gitconfig-work // 公司工程配置文件 全局通用配置文件~/.gitconfig里面的内容是:主要是通过includeIf配置匹配不用的目录映射到不同配置文件上, [includeIf "gitdir:~/self-workspace/"]
path = .gitconfig-self
[includeIf "gitdir:~/workspace/"]
path = .gitconfig-work 个人工程配置文件~/.gitconfig-self: [user]
name = yourname-self
email = yourname-self@gmail.com 公司工程配置文件~/.gitconfig-work: [user]
name = yourname-work
email = yourname-work@yourCompanyName.com 遇到的问题: 文件~/.gitconfig里面的includeIf后面的path最后需要/结尾 |
--- 生成账户私钥并添加到ssh-agent信任列表
ssh-keygen -t rsa -C "junhey@qq.com" -f ~/.ssh/github.id_rsa
ssh-keygen -t rsa -C "junhey.hj@alibaba-inc.com" -f ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/github.id_rsa
--- 在~/.ssh目录下新建config文件,以下是内容:
# 公司
Host gitlab.com
Hostname gitlab.com
IdentityFile ~/.ssh/id_rsa
User company
# 个人
Host github.com
Hostname github.com
IdentityFile ~/.ssh/github.id_rsa
User github
--- 测试连接
ssh -T git@github.com
--- 按目录配置多用户
# 在~/目录下面存在三个配置文件
.gitconfig // 全局通用配置文件
[includeIf "gitdir:/Users/junhey/Documents/github/"]
path = .gitconfig-self
[includeIf "gitdir:/Users/junhey/Documents/gitlab/"]
path = .gitconfig-work
.gitconfig-self // 个人工程配置文件
[user]
name = junhey
email = junhey@qq.com
.gitconfig-work // 公司工程配置文件
[user]
name = junhey.hj
email = junhey.hj@alibaba-inc.com
|
Update: #28 (comment)
有两个github帐号,一个是个人所用,一个是为公司项目所用。如果是单用户(single-user),很方便,默认拿id_rsa与你的github服务器的公钥对比;如果是多用户(multi-user)如user1,user2,那么就不能用在user2的身上了,这个时候就要配置一下了,具体配置如下:
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文件,如果没有就创建:
然后修改如下:
config配置如下:
如果存在的话,其实就是往这个config中添加一个Host:
其规则就是:从上至下读取config的内容,在每个Host下寻找对应的私钥。这里将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名如:github2,那么原地址是:git@github.com:git_catalog/Mywork.git,替换后应该是:github2:git_catalog/Mywork.git.
打开新生成的公钥文件,类似于:~/.ssh/id_rsa2.pub,将里面的内容添加到Github/Gitlab后台。
测试
克隆成功,那么就完全OK啦!
遇到问题是git pull push的时候有问题,原因是还是用的原来的global的用户名和邮箱,解决方式是:
带来的麻烦是每次clone了项目之后需要config下
问题:如果之前已经提交过commit怎么修改呢?
解决方式:
可以采用git filter-branch这个工具,具体用法请谷歌。
The text was updated successfully, but these errors were encountered: