Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git的多账号如何处理? 1.同一台电脑多个git(不同网站的)账号 2.同一台电脑多个git(同一个网站的比如github的)多个账号

1.同一台电脑可以有2个git账号(不同网站的)

首先不同网站,当然可以使用同一个邮箱,比如我的github,gitlab,bitbucket的账号都是monkeysuzie[at]gmail.com 这时候不用担心密钥的问题,因为这些网站push pull 认证的唯一性的是邮箱 比如我的windows 上 2个账号一个gitlab 一个github (用的都是id_rsa)

host github
  hostname github.com
  Port 22
host gitlab.zjut.com
  hostname gitlab.zjut.com
  Port 65095

不需要指定key的位置。因为默认读取id_rsa了

这样子使用起来没有任何区别,remote 也想平时一样操作即可。因为邮箱是相同的。

2.同一台电脑有2个github账号?咋办

比如我服务器上模拟的2个用户

#monkeysuzie@gmail.com   我在gitlab的第一个账号suzie
host gitlab.zjut.com
    hostname gitlab.zjut.com
    Port 65095
    User suzie
    IdentityFile /home/suzie/.ssh/id_rsa
#  我在gitlab的第2个账号test
host gitlab-test.zjut.com
    hostname gitlab.zjut.com
    Port 65095
    User test
    IdentityFile /home/suzie/.ssh/id_rsa_second
#837368104@qq.com 我在github的账号 
host github-osteach.com
    hostname github.com
    Port 22
    User osteach
    IdentityFile /home/suzie/.ssh/id_rsa_second

这种情况下,需要几点注意

1.remote pull push的时候有问题,因为要设置邮箱问题了 pull的时候识别的是邮箱,2个github账号,2个邮箱,我们自然不能使用global的user.email了

1.取消global
git config --global --unset user.name
git config --global --unset user.email

2.设置每个项目repo的自己的user.email
git config  user.email "xxxx@xx.com"
git config  user.name "suzie"

之后push pull就木有问题了

备注

生成ssh key

ssh-keygen -m rsa -C "your mail" (当前目录) 然后可以命名默认id_rsa 或者id_rsa_second 把对应的pub放到公共服务器上。

@zarzen

This comment has been minimized.

Copy link

@zarzen zarzen commented Jan 26, 2013

good job

@jiyinyiyong

This comment has been minimized.

Copy link

@jiyinyiyong jiyinyiyong commented Nov 8, 2013

我的 Google 搜索结果第一条...

@tonyc726

This comment has been minimized.

Copy link

@tonyc726 tonyc726 commented Jan 7, 2014

@suziewong 我也按照你的经行了设置

# ~/.gitconfig

host = test-github
    hostname = github.com
    Port = 666333
    identityfile = ~/.ssh/github-shell

但是使用

ssh -T  test-github

会报错,不知道你有没有遇到过这种问题?

@iqiancheng

This comment has been minimized.

Copy link

@iqiancheng iqiancheng commented Apr 8, 2015

使用

ssh -vT git@github.com 

可以查看ssh 连接主机的debug信息。

@ladder1984

This comment has been minimized.

Copy link

@ladder1984 ladder1984 commented Aug 4, 2015

呃,这个文件放在哪里?

@hzchenyh

This comment has been minimized.

Copy link

@hzchenyh hzchenyh commented Feb 25, 2016

看来还是工大的校友嘛 哈哈

@huguangju

This comment has been minimized.

Copy link

@huguangju huguangju commented Mar 14, 2016

+1

@hzxd

This comment has been minimized.

Copy link

@hzxd hzxd commented Mar 21, 2016

现在还是google第一

@qiuchengjia

This comment has been minimized.

Copy link

@qiuchengjia qiuchengjia commented Jun 27, 2016

我这样不行啊

@ChrisCindy

This comment has been minimized.

Copy link

@ChrisCindy ChrisCindy commented Jun 30, 2016

mac上的文件地址是/etc/ssh/ssh_config

@cxiaoer

This comment has been minimized.

Copy link

@cxiaoer cxiaoer commented Jul 27, 2016

多谢了

@Mark-Han

This comment has been minimized.

Copy link

@Mark-Han Mark-Han commented Oct 30, 2016

It works.

@iewgnahz

This comment has been minimized.

Copy link

@iewgnahz iewgnahz commented Dec 4, 2016

为什么我测试的结果两次用户名都是相同的都是:hi zhangwei

@TabSh

This comment has been minimized.

Copy link

@TabSh TabSh commented Dec 28, 2016

使用global的用户一样是可以的,前提是在目标里添加了ssh

@Irischen666

This comment has been minimized.

Copy link

@Irischen666 Irischen666 commented Jun 16, 2017

我按照你的方法尝试了,还是提示permission denied 。于是,到mac的钥匙串里面,把所有跟git 相关的密码删除。重新push的时候,会提示输入用户名和邮箱密码,输入之后就好了。

@dousuguang

This comment has been minimized.

Copy link

@dousuguang dousuguang commented Jun 22, 2017

我在mac上切换github账户时,当目标是http地址时,也需要删除keychain access里面和github有关的密码,如果把目标换成ssh地址,并且设置好ssh钥匙,应该就会自动登陆了,等会儿试验了再来汇报。

@r888800009

This comment has been minimized.

Copy link

@r888800009 r888800009 commented Mar 17, 2018

如果使用windows版本的git,則修改到這個檔案。
C:\Program Files\Git\etc\ssh\ssh_config
如果在安裝的時候,有修改過安裝路徑則自行調整至git的安裝路徑。

註:windows版本的git安裝目錄類似linux上的根目錄,透過修改ssh_config達成配置的效果。

@XuQK

This comment has been minimized.

Copy link

@XuQK XuQK commented Apr 29, 2018

你好,我没有配置'$HOME/.ssh/config`文件,只是为不同的Repo单独指定了email和user,也可以分别向公司的gitlab和github.com正常push和pull,请问知道这是为什么吗?

@zxyvirgo

This comment has been minimized.

Copy link

@zxyvirgo zxyvirgo commented May 25, 2018

 虽然添加好了,但是push 的时候还是提示无权限

@lichao2018

This comment has been minimized.

Copy link

@lichao2018 lichao2018 commented Jun 12, 2018

可以可以。我的情况是一个电脑两个git账号,两个git账号使用相同的邮箱,设置步骤是:
1.在.ssh中新建config文件,config文件内容:

Host github
  hostname github.com
  User lichao2018
  IdentityFiLE ~/.ssh/github_rsa
  
Host code.aliyun.com
  hostname code.aliyun.com
  User lichao2018
  IdentityFiLE ~/.ssh/github_rsa

2.将github_rsa中内容复制到阿里云的ssh key中。
然后两个网站都可以clone和push了。

@edward12699

This comment has been minimized.

Copy link

@edward12699 edward12699 commented Dec 6, 2018

emote pull push的时候有问题,因为要设置邮箱问题了 pull的时候识别的是邮箱,2个github账号,2个邮箱,我们自然不能使用global的user.email了

这里感觉不对,我之前错误操作的时候,用的全局的另一个邮箱,也是能push/pull的

@CubeSugarZhang

This comment has been minimized.

Copy link

@CubeSugarZhang CubeSugarZhang commented Oct 28, 2019

为啥我这边在github上面新建了个仓库,github上面配置的公钥(生成时直接回车),本地克隆这个仓库代码时(ssh方式)总让我输入密码。。。

@kska32

This comment has been minimized.

Copy link

@kska32 kska32 commented Feb 16, 2020

没用,切换账户登陆时还是得删除windows凭据中的GITHUB账户信息, WINDOWS10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.