Skip to content

Instantly share code, notes, and snippets.

@suziewong
Last active February 19, 2023 05:38
Star You must be signed in to star a gist
Save suziewong/4378434 to your computer and use it in GitHub Desktop.
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放到公共服务器上。

@demon-zhonglin
Copy link

同一台电脑有2个github账号的 SSH 方案:https://www.sulinehk.com/post/github-multiple-account-setttings/

@yuelfei
Copy link

yuelfei commented Mar 20, 2022

同一台电脑有2个github账号的 SSH 方案:https://www.sulinehk.com/post/github-multiple-account-setttings/

对了。

@makemoremoney80
Copy link

gid:TKCbqTyCtBKXiPW3tt5YfZ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment