3

I wanted to discard changes in the current branch and without committing, I accidentally entered the command git revert HEAD. The changes in the prev branch (committed earlier) appear to be lost?

How can I undo agit revert HEAD command?

1

4 Answers 4

6

You can do

git reset --hard HEAD~1

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

2

If you did not do a git push after reverting the commit, then Bidhan's answer is spot on. However, if you have pushed since then, you will want to revert your revert by simply doing git revert HEAD again.

1

git revert just creates a new commit. If you haven't pushed it, you can "undo" it using --keep:

 git reset --keep HEAD~1

--keep will reset HEAD to a previous commit and preserve uncommitted local changes.

 git reset --hard HEAD~1

--hard if used instead will discard all local changes in your working directory.

Another way is to use git revert again. Since the command git revert just creates a commit that undoes another, you can run this command again:

git revert HEAD

It'll undo your previous revert and add another commit for that though the commit message becomes messier.

1
  • 1
    git revert HEAD saved my 10 days offline work. Thanks a bunch!
    – Samuel
    Commented Jun 20, 2017 at 4:31
0

git reset --keep is the most direct way of doing this, but I tend to do a lot of git rebase -i.

When the interactive pops up the editor with the list of commits not in the (by default) tracked remote branch, delete the last line, which should be the line corresponding to the revert.

You can also squash or reorder the lines, or stop and edit an older commit.

Not the answer you're looking for? Browse other questions tagged or ask your own question.