Reputation: 163
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?
Upvotes: 3
Views: 2658
Reputation: 16036
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.
Upvotes: 0
Reputation: 47866
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.
Upvotes: 1
Reputation: 19015
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.
Upvotes: 2
Reputation: 10687
You can do
git reset --hard HEAD~1
It will take you to the commit before the current HEAD.
Upvotes: 6