zoukankan      html  css  js  c++  java
  • 怎么在多场景下使用不同的 git 账号 commit

    应用场景

    • 我有多个github的账号,不同的账号对应不同的repo,需要push的时候自动区分账号
    • 我有多个git的账号,有的是github的,有的是单位的gitlab的,不同账号对应不同的repo,需要push的时候自动区分账号

    解决办法

    • 先假设我有两个账号,一个是github上的,一个是公司gitlab上面的。先为不同的账号生成不同的ssh-key

      ssh-keygen -t rsa -f ~/.ssh/id_rsa_work -c xxx@gmail.com

      然后根据提示连续回车即可在~/.ssh目录下得到id_rsa_work和id_rsa_work.pub两个文件,id_rsa_work.pub文件里存放的就是我们要使用的key

      ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -c xxx@gmail.com

      然后根据提示连续回车即可在~/.ssh目录下得到id_rsa_github和id_rsa_github.pub两个文件,id_rsa_gthub.pub文件里存放的就是我们要使用的key

    • 把id_rsa_xxx.pub中的key添加到github或gitlab上,这一步在github或gitlab上都有帮助,不再赘述

    • 编辑 ~/.ssh/config,设定不同的git 服务器对应不同的key

     
    # Default github user(first@mail.com),注意User项直接填git,不用填在github的用户名
    Host github.com
     HostName github.com
     User git
     IdentityFile ~/.ssh/id_rsa_github
    
    # second user(second@mail.com)
    # 建一个gitlab别名,新建的帐号使用这个别名做克隆和更新
    Host 172.16.11.11
     HostName 172.16.11.11
     User work
     IdentityFile ~/.ssh/id_rsa_work

    这样每次push的时候系统就会根据不同的仓库地址使用不同的账号提交了

    延伸问题

    • 我有一个repo,想要同时push到不同的仓库该如何设置?

      直接更改 repo/.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

    http://stackoverflow.com/questions/7438313/pushing-to-git-returning-error-code-403-fatal-http-request-failed 

    http://stackoverflow.com/questions/849308/pull-push-from-multiple-remote-locations/3195446#3195446

    http://happy123.me/blog/2014/12/07/duo-ge-gitzhang-hao-zhi-jian-de-qie-huan/

  • 相关阅读:
    JVM—Java内存结构
    myeclipse中页面utf-8的设置
    Python-统计txt文件里面的分数
    Python-字符串常用函数
    初识C#扩展方法
    Artwork 18年中南多校第一场A
    HDU2586 How far away ?
    HDU5952 Counting Cliques 暴搜优化
    POJ 2763 Housewife Wind 树链拋分
    HDU 3966 Aragorn's Story 树链拋分
  • 原文地址:https://www.cnblogs.com/kuangliu/p/8485041.html
Copyright © 2011-2022 走看看