I have a repository with two remotes... one on GitHub (public) and one on a private server. On GitHub I want the branch to always be available and up-to-date master
. On my private server I want to work in peace of mind that all my branches are always up to date and master
(the work on my branches is temporary and I don't want it to be visible on GitHub).
How can I make git push
it upload to both remotes when I run if I'm in master
or only to my private repository if I'm in another branch?
Unfortunately,
git push
you're always going to push to a single remote... but, as usual, you can cheat.This solution works for MacOS and Linux. If someone adapts it to Windows, welcome :)
I'm assuming it
origin
points to the private server and itgithub
points to... yes, GitHub.The idea is to use git aliases . An alias is a command that we define and that can be a call to another command
git
or a call toshell
.An alias cannot override an existing command, so you can make it
git pushear
dogit papafrita
what you want, like this:This is just code
shell
that does:and works! the problem is that you have to remember to do
git papafrita
it every time you want to do itgit push
. If you wanted to usegit push
and not another command, then the trick (I got the idea from this answer ) is to replacegit
with a modified version ofgit
... what I did was move the executablegit
and call itgitreal
, and make a scriptgit.sh
with this content:What this does is: every time it receives
git <comando>
, it checks if there is an alias namedcomandoalias
. If there is, invokegitreal comandoalias
. If not, callgitreal comando
.This trap is dangerous... but I find it very practical... finally, my alias is:
The first
if
is checking to see if it's agit push
or agit push -f
... if not (for example, in the case of agit push origin
), don't overwrite anything you're doing.Another benefit of this solution is that aliases can be per repository... so, you can have this behavior in some repositories or the
git push
default in another.You can make two commands, one for each remote:
Note that I'm assuming the GitHub remote is called
github
and the private remote is calledprivado
. You can substitute these names as appropriate.