Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add a Heroku remote to an existing git repo

Working with git remotes on Heroku

Generally, you will add a git remote for your Heroku app during the Heroku app creation process, i.e. heroku create. However, if you are working on an existing app and want to add git remotes to enable manual deploys, the following commands may be useful.

Adding a new remote

Add a remote for your Staging app and deploy

Note that on Heroku, you must always use master as the destination branch on the remote. If you want to deploy a different branch, you can use the syntax local_branch:destination_branch seen below (in this example, we push the local staging branch to the master branch on heroku.

$ git remote add staging
$ git push staging staging:master

In some cases, your local branch may be missing some commits that were already deployed to Heroku, resulting in an error. If you are very sure you want to proceed, add the --force (-f) flag.

$ git push staging staging:master -f

Add a remote for your Production app and deploy

By convention, the remote name "heroku" is typically used for the production application.

$ git remote add heroku
$ git push heroku master

Add a remote via Heroku CLI

As @voke points out, you can alternatively use a Heroku CLI command to add your remote. However, it looks like this will always use the default remote name heroku for the remote. If you would like to use a different name for your remote, see the "Rename a remote" section below.

$ heroku git:remote -a staging-app

Edit: Thanks to @nruth for pointing out you can supply a remote name to this command with the -r flag.

$ heroku git:remote -a staging-app -r staging

Add a remote using the SSH protocol

As @Saworieza points out, all of the examples above use the https protocol for connecting to the remotes, but it is also possible to connect via ssh.

$ git remote add staging
$ git remote add heroku

Other useful commands

List your git remotes

The -v is the flag for "verbose" and includes the remote URL in addition to the remote name.

$ git remote -v

Rename a remote

$ git remote rename heroku staging

Change a remote URL or protocol

If you have already created https remotes and want to switch them to use ssh, the following command can be used. This command can also be used to change the target URL without changing the protocol

$ git remote set-url staging
$ git remote set-url heroku
Copy link

tzvc commented Jun 12, 2018

Thanks for the gist! Quick question tho, when I do that and then git push heroku master nothing is pushed to heroku:master and the build threfore fail, what step I am missing here ?

Copy link

Saworieza commented Dec 13, 2018

Hi. I found that this method is slightly misleading.
git remote add heroku
works much better. doesn't add two apps to your remote and is straightforward

Copy link

voke commented Nov 11, 2019

Another way is to run heroku git:remote app-name

Copy link

randallreedjr commented Nov 22, 2019

@theochampion Sorry, I just saw your question. You probably already figured out the solution, but in case anyone else comes here with the same question, my guess would be that your local branch doesn't have any new commits, so there's nothing to push to Heroku.

Copy link

nruth commented Jan 17, 2020

heroku git:remote -a your-staging-app-name -r staging then it doesn't overwrite heroku

Copy link

randallreedjr commented Jan 17, 2020

@nruth nice tip, thank you!

Copy link

levous commented Feb 29, 2020

Dude, thanks for this. Super helpful

Copy link

Joxebus commented Jun 3, 2020

This is really helpful, thanks for this amazing gist.

Copy link

devincloudkelly commented Aug 14, 2020

Awesome, this is so helpful! I re deleted and then re-created a heroku deploy, and couldn't remember how to change the remote URL for heroku. Thanks!

Copy link

Abraham-Felix commented Aug 18, 2020


Copy link

byrmylmz commented Oct 28, 2021

Thanks. That's what I need.

Copy link

JO101100 commented Feb 28, 2022

I tried this way but the cmd give this push action was rejected
can u help me :(

Copy link

omsonawane637 commented May 13, 2022

plesase try = git push heroku masterbranch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment