Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git force revert to HEAD~7

Tags:

git

I committed and pushed some bad things. How do I force revert my local repo to HEAD~7, and re-commit so that HEAD is now at that version? Git docs confuse me.

Thanks!

like image 212
atp Avatar asked Jul 14 '10 17:07

atp


People also ask

How do I revert back to a git head?

To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).

What does git reset -- hard head do?

Running git reset --hard ORIG_HEAD will let you go back to where you were, but it will discard your local changes, which you do not want. git reset --merge keeps your local changes.


2 Answers

The nicest approach is to push another commit that reverts the unintended commits. See Jakub Narębski's answer on how to do that.

If for some reason it's worth the potential unfriendliness of pushing an update that isn't a fast-forward (sensitive bits in the commits, for example), give these commands:

git reset --hard HEAD~7
git push --force origin master

The first rewinds your current branch. This is a sharp tool, so be careful.

To stop you accidentally losing work, git won't push your rewound branch. The --force option disables this safety feature.

like image 81
Greg Bacon Avatar answered Sep 22 '22 14:09

Greg Bacon


git reset --hard HEAD~7 will discard your changes entirely.

git reset HEAD~7 will drop the commits but leave changes in the working copy, so that you can edit and re-commit them.

like image 38
Roman Cheplyaka Avatar answered Sep 20 '22 14:09

Roman Cheplyaka