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.

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?

Joy
  • 96
  • 7
  • 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

4

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.

  • For solution 1: I have mapped remote/s to particular branches only because i don't want other branches on those remote/s. – Joy Jun 08 '22 at 09:35
  • For solution 2: It seems like a good idea and I will explore it. But I would still prefer if there was some other native git way of doing it. Maybe something as simple as `git push *` ? – Joy Jun 08 '22 at 10:12
  • Maybe something as simple as `git push *` ? This should work since 1st argument after push is supposed to be a remote. So I want to push to all remotes. But if I'm not specifying a branch as 2nd argument, it should push the branch the remote is mapped to. I know this wouldn't work. But just an idea or hint about the kind of solution I need. – Joy Jun 08 '22 at 10:18