How to undo "git head back"?
git revert
just creates a new commit. If you haven't clicked it, you can "undo" it with --keep
:
git reset --keep HEAD~1
--keep
will reset the HEAD to the previous commit and keep uncommitted local changes.
git reset --hard HEAD~1
--hard
if used instead will undo all local changes in your working directory.
Another way is to use again git revert
. Since the command git revert
just creates a commit that undoes another, you can run this command again:
git revert HEAD
It will undo the previous return and add another commit for it, although the commit message is getting messy.
source to share
git reset --keep
is the most direct way to do it, but I tend to do a lot git rebase -i
.
When an interactive editor pops up with a list of commits not on the remote branch (default), remove the last line, which should be the line that matches the checkout.
You can also squish or reorder lines, and stop and edit old commits.
source to share