0

I was trying to push my code to my git but accidentally did a git revert. It looks like this:

[master 36aff31] Revert "Addded files"
 54 files changed, 44739 deletions(-)
 delete mode 100644 myfolder/.DS_Store
 delete mode 100644 myfolder/1198232958151161470_462274348.jpg
 delete mode 100644 myfolder/localfile.py
 ................
 ................

I did a git checkout . in order to get files back but nothing happened. How can I get my files back?

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
Deepak
  • 33
  • 5
  • 2
    You can just `git revert` the revert commit. Alternatively [reset to the previous commit](https://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit). – Oliver Charlesworth May 24 '17 at 08:55
  • You need to understand how git works. Your files were deleted on disk but not in the git repository history. Older commits will still have those files. If you didn't intend to revert a commit that added a lot of files you can either reset back before this revert commit (assuming there aren't any more commits following it), or do a revert again which will revert the revert (two negatives make a positive here). – Lasse V. Karlsen May 24 '17 at 09:03
  • 1
    From experience I will not tell you to use the git reset approach because if you don't understand what git does I'm afraid that you might come back with "all my latest commits are now gone as well, halp!" – Lasse V. Karlsen May 24 '17 at 09:04
  • Possible duplicate of [How to revert Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit) – Oliver Charlesworth May 25 '17 at 12:01

1 Answers1

0

If you want to keep your commit history clean while undoing your accidental git revert, you can use git reset with the --hard option to set the state of your working directory back to the commit immediately before the current (revert) commit (HEAD^):

git reset --hard HEAD^

NB: be sure that you haven't made any other commits since the accidental commit or this will discard those changes from your working directory.

If you don't care about a clean commit history, running a second git revert is probably safer.

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56