mac配置多个代码仓库账号

374 阅读2分钟

有时候会遇到在一台电脑上使用多种仓库的情况,git gitee gitlab,来,配置看看~

1. 如果要配置多个账号,那么需要生成多个ssh,在对应的代码仓库

-   github_rsa、gitee_rsa、gitee_rsa 这样的ssh文件名是自定义的,随便取,跟全局的config保持一致就行
gitee:
ssh-keygen -t rsa -C "你的gitee邮箱" -f ~/.ssh/github_rsa 
Github:
ssh-keygen -t rsa -C "你的gitee邮箱" -f ~/.ssh/gitee_rsa
gitlab:
ssh-keygen -t rsa -C "你的gitee邮箱" -f ~/.ssh/gitee_rsa
配置时具体的代码过程,以gitlab为例:
MacBook-Pro:~ $ ssh-keygen -t rsa -C "你的email"Generating public/private rsa key pair.
​
Enter file in which to save the key (~/.ssh/id_rsa): 
​
Created directory '~/.ssh'.
​
Enter passphrase (empty for no passphrase): 
​
Enter same passphrase again: 
​
Your identification has been saved in ~/.ssh/id_rsa.
​
Your public key has been saved in ~/.ssh/id_rsa.pub. //找到文件,复制密匙到个人的gitlab网站的ssh中

2. 把公钥加到对应的代码仓库

  • ssh如果不能用,要在命令行输入,换一下ssh算法,再重新加入ssh ssh -oHostKeyAlgorithms=+ssh-dss 账号
  • 在~/.ssh/目录下创建一个config文件,分别配置公司gitlab的ssh key 和 自己个人的 ssh key
    • /.ssh/通常在用户名下,是全局的
Host gitlab
  HostName 公司的代码仓库服务器地址
  User gitlab用户名
  IdentityFile ~/.ssh/id_rsa
Host github.com
  HostName github.com
  User github用户名
  IdentityFile ~/.ssh/github_rsa  // 这个

3. 测试是否装好了

ssh -T git@gitlab
ssh -T git@github.com
ssh -T git@gitee
  • gitee如果连不上没关系,在项目里面重新配置ssh,走一遍配置流程

4. 配置global账号和某个仓库的账号

  • 如果是公司的电脑,那么建议设置global,单个项目单个设置local 的user
git config --local user.name "gitee用户名" //某个项目的
git config --local user.email "gitee邮箱" 

git config --global user.name '全局用户名' //全局的
git config --global user.email 全局邮箱

// 查看git配置
git config --list 

参考

  1. www.cnblogs.com/hezhi/p/103…
  2. www.jianshu.com/p/6c8921839…
  3. blog.csdn.net/jxwzh/artic…