Home > Mobile >  Is there any way to get back the committed changes before a git revert?
Is there any way to get back the committed changes before a git revert?

Time:09-16

I accidently performed a git revert so that I could change a spelling mistake in the immediate previous commit message

$ git commit -m "fix: pagination for tag, auhtor pages"
[dev 389531c] fix: pagination for tag, auhtor pages
10 files changed, 398 insertions( ), 64 deletions(-)
...
$ git revert 389531c

I have lost all my changes and want to get them back. I have tried git reset 389531c and git cherry-pick 389531c but I am not getting my committed changes back and digging myself more into reset. Is there any way to get them back? I am not familiar with "advanced" git commands.

What I want is to get the changes I committed in HEAD@{4}.Here is my git reflog> Thank you.

$ git reflog
389531c (HEAD -> dev) HEAD@{0}: reset: moving to 389531c
389531c (HEAD -> dev) HEAD@{1}: reset: moving to 389531c
389531c (HEAD -> dev) HEAD@{2}: reset: moving to HEAD@{1}
e7d8df9 HEAD@{3}: revert: Revert "fix: pagination for tag, auhtor pages"
389531c (HEAD -> dev) HEAD@{4}: commit: fix: pagination for tag, auhtor pages
f4c0fb3 (origin/main, origin/HEAD, main) HEAD@{5}: checkout: moving from main to dev

CodePudding user response:

Solution: Add --hard to your reset command.

Reason: The command git reset 389531c defaults to --mixed which means your reverted changes will be pending and still present in your local directory. When you do --hard it will also delete those reverted changes. Another thing you can do is undo your pending changes after a mixed reset.

Side Note: As Leonardo Alves Machado pointed out in a comment, a simple way to fix the spelling error (which you can still do after the hard reset) is to make your minor tweak and amend your existing commit (git commit --amend).

  •  Tags:  
  • git
  • Related