本地配置多个Git仓库

工作用到公司的git repo,自己在github、coding有自己的repo,但是是在同一台笔记本上连接
一个ssh证书对应一个仓库,在做相应的配置即可
以公司、GitHub、Coding三个repo为例

创建三份ssh证书

1、根据每个repo用到的email生成ssh证书
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
2、根据不同的repo进行命名,后面需要给每个repo配置证书
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [XXXXXX(每个repo对应的ssh名称)]
3、输入密码,一般都是直接回车,每次都是免密
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
重复3次,生成3个ssh证书
分别为: id_rsa_work 、id_rsa_github、id_rsa_coding
位于~/.ssh 目录下(以Mac为例)

配置repo相应的ssh证书

~/.ssh 目录下新建 config文件 touch ~/config
填写内容如下

#dhms 
host git-server
user shiquan
hostname lan.work.cloud
port 29418
identityfile ~/.ssh/id_rsa_work
KexAlgorithms XXXXXXXXXXXXXXXXXX

#github
Host github.com
User shixiaoquan
HostName github.com
IdentityFile ~/.ssh/id_rsa_github

#coding
Host git.coding.net
User shixioaquan
HostName git.coding.net
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_coding

原理分析(本段摘自here

  1. ssh 客户端是通过类似 git@github.com:githubUserName/repName.git 的地址来识别使用本地的哪个私钥的,地址中的 User 是@前面的git, Host 是@后面的github.com
  2. 如果所有账号的 User 和 Host 都为 git 和 github.com,那么就只能使用一个私钥。所以要对User 和 Host 进行配置,让每个账号使用自己的 Host,每个 Host 的域名做 CNAME 解析到 github.com,如上面配置中的Host second.github.com。
  3. 配置了别名之后,新的地址就是git@second.github.com:githubUserName/repName.git(在添加远程仓库时使用)。
    这样 ssh 在连接时就可以区别不同的账号了。

填写ssh证书内容到相应的repo

~/.ssh 目录下的 id_rsa_work.pub 、id_rsa_github.pub、id_rsa_coding.pub中的内容填写到相应的repo的ssh配置中

完活儿

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • 1. 安装 Github 查看是否安装git: $ git config --global user.name "...
    Albert_Sun阅读 13,701评论 9 163
  • 文章1: Git 多账户配置使用,同时使用github 和 gitoschina http://blog.csdn...
    ouhoo阅读 1,679评论 0 0
  • 问题提出 最近在自己的MAC上面提交Github代码的时候发现居然失败了: 这不是坑爹吗,Github都提交过无数...
    keysaim阅读 14,841评论 0 6
  • 8.12-11.22 每天写800字,今日打卡1/100 最近看很多人在写作,心中痒痒的,于是很希望自己也试试看。...
    泽阳9阅读 560评论 2 15