当前位置:   article > 正文

同一台电脑怎样配置并使用多个git账号_同一台电脑git账户名

同一台电脑git账户名

一、基础设置

1.生成账号1密钥

ssh-keygen -t rsa -C "one@qq.com"
//接下来回车两次
/*第一次其实是让输生成密钥的名字,生成后放后你git命令所在的目录
效果同2.生成密钥
第二次是让输入密码,因为并不机密所以可以空着直接回车就好
*/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.生成账号2密钥

ssh-keygen -t rsa -C "two@qq.com" -f /c/Users/Administrator/.ssh/id_rsa_two
  • 1

3.会在文件夹(C:\Users\Administrator.ssh)看到对应四个文件:

.pub的是公钥,其它两个是私钥不能泄漏给他人

id_rsa
id_rsa.pub
id_rsa_two
id_rsa_two.pub
  • 1
  • 2
  • 3
  • 4

4.把公钥放到对应的账号上

1.把id_rsa_two.pub加到你的第2个账号上

2.把id_rsa.pub放到你的第1个git账号上

3.在git上右上图标setting—ssh—new ssh key

在这里插入图片描述

5.把该key加到ssh agent上。

1.由于不是使用默认的.ssh/id_rsa,所以你需要显示告诉ssh agent你的新key的位置

ssh-agent bash //如果不能执行以下两句

ssh-add ~/.ssh/id_rsa_tow //linux用这个

ssh-add id_rsa_two //windows用这个

//显示:
//Identity added: id_rsa_two (two@qq.com)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2. 可以通过ssh-add -l来确认结果

6.新建并配置.ssh/config

说明
说明:
# 注释
Host 为域名别名
Hostname:真实地址
User:用户名
IdentityFile:rsa存放地址
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

a) windows实际内容

# 默认github账号
Host github
  HostName github.com
  IdentityFile C:\Users\Administrator\.ssh\id_rsa

# github账号2
Host github_two
  HostName github.com
  IdentityFile C:\Users\Administrator\.ssh\id_rsa_two
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

b) linux实际内容

#默认github账号
Host github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa

# github账号2
Host github_two
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_two
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

7.测试配置是否生效

$ ssh -T git@github
# Hi sky! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@github_two
# Hi sky_two! You've successfully authenticated, but GitHub does not provide shell access.
  • 1
  • 2
  • 3
  • 4
  • 5

8.这样,你就可以通过使用github.com别名github_two

来明确说你要是使用id_rsa_two的SSH key来连接github,即使用工作账号进行操作

命令:git remote add origin git@github_two:xxxx/test.git

#本地建库
$ git init
$ git commit -am "first commit'

#push到github上去
$ git remote add origin git@github_two:xxxx/test.git
$ git push origin master
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

二、进阶:8步也可以如下设置

1.清除git的全局设置(2.3二选一)

使用 git config --list 查看当前配置。
如果你之前在设置本地仓库和github连接的时候设置过user.name和user.email,那么你必须首先清除掉该设置,因为不清除掉该设置,两个账号在提交资料的时候,验证肯定冲突。
只能设置一个全局的user.name和user.email,而你现在有两个账号。
【清除全局】:

$ git config --global --unset user.name
$ git config --global --unset user.email
  • 1
  • 2

2.当前项目设置用户名邮箱(如选此步,3略过)

$ git config user.name 'sky_two'
$ git config user.email 'sky_two@qq.com'

git remote add origin git@github_two:xxxx/test.git

之后操作和一个账号就一样了
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. 设置每个项目repo自己的user.email(ab二选一)

在这里插入图片描述
GitHub提供了两种方法来保护我们的邮箱隐私:使用GitHub专用的替代邮箱

a) 添加隐私邮箱

如果勾选第1项,且不勾选第2项,要push成功,git这样设置:

$ git config user.email "sky_one@gmail.com"
$ git config user.name "sky_one"

$ git config user.email "sky_two@126.com"
$ git config user.name "sky_two"
  • 1
  • 2
  • 3
  • 4
  • 5

git设置的邮箱尽量和github的邮箱保持一致

b) 在推送时发现隐私邮箱则阻止推送

如果勾选第2项,要push成功,要么去掉第一项勾选,要么把修改git设置:

$ git config user.email "sky_one@users.noreply.github.com"
$ git config user.name "sky_one"

$ git config user.email "sky_two@users.noreply.github.com"
$ git config user.name "sky_two"
  • 1
  • 2
  • 3
  • 4
  • 5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/783386
推荐阅读
相关标签
  

闽ICP备14008679号