Skip to content

Instantly share code, notes, and snippets.

@randallreedjr
Last active October 5, 2025 12:05
Show Gist options
  • Save randallreedjr/aa89e069371d07371882eea2df15fb4d to your computer and use it in GitHub Desktop.
Save randallreedjr/aa89e069371d07371882eea2df15fb4d to your computer and use it in GitHub Desktop.
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.

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 below (in this example, we push the

local staging branch to the master branch on heroku

$ git remote add staging https://git.heroku.com/staging-app.git $ 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 sure you want to proceed, add the --force (-f) flag

$ git push staging staging:master -f

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

$ git remote add heroku https://git.heroku.com/app.git $ git push heroku master

To see a list of your remotes

$ git remote -v

As @voke points out, you can alternatively use a Heroku CLI command.

However, it looks like this will always use the default remote name "heroku"

You could then use git to rename the remote, as follows

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

As @Saworieza points out, all of the examples above use the https protocol for connecting to the remotes,

but it is also to connect via ssh

$ git remote add staging [email protected]:staging-app.git $ git remote add heroku [email protected]:app.git

If you have already created https remotes and want to switch them to use ssh, the following command can be used

$ git remote set-url staging [email protected]:staging-app.git

@tzvc
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 ?

@Saworieza
Copy link

Saworieza commented Dec 13, 2018

Hi. I found that this method is slightly misleading.
git remote add heroku [email protected]:app-name.git
works much better. doesn't add two apps to your remote and is straightforward

@voke
Copy link

voke commented Nov 11, 2019

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

@randallreedjr
Copy link
Author

@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.

@nruth
Copy link

nruth commented Jan 17, 2020

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

@randallreedjr
Copy link
Author

@nruth nice tip, thank you!

@levous
Copy link

levous commented Feb 29, 2020

Dude, thanks for this. Super helpful

@Joxebus
Copy link

Joxebus commented Jun 3, 2020

This is really helpful, thanks for this amazing gist.

@devincloudkelly
Copy link

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!

@Abraham-Felix
Copy link

Thanks.

@byrmylmz
Copy link

Thanks. That's what I need.

@JOYoussefHassan
Copy link

JOYoussefHassan commented Feb 28, 2022

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

@omsonawane637
Copy link

plesase try = git push heroku masterbranch

@f2ka07
Copy link

f2ka07 commented Feb 19, 2023

Here is a video that explains how to use Heroku CLI

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