Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an error pushing to github - Updates were rejected because a pushed branch tip is behind its remote

Tags:

git

heroku

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 [email protected]: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.

like image 401
Michael Durrant Avatar asked Jan 30 '13 16:01

Michael Durrant


People also ask

How do you fix updates were rejected because the tip of your current branch is behind?

The updates were rejected because the tip of your current branch is behind error can be fixed by pushing to a remote branch. This process is called a Git push, and it debugs the errors of the current branch, which is also known as a local branch.

Why is Github rejecting my push?

A commit gets rejected and causes a failed to push some refs to error because the remote branch contains code that you do not have locally. What this means is that your local git repository is not compatible with the remote origin. Based on the above, your local machine is missing commits C and D.

How do you resolve hint updates were rejected because the remote contains work that you do?

hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again.

What to do if branch is behind Master?

The 4 commits behind master just means that your branch is out of sync with the master, and you should do a merge from master to your branch in order to get the latest changes from master into your branch.


3 Answers

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 [email protected]:<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.

like image 78
uday Avatar answered Sep 29 '22 22:09

uday


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

like image 38
Michael James Kali Galarnyk Avatar answered Sep 29 '22 23:09

Michael James Kali Galarnyk


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.

like image 6
guest101 Avatar answered Sep 30 '22 00:09

guest101