16

I am having a problem pushing to a different heroku remote.

To check myself I renamed my entire project directory to _backup and then:

git clone account/repo_name

git remote add repo2 git@heroku.com:repo2.git

git push repo2 branch_abc:master

But I am still getting

hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and merge the remote changes
hint: (e.g. 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I have tried several SO questions and answers but not worked for me, still getting the error regardless.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497

3 Answers3

42

If you don't care about what's currently in repo2 and are confident that totally overwriting it is ok then you can use:

$ git push -f git@heroku.com:<heroku repo name>.git

Remember though that push -f can wipe out other developers changes if they were posted since you last pulled from the repo... so always use with extreme caution on multi-developer teams!
In this case heroku is always downstream and github is where the code is managed and maintained so this makes push -f on heroku a safer option that it would otherwise be.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
uday
  • 8,544
  • 4
  • 30
  • 54
7

I am always a big fan of using git pull --rebase and then git push origin master . A couple of the places I have worked since a lot of places do not allow a push -f (especially places that use bitbucket).

git pull --rebase 
git push origin master

The rebase will apply your changes after those already to the remote (online website). This video literally goes over your exact issue and solves it using git pull --rebase https://youtu.be/IhkvMPE9Jxs?t=10m36s

6

If you pull the other repo first:

git pull repo2

This will merge in the other repos's changes which you can add and commit.

Then you can push the repo back.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
guest101
  • 61
  • 1
  • 1
  • 1
    Yeah, I do that from time to time. But why does it happen? Also, the changes always appear to be blank. "Showing zero changed files" – holden Feb 02 '14 at 16:56
  • I suspect it's because repo2 existed before. Maybe as a skeleton. If so one would either need to pull the skeleton files with `git pull` to have those plus the newer changes merged in. Then pushing is ok. Or use `git push -f` if confident that the older code is irrelevant. – Michael Durrant Apr 28 '14 at 21:24