2

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.

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

Joy
  • 66
  • 5
  • You can specify the remote and the branch on git push without having to have that branch checked out – Steve Jun 02 '22 at 05:22
  • Does this answer your question? [How can I pull/push from multiple remote locations?](https://stackoverflow.com/questions/849308/how-can-i-pull-push-from-multiple-remote-locations) – Steve Jun 02 '22 at 05:25
  • @Steve No I don't want that as mentioned in my question, and No. I know it's easy to confuse it with the question you mentioned. But it's a completely different scenario. I know how to add and use multiple remotes to a particular branch. My question is: after doing above on different branches, how to push each branch to it's respective remote/s at once. – Joy Jun 02 '22 at 07:50
  • [Pedro Sousa's alias suggestion](https://stackoverflow.com/a/72474379/1256452), or a shell alias that invokes multiple `git push` commands, is going to be the way to go. Remember that the syntax for `git push` is `git push ...` so you can list the refspec you want pushed and not use `HEAD` and you won't be referring to your current branch. – torek Jun 02 '22 at 14:15

1 Answers1

2

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.