Git push to multiple remotes
by Patrick Connelly posted on May 02, 2016
With today’s PaSS offerings, you may find yourself in a position like I was. Let’s take the scenario of hosting an application on Openshift, but also wanting to store that code on Github. Historically, anytime you wanted to do a git push to both Openshift and Github, you’d have to submit two push commands. Now, you could write your own git alias to do that for you, but I recently discovered a better way to do this. (This also works if you are using Heroku)
Example Layout
Let’s take the following layout as what our PaSS and git structure looks like
$ git remote -v
github https://github.com/OWNER/REPOSITORY.git (fetch)
github https://github.com/OWNER/REPOSITORY.git (push)
origin ssh://abc123@APP-OWNER.rhcloud.com/~/git/APP.git/ (fetch)
origin ssh://abc123@APP-OWNER.rhcloud.com/~/git/APP.git/ (push)
Current git workflow
- Make change in git
- git push to openshift (origin remote)
- git push to github (github remote)
- Repeat
Where this workflow breaks down is if you forget to push to github and then try to update openshift on a second host that has pulled from github but not from openshift. While it may seem as simple as “don’t forget to do it,” I’ve found that I’m pretty forgetful. And in the case of this, you have two systems of record.
Ideal git workflow
- Make change in git
- git push (to openshift and github)
- Repeat
The ideal workflow would be to just run a single git push command and have the code land in both github and openshift. Well, we can!
Adding additional remotes to git push
The first step we will want to do is to clean up our remotes. You goal is to have just the origin remote and have that point to Github. If you’re not familiar with removing remotes, read over this article. So what we want to see is
$ git remote -v
origin https://github.com/OWNER/REPOSITORY.git (fetch)
origin https://github.com/OWNER/REPOSITORY.git (push)
Next we want to add the Openshift repository as a push url for our remote. As stated in this Stack Overflow article, we need to set both urls.
$ git remote set-url --add --push origin https://github.com/OWNER/REPOSITORY.git
$ git remote set-url --add --push origin ssh://abc123@APP-OWNER.rhcloud.com/~/git/APP.git/
Now if we inspect our remotes, we can see that we now have two push targets
$ git remote -v
origin https://github.com/OWNER/REPOSITORY.git (fetch)
origin https://github.com/OWNER/REPOSITORY.git (push)
origin ssh://abc123@APP-OWNER.rhcloud.com/~/git/APP.git/ (push)
Now anytime we do a git push, the code will be deployed to both Github and Openshift.
Notes
- When setting this up for the first time, make sure that both the Github and the Openshift repositories share the same base. ie, spin up the Openshift app then create the Github repo from it, or create the Github repo and spin up the Openshift app based on it.