One Reply to “Find and restore a deleted file in a Git repository”

  1. That’s even better: http://blog.kablamo.org/2013/12/08/git-restore/

    ==================

    Don’t be afraid to delete files from your git repository. You can get restore them. You can even search for a string in a deleted file. Here is how to find a deleted file and its commit:

    git log –diff-filter=D –summary # all deleted files ever
    git log –diff-filter=D –summary . # all deleted files in cwd
    git log –diff-filter=D –author=Batman –summary # all files deleted by Batman

    How to restore a deleted file:

    git checkout ~1

    To make this process a little easier next time I need to do it, I created a git alias for finding deleted files by adding this to my .gitconfig file:

    [aliases]
    deleted = log –diff-filter=D –summary

    Now I can find and restore files like this:

    git deleted # find a deleted file and its commit
    git checkout ~1 # restore the deleted file

    How to search the contents of deleted files

    But lets say I don’t remember the filename of that file I deleted in a fit of cleanup passion. I do remember the name of one of the functions in it though. Here is how to deal with that. Search the contents of all files that have ever existed in git for a string:

    git log –summary -S [] [–since=2009.1.1] [–until=2010.1.1]

    Another way to do this:

    git rev-list –all | xargs git grep ‘string’

    Git is all knowing and all seeing and all powerful. Hail git, powerful arcane lord of source control.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.