How to push to multiple remotes from multiple branches individually at once?

Viewed 80

So, the situation is like this: Say I have 3 branches in my local git repo: master, dev, staging.

Now, I have different servers for each branch that I want to push to. For example I have set prodS (prodServer) as remote to master and have it mapped to master(git push -set--upstream prodS master). So that git push pushes master to prodS when I have checkedout master.

Similarly, devS for dev branch, stagingS for staging branch

When my dev and staging branches are ready for push, (Regardless of which branch I have currently checkedout,) I want to be able to run a single command that pushes dev to devS and staging to stagingS; instead of having to switch to each branch and then run git push or run git push devS dev and so forth for each branch since I already mapped dev to devS/dev.

I'm aware that multiple URLs can be added to a remote. But that is not what I'm looking for. Say there are already multiple remotes added to each of the branches. But I added them to a particular branch because I want only that particular branch on those remote/s. That's already ok and working.

Is there such an option in git itself? Or the only option here maybe some other workaround like a script?

EDIT: To simpify, and make it easier to understand the question, If git pull --all pulls from all remote branches tracked by all local branches; shouldn't git push --all work in similar way?

1 Answers

I don't think there is an option to specify multiple branches for multiples remotes in a single push, but I think there are a couple alternatives that might be useful.

1. Multiple push urls for a single remote:

If you are fine with having your dev branch on stagingS and your staging branch on devS then you can define a new remote <name> and set the urls for both devS and stagingS as push urls for this new remote:

$ git remote set-url --add --push <name> <url-for-devS>
$ git remote set-url --add --push <name> <url-for-stagingS>

Now you can push both dev and staging to both remotes with git push <name> dev staging

2. Aliases

You probably know this already, but you can set a new command on your .git/config file (or even ~/.gitconfig if this behaviour is something you need over multiple projects) under the alias section and you will be able to use it as a git command, even using normal flags for the push command like -v and -f.

[alias]
    my-command = !git push devS dev $* && git push stagingS staging $*

Now you can run git my-command -f, for example.

Related