I reverted a Git commit using git revert
but doing so deleted crucial files that were only saved to my local repo. How can I recover those files?
Asked
Active
Viewed 142 times
1
-
1`git revert` does not *delete* **anything**. Instead, it *adds a new commit*. The existing commits remain intact. Note, however, that your working tree may contain files that were never committed (are not in any commit); if you delete one of these files, Git can't get it back because Git never had it. (`git revert` won't normally clobber one of these files, though.) – torek Oct 06 '20 at 04:53
-
@torek That's a bit misleading. If you revert a commit which just created some files then those files will be deleted from your working tree. – Calum Halpin Oct 06 '20 at 12:54
-
@CalumHalpin: true, I meant to say doesn't delete anything *from the repository* (which is why I added the comment about working tree files that were never committed). It's important to realize that working tree files aren't in the repository. – torek Oct 06 '20 at 15:44
-
thank you all. this is resolved. I used git reflog to view prior commits and then git revert to return to that version. – zessok Oct 06 '20 at 19:12
1 Answers
1
You can use git restore
to restore the files as they were before your reversion:
git restore --source=<id-of-revert-commit>^ -- <path-to-file> <path-to-other-file>...

Calum Halpin
- 1,945
- 1
- 10
- 20