Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error "Updates were rejected because the tip of your current branch is behind"

My local system code was synchronized with the remote system at Bitbucket, but there was some problem so I removed the last commit by running git reset --hard HEAD^. After that I had made some changes and commit those changes. Now when I try to push those changes on the remote, I am getting following message:

[vagrant@localhost horizon]$ git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Password for 'https://[email protected]': 
To https://[email protected]/user_name/repo_name.git
 ! [rejected]        stable/kilo -> stable/kilo (non-fast-forward)
error: failed to push some refs to 'https://[email protected]/user_name/repo_name.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.   

My question is, how do I to push in such scenario? Please explain.

like image 787
geeks Avatar asked Jul 20 '15 05:07

geeks


2 Answers

You did the following command:

 git reset --hard HEAD^

The result is a history rewrite. Imagine the following history on local and remote:

Local

A->B->C->D

Remote

A->B->C->D

Now

git reset --hard HEAD^

Some hashes will be rewritten e.g.:

Local

A1->B1->C1->D1

Remote

A->B->C->D

So your history is divirgent from the remote one. Which means for git, that you need to merge. So but your next questions will be: What to do to rescue from this situation? Glad you asked:

1. Note: !Never ever reset history and try to push, especially if you push --force!

  • Reseting history is fine, if you know what you are doing.

A possible solution would be to revert the commit you reseted first and push this reverted commit. But for this you need to get back the 'screwed commit' you reseted with git reset. You can do this by git reflog and extract the commit SHA-1 from there and reset to this commit back. Then you are in state you were before the commit. After this, you do a git reset HEAD^ and you are safe!

like image 96
ckruczek Avatar answered Sep 19 '22 00:09

ckruczek


The problem with a push --force is that you would force all other contributors to reset their own local repo to the new origin/master.

A better alternative would be to use git revert @ (done just after the clone, and before any new commit), in order to create a new commit which cancel the current HEAD. You can then push it without any issue.

like image 42
VonC Avatar answered Sep 18 '22 00:09

VonC