Skip to content

Instantly share code, notes, and snippets.

@lhlyu
Last active December 13, 2023 09:54
Show Gist options
  • Save lhlyu/557b7daf59c3c3741b2bf0625993a2ea to your computer and use it in GitHub Desktop.
Save lhlyu/557b7daf59c3c3741b2bf0625993a2ea to your computer and use it in GitHub Desktop.
多个仓库配置git,根据不同仓库切换指定账号
  1. 需要准备两个仓库,我这里使用githubgitlab
  2. 下面分别是两个仓库的不同的账号信息
  • github
user.name=xxx
user.email=xxx@xx.com
  • gitlab
user.name=yyy
user.email=yyy@yy.com
  1. 生成私钥和公钥,下面是生成的命令行,生成的私钥和公钥,我放在了~/.ssh文件夹下面

ssh-keygen -t rsa -b 4096 -C "注释"

  1. 将生成的公钥(文件后缀是.pub)分别在github和gitlab的SSH密钥中设置好

  2. ~/.ssh文件夹下面新增一个config文件,配置如下:

Host Github
HostName github.com
PreferredAuthentications publickey
User xxx@xx.com
IdentityFile ~/.ssh/id_rsa
AddKeysToAgent yes
UseKeychain yes

Host Gitlab
HostName gitlab.com
PreferredAuthentications publickey
User yyy@yy.com
IdentityFile ~/.ssh/id_rsa
AddKeysToAgent yes
UseKeychain yes
  1. 创建三个.gitconfig文件,内容如下:
  • ~/.gitconfig
[user]
    name = "xxx"
    email = "xxx@xx.com"

[includeIf "hasconfig:remote.*.url:git@github.com:**/**"]
    path = ".gitconfig-github"

[includeIf "hasconfig:remote.*.url:git@gitlab.com:**/**"]
    path = ".gitconfig-gitlab"
  • ~/.gitconfig-github
[user]
    name = "xxx"
    email = "xxx@xx.com"
  • ~/.gitconfig-gitlab
[user]
    name = "yyy"
    email = "yyy@yy.com"
  1. 把密钥添加到ssh-agent的高速缓存中,命令行如下:

ssh-add ~/.ssh/id_rsa

  1. 这样就行了
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment