Managing multiple remotes with Git is a pretty common thing, I would say. However, I never had the need to do it, until today. I think it’s a totally awesome thing that you can change code locally and push it to different remotes with one command and every one is happy.
Since my particular requirement was to deal with Heroku and Github in the same repository, that’s the context I will be talking about in this post. So, let’s say you have a Heroku app. With the Heroku CLI installed, it’s pretty straightforward to look at the remote information on your repo.
git remote -v
That will give you something like this :-
origin email@example.com:YOUR_HEROKU_APP.git (fetch) origin firstname.lastname@example.org:YOUR_HEROKU_APP.git (push)
Now, since I wanted to use “origin” for the github repo, I decided to rename heroku’s origin to “heroku” – which makes sense I think.
git remote rename origin heroku
So, that now allows me to add github’s repo as origin
git remote add origin email@example.com:YOUR_GITHUB_REPO.git
And that’s it.
Now, I can push to heroku with
git push heroku master
and push to github with
git push origin master
I find that totally awesome!