Skip to content
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

Open
junhey opened this issue Jul 28, 2018 · 4 comments
Open

git多帐号的SSH key切换 #28

junhey opened this issue Jul 28, 2018 · 4 comments

Comments

@junhey
Copy link
Owner

junhey commented Jul 28, 2018

Update: #28 (comment)

有两个github帐号,一个是个人所用,一个是为公司项目所用。如果是单用户(single-user),很方便,默认拿id_rsa与你的github服务器的公钥对比;如果是多用户(multi-user)如user1,user2,那么就不能用在user2的身上了,这个时候就要配置一下了,具体配置如下:

  1. 新建user2的SSH Key
#新建SSH key:
cd ~/.ssh     # 切换到C:\Users\Administrator\.ssh
ssh-keygen -t rsa -C "mywork@email.com"  # 新建工作的SSH key
# 设置名称为id_rsa_work
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_work
  1. 新密钥添加到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
  1. 修改config文件
    在~/.ssh目录下找到config文件,如果没有就创建:
touch config        # 创建config

然后修改如下:
config配置如下:

# 该文件用于配置私钥对应的服务器
# Default github user(first@mail.com)
Host github.com
HostName github.com
User git
IdentityFile C:/Users/Administrator/.ssh/id_rsa

# second user(second@mail.com)
# 建一个gitlab别名,新建的帐号使用这个别名做克隆和更新
Host gitlab.com
HostName gitlab.com
User git
IdentityFile C:/Users/Administrator/.ssh/id_rsa_work

如果存在的话,其实就是往这个config中添加一个Host:

#建一个新的github别名,新建的帐号使用这个别名做克隆和更新
Host github2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa2

其规则就是:从上至下读取config的内容,在每个Host下寻找对应的私钥。这里将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名如:github2,那么原地址是:git@github.com:git_catalog/Mywork.git,替换后应该是:github2:git_catalog/Mywork.git.

  1. 打开新生成的公钥文件,类似于:~/.ssh/id_rsa2.pub,将里面的内容添加到Github/Gitlab后台。

  2. 测试

ssh -T git@github.com
ssh -T git@gitlab.com
  1. 应用
git clone gitlab:git_catalog/Mywork.git

克隆成功,那么就完全OK啦!

遇到问题是git pull push的时候有问题,原因是还是用的原来的global的用户名和邮箱,解决方式是:

  1. 取消全局设置config
git config --global --unset user.name  
git config --global --unset user.email 
  1. 设置每个项目repo的自己的user.email
git config  user.email "xxxx@xx.com"  
git config  user.name "name"

带来的麻烦是每次clone了项目之后需要config下

问题:如果之前已经提交过commit怎么修改呢?
解决方式:

  1. 最近一次commit问题
git commit --amend --reset-author
  1. 所有的commit问题
    可以采用git filter-branch这个工具,具体用法请谷歌。
@ruochuan12
Copy link

有时也会出现每次重启电脑(windows)又要重新添加ssh-add的问题~

@junhey junhey transferred this issue from junhey/studyNotes Jul 2, 2021
@junhey
Copy link
Owner Author

junhey commented Jul 2, 2021

对于 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

然后把它添加到开机启动项中:系统偏好设置>用户与群组>登录项。

@junhey
Copy link
Owner Author

junhey commented Jul 2, 2021

按目录配置多用户
在2017年,git新发布的版本2.13.0包含了一个新的功能includeIf配置,可以把匹配的路径使用对应的配置用户名和邮箱;

在~/目录下面存在三个配置文件,

.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最后需要/结尾
文件~/.gitconfig里面原有的user部分需要删除
个人工程目录和公司工程目录需要要求是非包含关系,就是这两个工程目录配置路径不可以是父子关系。

@junhey
Copy link
Owner Author

junhey commented Jul 2, 2021

--- 生成账户私钥并添加到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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants