Git: Pushing to two repos in one command

Git

Git Problem Overview


I want to do git push origin and git push my_other_remote in the same line. Possible?

Git Solutions


Solution 1 - Git

You can get the same effect by adding an extra push URL for your origin remote. For example, if the URLs of your existing remotes are as follows:

$ git remote -v
origin  me@original:something.git (fetch)
origin  me@original:something.git (push)
my_other_remote git://somewhere/something.git (fetch)
my_other_remote git://somewhere/something.git (push)

You could do:

 git remote set-url --add --push origin git://somewhere/something.git

Then, git push origin will push to both repositories. You might want to set up a new remote called both for this, however, to avoid confusion. For example:

 git remote add both me@original:something.git
 git remote set-url --add --push both me@original:something.git
 git remote set-url --add --push both git://somewhere/something.git

... then:

 git push both

... will try to push to both repositories.

Solution 2 - Git

You can put the following in the .git/config file:

[remote "both"]
    url = url/to/first/remote
    url = url/to/other/remote

You can now push to both urls using git push both.

If you also want to fetch from them (useful for sync) you may add the following lines in your .git/config file:

[remotes]
    both = origin, other

Now you can also run git fetch both.

Solution 3 - Git

Just a small addition to the excellent answers provided already - if you dont want to bother with adding "both" and just want to have all operations pushed to both repos automatically, just add your second repo as another url under origin in the git config.

[remote "origin"]
    url = someUrl.git
    url = secondUrl.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

Now "git push" uses both.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRam RachumView Question on Stackoverflow
Solution 1 - GitMark LongairView Answer on Stackoverflow
Solution 2 - GitOlivier VerdierView Answer on Stackoverflow
Solution 3 - GitAmc_rttyView Answer on Stackoverflow