简体   繁体   中英

Git reset - branch is behind master

I made several commits (let's say with IDs: 1, 2, 3, 4) and then realized that I made a mistake in commit 3 and want to go back to the version of code at commit 2.

I did:

git reset --hard 2

Now git says:

On branch master. 
Your branch is behind origin/master by 11 commits and can be fast forwarded.

I am wondering how I can "push" my code so that everyone has this version.

Depending on what you need :

one way to just remove commit 3 from the history is :

$ git rebase -i <id of commit 2>
# an editor will open a text editor, asking you to describe what you want to do
# delete the line containing <commit 3>, save and exit

git will output some messages indicating its progress, and you should end up with a new master branch containing all the commits except <commit 3> .

You will then need to add the --force-with-lease option to push this edited master branch :

git push --force-with-lease origin master

git push -f origin to force push and alter history of your repo. Might be better to just git revert the 2 commits to keep history.

The message you got indicates that you have already pushed commits 3 and 4, so a hard reset is not what you want to do - that command changes history, and you should never try to change the history of a commit that has been pushed. Instead, do a git pull (so that you are up to date). Then git revert 3 to undo the changes you made in commit 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM