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!
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With