How to undo "git head back"?

I wanted to revert the changes in the current branch and not commit, I accidentally entered the command git revert HEAD

. Changes to the previous branch (committed earlier) are lost?

How can I cancel a command git revert HEAD

?

+3


source to share


4 answers


You can do

git reset --hard HEAD~1

      



It will take you to the commit before the current HEAD.

+6


source


If you didn't git push

after the commit returned, then Bidhan's answer will be included. However, if you've clicked since then, you'll want to go back just by doing it git revert HEAD

again.



+2


source


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.

+1


source


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.

0


source







All Articles