Ruby-on-rails – Cannot push to heroku after adding a remote heroku repo to the existing local repo

gitgit-remotegithubherokuruby-on-rails

Here is the scenario:

1) My project partner and I were working on a Ruby on Rails app together using github as our code repo.

2) The app is under her github account and she had added me as a collaborator

3) She deployed to Heroku and added me as a collaborator there as well

4) I used the following command from my existing app directory with the intention of adding the existing Heroku remote app as a remote to my existing local app. My existing local app, as I mentioned before, already had a remote github

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

5) I made some changes and pushed them to github and all was up to date

6) Then I attempted to push to heroku with the following command

git push heroku master

7) This gave me an error saying the tip of my branch was behind, as shown below, but when I tried to pull from github, it said I was up to date, as also shown below

➜  code-fellows-alumni git:(master) git push heroku master
To git@heroku.com:codefellow.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@heroku.com:codefellow.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
➜  code-fellows-alumni git:(master) git pull
Already up-to-date.

So does anyone know what is going on here? How could my local branch be out of sync with Heroku if I'm up to date with Github? Would it have been possible for my project partner to have pushed changes to Heroku without having pushed them to Github first? I checked and she does not have a fork of the application. I cannot get in touch with her at the moment to find out for sure what she might have done–I'm not even sure it would allow her to push changes to Heroku if they hadn't been pushed to Github yet. Any insights would be much appreciated. I don't want to clone the app from Heroku necessarily because I already have it locally synced with Github. I want to use Github as the code repository and I am reluctant to start from a clone from Heroku. I've already looked at the Heroku documentation on this: https://devcenter.heroku.com/articles/git. It just says to do a clone but I don't want to do that for the aforementioned reasons. I followed the directions given in the answer to this question (How to link a folder with an existing Heroku app) to get this far but it seems like there is a missing piece or else my project partner has done something unusual. Thanks in advance for any helpful ideas you might have.

Best Answer

The message you are seeing means that changes have been made to the application that you do not have in your local copy. When you push it's rejected because the Heroku remote is further ahead than yours, so you're right in thinking that your partner has pushed to Heroku without pushing to Github - it's a common scenario since you deploy from your local repository when you deploy to Heroku, unlike a traditional Capistrano deploy which would deploy the code from Github typically.

It's down to you as a team to come up with ways of working which prevent this from occuring, but if you need to get working right now, you can either

  • git push heroku master -f. This forces your changes and will overwrite what's there presently with your code
  • git pull heroku master to pull the changes from Heroku into your local repo which should then let you do a git push heroku master when you have the changes.