多个Git账号之间切换

技术文档网 2021-05-06

简介


多个Git账号有两种情况

  1. 多个Github账号,不同的账号对应不同的repository,需要push的时候自动区分账号
  2. 多个Git的账号,有的是GitHub的,有的是GitLab的等等,不同的账号对应不同的repository,需要push的时候自动区分账号

ssh key(完整流程可用)


假设我有两个账号,一个是GitHub的,一个是GitLab的,先生成不同的ssh-key

#如果没有指定 -f 选项或者 -f 选项为空,默认生成的私钥和公钥为: id_rsa, id_rsa.pub.

# ~/.ssh/id_rsa_github和~/.ssh/id_rsa_github.pub两个文件
ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "邮箱地址A"

# ~/.ssh/id_rsa_gitlib和~/.ssh/id_rsa_gitlab.pub两个文件
ssh-keygen -t rsa -f ~/.ssh/id_rsa_gitlib -C "邮箱地址B"

$ vim ~/.ssh/config
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github

Host gitlab.alibaba-inc.com
    HostName gitlab.alibaba-inc.com
    Port 22
    User git #表示git协议git@xxx
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitlib

#使用ssh git@github.com和ssh git@gitlab.alibaba-inc.com查看是否正确配置了
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

#分别进入各自的repository,进行git config user.name 和 user.email设置
$ cd xsearch
$ git config user.name "AAA"
$ git config user.email "邮箱地址A"

$ cd rule-preview
$ git config user.name "BBB"
$ git config user.email "邮箱地址B"

账号区分


ssh靠的是Host进行区分账号

$ vim ~/.ssh/config
Host A.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github_A

Host B.github.com
    HostName github.com
    Port 22
    User git #表示git协议git@xxx
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github_B

多个账号只需要修改repository中的ssh url即可,如:

$ vim xsearch/.git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@github.com:tbwuming/xsearch.git      #修改为git@A.github.com:tbwuming/xsearch.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[color]
        ui = true
[user]
        name = tbwuming
        email = 邮箱地址A
~

一个repository,同时push到不同的仓库该如何设置?


直接更改 xsearch/.git/config 里面的url即可,把里面对应tag下的url增加一个就可以了。例:

[remote "GitHub"]
    url = git@github.com:elliottcable/Paws.o.git
    fetch = +refs/heads/*:refs/remotes/GitHub/*
[branch "Master"]
    remote = GitHub
    merge = refs/heads/Master
[remote "Codaset"]
    url = git@codaset.com:elliottcable/paws-o.git
    fetch = +refs/heads/*:refs/remotes/Codaset/*
[remote "Paws"]
    url = git@github.com:Paws/Paws.o.git
    fetch = +refs/heads/*:refs/remotes/Paws/*
[remote "Origin"]
    url = git@github.com:Paws/Paws.o.git
    url = git@codaset.com:elliottcable/paws-o.git

上面这个立即就是有4个远端仓库,不同的tag表示不同的远端仓库,最后的Origin标签写法表示默认push到github和codaset这两个远端仓库去。当然,你可以自己随意定制tag和url

参考文档


相关文章

  1. 代码无法更新问题处理办法

    在本地维护代码时,经常要git pull一下以便拉取最新的代码 但是,有时候改了本地的文件,git pull的时候提示错误 可以酱紫操作: 强制覆盖本地文件(改完可能会导致本地配置信息被覆盖) git

  2. Git换行符检查CRLF与LF

    遇到的问题 在 git 提交或是签出时,提示如下问题:[git] warning: LF will be replaced by CRLF | fatal: CRLF would be replace

  3. 使用git和GitHub完成考核任务

    Git 是自由开源的分布式版本控制系统。 GitHub 是使用 Git 进行版本控制的代码托管平台。 准备工作 你需要在系统上安装 git 来管理工作区。 Ubuntu $ sudo apt up

  4. 如何防止新Feature把线上搞挂?

    除非恶意代码,比如说在一个死循环中不停的分配内存,否则服务不是那么容易挂掉的。 开发一个新的Feature通常会经历如下流程进行把关:1、有单元测试、自动化测试和性能测试来保证代码质量。2、每个大

  5. 如何更新自己Fork的代码

    以用户appframe为例子: 注意事项:在更新自己Fork的代码之前,需要先把自己在本地的更改进行提交。 1、检出自己在github上fork的APDPlat分支(如果已经从netbenas中检出了

随机推荐

  1. 代码无法更新问题处理办法

    在本地维护代码时,经常要git pull一下以便拉取最新的代码 但是,有时候改了本地的文件,git pull的时候提示错误 可以酱紫操作: 强制覆盖本地文件(改完可能会导致本地配置信息被覆盖) git

  2. Git换行符检查CRLF与LF

    遇到的问题 在 git 提交或是签出时,提示如下问题:[git] warning: LF will be replaced by CRLF | fatal: CRLF would be replace

  3. 使用git和GitHub完成考核任务

    Git 是自由开源的分布式版本控制系统。 GitHub 是使用 Git 进行版本控制的代码托管平台。 准备工作 你需要在系统上安装 git 来管理工作区。 Ubuntu $ sudo apt up

  4. 如何防止新Feature把线上搞挂?

    除非恶意代码,比如说在一个死循环中不停的分配内存,否则服务不是那么容易挂掉的。 开发一个新的Feature通常会经历如下流程进行把关:1、有单元测试、自动化测试和性能测试来保证代码质量。2、每个大

  5. 如何更新自己Fork的代码

    以用户appframe为例子: 注意事项:在更新自己Fork的代码之前,需要先把自己在本地的更改进行提交。 1、检出自己在github上fork的APDPlat分支(如果已经从netbenas中检出了