Skip to content

Instantly share code, notes, and snippets.

@KalemaEdgar
Last active November 3, 2021 12:15
Show Gist options
  • Save KalemaEdgar/a88a09b8bfbf4f63dafce67b7721600c to your computer and use it in GitHub Desktop.
Save KalemaEdgar/a88a09b8bfbf4f63dafce67b7721600c to your computer and use it in GitHub Desktop.
How to recover deleted files from Git
Display a location summary of all deleted files.
$ git log --diff-filter=D --summary
commit fd87304411b93bbf7414f39251ba5e4134b27755 (HEAD)
Author: Kalema Edgar <testemail@gmail.com>
Date: Wed May 1 14:02:36 2019 +0300
Revert "Version 1.0.0 for app"
This reverts commit 81585e50d2523c76f9ce7a4375e9b548895773f0.
delete mode 100644 bootstrap.php
delete mode 100644 composer.json
delete mode 100644 composer.lock
-- Find the last commit that affected the path. The path from where the files were deleted
$ git rev-list -n 1 HEAD -- <file_path>
username@servername /htdocs/app ((fd87304...))
$ git rev-list -n 1 HEAD -- . (I am using a dot cause I am in the same directory)
fd87304411b93bbf7414f39251ba5e4134b27755
The commit that caused the deletion was `fd87304411b93bbf7414f39251ba5e4134b27755`.
Checkout the version at that commit to return the files back.
username@servername /htdocs/app ((fd87304...))
$ git checkout fd87304411b93bbf7414f39251ba5e4134b27755^ -- <filepath_to_recover>
$ git checkout fd87304411b93bbf7414f39251ba5e4134b27755^ -- .
---- https://stackoverflow.com/questions/953481/find-and-restore-a-deleted-file-in-a-git-repository
---- https://www.atlassian.com/git/tutorials/undoing-changes
---- https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud
---- https://hibbard.eu/how-to-restore-a-previously-deleted-file-from-a-git-repository/
---- http://blog.kablamo.org/2013/12/08/git-restore/
@KalemaEdgar
Copy link
Author

This is the summarized version and straight to the point.
Recovers the entire folder while the detailed one has both recovering the folder and single file but you need to know what you are doing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment