Pushing code to two remotes

This question already has an answer here:

  • pull/push from multiple remote locations 14 answers

  • In recent versions of Git you can add multiple pushurl s for a given remote. Use the following to add two pushurl s to your origin :

    git remote set-url --add --push origin git://original/repo.git
    git remote set-url --add --push origin git://another/repo.git
    

    So when you push to origin , it will push to both repositories.

    UPDATE 1 : Git 1.8.0.1 and 1.8.1 (and possibly other versions) seem to have a bug that causes --add to replace the original URL the first time you use it, so you need to re-add the original URL using the same command. Doing git remote -v should reveal the current URLs for each remote.

    UPDATE 2: Junio C. Hamano, the Git maintainer, explained it's how it was designed. Doing git remote set-url --add --push <remote_name> <url> adds a pushurl for a given remote, which overrides the default URL for pushes. However, you may add multiple pushurl s for a given remote, which then allows you to push to multiple remotes using a single git push . You can verify this behavior below:

    $ git clone git://original/repo.git
    $ git remote -v
    origin  git://original/repo.git (fetch)
    origin  git://original/repo.git (push)
    $ git config -l | grep '^remote.'
    remote.origin.url=git://original/repo.git
    remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
    

    Now, if you want to push to two or more repositories using a single command, you may create a new remote named all (as suggested by @Adam Nelson in comments), or keep using the origin , though the latter name is less descriptive for this purpose. If you still want to use origin , skip the following step, and use origin instead of all in all other steps.

    So let's add a new remote called all that we'll reference later when pushing to multiple repositories:

    $ git remote add all git://original/repo.git
    $ git remote -v
    all git://original/repo.git (fetch)               <-- ADDED
    all git://original/repo.git (push)                <-- ADDED
    origin  git://original/repo.git (fetch)
    origin  git://original/repo.git (push)
    $ git config -l | grep '^remote.all'
    remote.all.url=git://original/repo.git            <-- ADDED
    remote.all.fetch=+refs/heads/*:refs/remotes/all/* <-- ADDED
    

    Then let's add a pushurl to the all remote, pointing to another repository:

    $ git remote set-url --add --push all git://another/repo.git
    $ git remote -v
    all git://original/repo.git (fetch)
    all git://another/repo.git (push)                 <-- CHANGED
    origin  git://original/repo.git (fetch)
    origin  git://original/repo.git (push)
    $ git config -l | grep '^remote.all'
    remote.all.url=git://original/repo.git
    remote.all.fetch=+refs/heads/*:refs/remotes/all/*
    remote.all.pushurl=git://another/repo.git         <-- ADDED
    

    Here git remote -v shows the new pushurl for push, so if you do git push all master , it will push the master branch to git://another/repo.git only. This shows how pushurl overrides the default url (remote.all.url).

    Now let's add another pushurl pointing to the original repository:

    $ git remote set-url --add --push all git://original/repo.git
    $ git remote -v
    all git://original/repo.git (fetch)
    all git://another/repo.git (push)
    all git://original/repo.git (push)                <-- ADDED
    origin  git://original/repo.git (fetch)
    origin  git://original/repo.git (push)
    $ git config -l | grep '^remote.all'
    remote.all.url=git://original/repo.git
    remote.all.fetch=+refs/heads/*:refs/remotes/all/*
    remote.all.pushurl=git://another/repo.git
    remote.all.pushurl=git://original/repo.git        <-- ADDED
    

    You see both pushurl s we added are kept. Now a single git push all master will push the master branch to both git://another/repo.git and git://original/repo.git .


    To send to both remote with one command, you can create a alias for it:

    git config alias.pushall '!git push origin devel && git push github devel'
    

    With this, when you use the command git pushall , it will update both repositories.

    链接地址: http://www.djcxy.com/p/8766.html

    上一篇: git别名:多个命令,可变参数

    下一篇: 将代码推送到两个遥控器