search

Home  >  Q&A  >  body text

终端下如何配置 git 使其可以同时 push 到两个远程仓库?

[remote "origin"]
        url = git@github.com:SegmentFault/XXX.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[remote "dev"]
        url = git@gitlab.com:root/XXX.git
        fetch = +refs/heads/*:refs/remotes/dev/*

config 中配置了两个远程仓库。我想终端下一句 git push 同时把代码提交到两个仓库,该怎么做?

迷茫迷茫2769 days ago664

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-27 09:05:02

    I have used two methods. The first was to set multiple remotes and then write an alias, such as:

    $ git config alias.pushall "!git push origin && git push dev"
    

    Later, after a certain version (forgot the specific version number) was upgraded, Git added an additional setting that allows you to set multiple pushurls for a remote. For example, in the example in your question, I can leave out the remote "dev",只留下 remote "origin" and add:

    git remote --set-url --add --push origin git@gitlab.com:root/XXX.git
    

    After this, your remote "origin" will become a structure similar to the following:

    [remote "origin"]
            url = git@github.com:SegmentFault/XXX.git
            fetch = +refs/heads/*:refs/remotes/origin/*
            pushurl = git@github.com:SegmentFault/XXX.git
            pushurl = git@gitlab.com:root/XXX.git
    

    In this way, I can directly git push origin push two repos.

    These two methods actually have their own applicable scenarios, so make your own choice.

    reply
    0
  • 世界只因有你

    世界只因有你2017-04-27 09:05:02

    Since there is a risk of being blocked by a wall, it is a pain in the ass that the two warehouses are out of sync. It’s not too much trouble..
    If you really want to do this, just write a shell file.

    #!/bin/bash
    git push origin master
    git push dev master
    #以上,根据你自己的要求调整一下.
    

    Then don’t forget to make it executable: (assuming the script is called push.sh)

    JerryMac:Dev Jerry$ chmod +x ./push.sh
    JerryMac:Dev Jerry$ ./push.sh
    

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-27 09:05:02

    Answer: http://blog.codepiano.com/2013/07/03/push-multi-remote-repositories/

    The same questions in SegmentFault:
    /q/1010000000367632/a-1020000000369754
    /q/1010000000411859/a-1020000000411882

    reply
    0
  • Cancelreply