474

Heroku has a policy of ignoring all branches but 'master'.

While I'm sure Heroku's designers have excellent reasons for this policy (I'm guessing for storage and performance optimization), the consequence to me as a developer is that whatever local topic branch I may be working on, I would like an easy way to switch Heroku's master to that local topic branch and do a "git push heroku -f" to over-write master on Heroku.

What I got from reading the "Pushing Refspecs" section of http://progit.org/book/ch9-5.html is

git push -f heroku local-topic-branch:refs/heads/master

What I'd really like is a way to set this up in the configuration file so that "git push heroku" always does the above, replacing local-topic-branch with the name of whatever my current branch happens to be. If anyone knows how to accomplish that, please let me know!

The caveat for this, of course, is that this is only sensible if I am the only one who can push to that Heroku app/repository. A test or QA team might manage such a repository to try out different candidate branches, but they would have to coordinate so that they all agree on what branch they are pushing to it on any given day.

Needless to say, it would also be a very good idea to have a separate remote repository (like GitHub) without this restriction for backing everything up to. I'd call that one "origin" and use "heroku" for Heroku so that "git push" always backs up everything to origin, and "git push heroku" pushes whatever branch I'm currently on to Heroku's master branch, overwriting it if necessary.

Would this work?

[remote "heroku"]
    url = git@heroku.com:my-app.git
    push = +refs/heads/*:refs/heads/master

I'd like to hear from someone more experienced before I begin to experiment, although I suppose I could create a dummy app on Heroku and experiment with that.

As for fetching, I don't really care if the Heroku repository is write-only. I still have a separate repository, like GitHub, for backup and cloning of all my work.

Footnote: This question is similar to, but not quite the same as Good Git deployment using branches strategy with Heroku?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Lawrence I. Siden
  • 9,191
  • 10
  • 43
  • 56
  • 1
    The currently most voted answer is the idiomatic way of doing this (and Imo the real correct answer) – Selali Adobor Feb 07 '17 at 00:33
  • An alternative https resource about pushing refspecs: [git scm documentation about pushing refspecs](https://git-scm.com/book/en/v2/Git-Internals-The-Refspec). – Dylan Landry Nov 09 '18 at 19:35

12 Answers12

1693

See https://devcenter.heroku.com/articles/git#deploying-code

$ git push heroku yourbranch:master
jassa
  • 20,051
  • 4
  • 26
  • 24
  • 37
    May require a -f force. – Scott Stafford Feb 24 '15 at 23:04
  • 3
    @DaveMeehan this still works. You're trying to do `git push :master` which deletes the master branch by overwriting it with nothing. That's different than overwriting it with another branch. Heroku probably has safeguards in place to prevent deleting the master branch. – Dennis Aug 17 '15 at 10:11
  • 2
    @nxmohamad It does not unless your branch in behind the master branch and you want to override the current code on Heroku. – ricks Feb 26 '19 at 20:38
  • looks like we need force push git push -f heroku yourbranch:master @jassa – Nischal Hada Jun 11 '20 at 03:44
  • Is there a way to do this but have the branch deploy at a different url (so it doesn't affect the production version of the site)? – stevec Feb 01 '21 at 05:30
  • `git push heroku $(git branch --show-current):master` – Romain TAILLANDIER Jul 13 '22 at 08:35
147

When using a wildcard, it had to be present on both sides of the refspec, so +refs/heads/*:refs/heads/master will not work. But you can use +HEAD:refs/heads/master:

git config remote.heroku.push +HEAD:refs/heads/master

Also, you can do this directly with git push:

git push heroku +HEAD:master
git push -f heroku HEAD:master
Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
  • 4
    whats the difference btween these two commands or do we have to carry out both – Saad Masood Sep 26 '13 at 10:13
  • 2
    @SaadMasood: Those last to `git push` commands do the same thing. See `git push --help` for the meaning of the `-f` option and the `+` in the refspec. – Chris Johnsen Sep 26 '13 at 10:33
  • 4
    @Chris Johnson: Could you please tell us what the -f parameters means instead of RTFMing the guys here? – AHH Oct 12 '17 at 06:41
  • @AHH `-f` stands for **force**. It worked for me with [jassa’s answer](https://stackoverflow.com/a/11143639/2948889). – Mr. Tao May 06 '18 at 10:05
  • @Chris Johnson: Is HEAD used to only push the most recent release of the app, rather than the entire history? – Cameron Wilby Mar 31 '20 at 19:02
  • 1
    @Cameron Wilby `HEAD` just refers to the very last commit in your for branch (the head of the branch), and because of the way git works will include every preceding commit up to that point. – Wes Mason Jun 25 '20 at 09:46
80
git push -f heroku local_branch_name:master
testuser
  • 793
  • 3
  • 13
  • 35
Tomasz Mazur
  • 997
  • 7
  • 5
  • 4
    Watch out, this uses `-f` or `--force`, and it's always best to ensure you know what you're doing when you force push. – MiFiHiBye May 10 '17 at 22:13
  • @tomasz-mazur why does it require -f? – nxmohamad Sep 21 '17 at 18:13
  • Yes we may need to use -f in some cases like working with multiple in-progress branches and replace the whatever in heroku and test the working branch, please advice in case do we have any other better way to test – Fahad Apr 15 '18 at 09:18
  • it works on my computer, thanks for the answer, actually is this syntax valid in normal Github repository? – Luk Aron Mar 20 '20 at 09:10
18

For me, it works,

git push -f heroku otherBranch:master

The -f (force flag) is recommended in order to avoid conflicts with other developers’ pushes. Since you are not using Git for your revision control, but as a transport only, using the force flag is a reasonable practice.

source :- offical docs

Ashad Nasim
  • 2,511
  • 21
  • 37
13

The safest command to push different local Git branches to Heroku/master.

git push -f heroku branch_name:master

Note: Although, you can push without using the -f, the -f (force flag) is recommended in order to avoid conflicts with other developers’ pushes.

techdreams
  • 5,371
  • 7
  • 42
  • 63
6

Also note that if your using the git flow system and your feature branch might be called

feature/mobile_additions

and with a git remote called stagingtwo, then the command to push to heroku would be

git push stagingtwo feature/mobile_additions:master
Jonathon Batson
  • 1,191
  • 11
  • 13
5
git push heroku $(git branch --show-current):master

Alternately:

git push heroku HEAD:master
Dr Nic
  • 2,072
  • 1
  • 15
  • 19
4

You should check out heroku_san, it solves this problem quite nicely.

For example, you could:

git checkout BRANCH
rake qa deploy

It also makes it easy to spin up new Heroku instances to deploy a topic branch to new servers:

git checkout BRANCH
# edit config/heroku.yml with new app instance and shortname
rake shortname heroku:create deploy # auto creates deploys and migrates

And of course you can make simpler rake tasks if you do something frequently.

jqr
  • 654
  • 6
  • 11
2

I found this helpful. http://jqr.github.com/2009/04/25/deploying-multiple-environments-on-heroku.html

David
  • 281
  • 3
  • 4
1

Heroku labs now offers a github add-on that let's you specify which branch to push.

See Heroku's write up on this beta feature.

You'll need to sign-up as a beta tester for the time-being.

irth
  • 1,696
  • 2
  • 15
  • 24
0

I think it should be

push = refs/heads/*:refs/heads/*

instead...

Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
ken
  • 13,869
  • 6
  • 42
  • 36
0

At some point in 2022, the syntax 'git push heroku otherbranchname:master' stopped working for me. It would always just return "Everything up-to-date". After consulting the Heroku docs, I found that "master" had been changed to "main", so the syntax is now like this:

git push heroku otherbranchname:main

Derrick Miller
  • 1,860
  • 3
  • 21
  • 37