91

How can I make git push to push not only to origin but also another remote repository?

as git push is only an alias for git push origin, can I alias git push to push to 2 remote repositories at once (with just that one command)?

I’m not looking for a non-git script here but would like to set this up for my local repository in git.

When I tried it with post-push scripts I failed.

Kissaki
  • 8,810
  • 5
  • 40
  • 42

3 Answers3

147

I don't think you can do it just by setting a flag on git, but you can modify a config file that will allow you to push to multiple remote repositories without manually typing them all in (well only typing them in the first time and not after)

In the .git/config file you can add multiple urls to a defined remote:

[remote "all"]
    url=ssh://user@server/repos/g0.git
    url=ssh://user@server/repos/g1.git

If you git push all now you push to all the remote urls.

Matt
  • 74,352
  • 26
  • 153
  • 180
g19fanatic
  • 10,567
  • 6
  • 33
  • 63
  • 1
    The remote “hack” is probably what I was looking for. (It’s not redefining the `git push` -> `git push origin HEAD` alias but does the thing I want it to.) In `.git/config` once can add multiple `url`s to a `remote`. Thanks. – Kissaki Nov 23 '10 at 13:36
  • Something isn't working for me: http://stackoverflow.com/questions/15273040/git-push-all-multiple-repositories-doesnt-work – ripper234 Mar 07 '13 at 13:56
  • What if the two repo has different password for same username (email)? – Shafi Jan 08 '18 at 04:08
  • @MASh You can usually setup ssh keys to solve that issue. – g19fanatic Jan 08 '18 at 17:05
  • That just asks me if I want to create a new branch on the remote with the name I am using for "all". – Latency Aug 12 '20 at 16:38
  • Does this push in parallel or sequentially ? – Ashwin Aug 16 '22 at 20:49
64

No manual editing

You can add multiple URLs to a remote branch (e.g. all) directly from command line by using git config --add remote.xyz.url with different URLs:

git config --add remote.all.url ssh://user@server/repos/g0.git
git config --add remote.all.url ssh://user@server/repos/g1.git

Fully automatic

If you're super lazy and don't want to copy/paste URLs several times, this is for you:

function git-add-push-all() {
  while read -r name url method; do
    git config --add remote.all.url "$url"
  done < <(git remote -v | awk '!/^all/ && /push/')
}

git-add-push-all # from git (sub)directory

A full bashy script is possible (test $name and $method), but awk is sweet and there is love for everyone.

Push

Then you can push to all remote with

git push all

References

kcsquared
  • 5,244
  • 1
  • 11
  • 36
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
  • Thank you for this answer. I just set this up and it is probably worth noting that I needed to leave out the ssh:// in the urls (I'm using bitbucket and bluehost) so my git config command looked like this: git config --add remote.all.url user@server/repos/g0.git – MatthewLee Apr 26 '14 at 20:07
  • 1
    additional info: the first `git config -add` will create a fetch & push remote, while the subsequent `git config -add` will only create a push remote. – Paul Verschoor May 22 '14 at 12:15
  • I'm sorry, how Windows users may use your automatic script? Thanks. – Саша Черных Mar 22 '17 at 18:35
  • @СашаЧерных try [Bash on Windows](https://msdn.microsoft.com/en-us/commandline/wsl/about) – Édouard Lopez Mar 23 '17 at 15:48
  • @ÉdouardLopez: I have Windows 10 LTSB, at the moment Bash on Windows [**don't support LTSB**](https://github.com/Microsoft/BashOnWindows/issues/1281). Thanks. – Саша Черных Mar 23 '17 at 15:53
0

You can also get url from configured remotes :

for repo in g0 g1 ...
do
    git config --add remote.all.url `git config remote.$repo.url`
done

where g0, g1, ... are the names of your remotes.

Pierre-Olivier Vares
  • 1,687
  • 15
  • 20