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

♀尐吖头ヾ 提交于 2020-12-29 18:15:17

问题


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


回答1:


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!




回答2:


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.



来源:https://stackoverflow.com/questions/31509324/getting-error-updates-were-rejected-because-the-tip-of-your-current-branch-is-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!