赞
踩
在使用Git管理代码时,很大可能会存在两个(或多个)账号,公司(一般gitlab),个人(github或其他的),这时候就要同时操作两个不同的账号。
官方文档:通过 SSH 连接到 GitHub - GitHub 文档
第一个帐号和地址:first_name / gitlab.com
第一个帐号和地址:seconde_name / github.com
- // 最好以公司的账号地址为准,尽量避免操作公司项目代码时,一个人出现两个不同的账号名称
- // gitlab/github的帐号名(也可以自定义)
- git config --global user.name "diy_name"
-
- // gitlab/github的邮箱地址(也可以自定义)
- git config --global user.email "email_adress"
1)操作第一个账号(gitlab.com)生产 ssh 密钥
// 一定是gitlab.com网站上的邮箱地址,不可以自定义
① ssh-keygen -t rsa -C "gitlab_email_adress"② 一路回车
③ 在gitlab.com网站上添加公钥,即~\.ssh\id_rsa.pub中的所有内容
2)操作第二个账号(github.com)生产 ssh 密钥
// 一定是gitlab.com网站上的邮箱地址,不可以自定义
① ssh-keygen -t rsa -C "github_email_adress"② 回车后提示设置私钥文件的文件名,我们输入id_rsa_github即可,默认保存在和第一个帐号私钥文件同级目录下,可以自定义路径。
③ 一路回车
④ 新密钥添加到 SSH agent 中:
ssh-agent bash
ssh-add ~/.ssh/id_rsa_github (第二个帐号私钥文件的完整路径)
⑤ 新生成的id_rsa_github.pub文件中存放着第二个帐号的公钥,将全部内容复制,添加到gitlab.com网站上
- # gitlab user
-
- Host gitlab //主机名,可自定义
- HostName gitlab.com //代码托管网站域名
- User git
- IdentityFile ~/.ssh/id_rsa//此帐号私钥的路径
-
- # github user
-
- Host github
- HostName github.com
- User git
- IdentityFile ~/.ssh/id_rsa_github
// gitlab是config文件中我们定义的Host的值,一般我们测试连通性时要输入:ssh -T git@gitlab.com 需要网站的域名,在配置之后直接使用主机名即Host的值代替网站名
- ssh -T gitlab
-
- ssh -T github
-
- // 返回这个表示正常
- Welcome to GitLab, @username!
- //或者返回这个也表示正常
- Hi username! You've successfully authenticated, but GitHub does not provide shell access.
- 之前的命令:git clone git@gitlab.com:first_name/demo_proj.git
-
- 现在的命令:git clone gitlab:first_name/demo_proj.git
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。