How to find and restore a deleted file in a Git repository

前端 未结 27 1053
挽巷
挽巷 2020-11-22 01:25

Say I\'m in a Git repository. I delete a file and commit that change. I continue working and make some more commits. Then, I find I need to restore that file.

I know

相关标签:
27条回答
  • 2020-11-22 02:12

    To restore a deleted and commited file:

    git reset HEAD some/path
    git checkout -- some/path
    

    It was tested on Git version 1.7.5.4.

    0 讨论(0)
  • 2020-11-22 02:13

    If you only made changes and deleted a file, but not commit it, and now you broke up with your changes

    git checkout -- .
    

    but your deleted files did not return, you simply do the following command:

    git checkout <file_path>
    

    And presto, your file is back.

    0 讨论(0)
  • 2020-11-22 02:13
    git checkout /path/to/deleted.file
    
    0 讨论(0)
  • 2020-11-22 02:14

    For the best way to do that, try it.


    First, find the commit id of the commit that deleted your file. It will give you a summary of commits which deleted files.

    git log --diff-filter=D --summary

    git checkout 84sdhfddbffffdf~1

    Note: 84sdhfddbffffd is your commit id

    Through this you can easily recover all deleted files.

    0 讨论(0)
  • 2020-11-22 02:17

    My new favorite alias, based on bonyiii's answer (upvoted), and my own answer about "Pass an argument to a Git alias command":

    git config alias.restore '!f() { git checkout $(git rev-list -n 1 HEAD -- $1)~1 -- $(git diff --name-status $(git rev-list -n 1 HEAD -- $1)~1 | grep '^D' | cut -f 2); }; f'
    

    I have lost a file, deleted by mistake a few commits ago?
    Quick:

    git restore my_deleted_file
    

    Crisis averted.

    Warning, with Git 2.23 (Q3 2019) comes the experimental command named git restore(!).
    So rename this alias (as shown below).


    Robert Dailey proposes in the comments the following alias:

    restore-file = !git checkout $(git rev-list -n 1 HEAD -- "$1")^ -- "$1"
    

    And jegan adds in the comments:

    For setting the alias from the command line, I used this command:

    git config --global alias.restore "\!git checkout \$(git rev-list -n 1 HEAD -- \"\$1\")^ -- \"\$1\"" 
    
    0 讨论(0)
  • 2020-11-22 02:19

    I've got this solution.

    1. Get the id of the commit where the file was deleted using one of the ways below.

      • git log --grep=*word*
      • git log -Sword
      • git log | grep --context=5 *word*
      • git log --stat | grep --context=5 *word* # recommended if you hardly remember anything
    2. You should get something like:

    commit bfe68bd117e1091c96d2976c99b3bcc8310bebe7 Author: Alexander Orlov Date: Thu May 12 23:44:27 2011 +0200

    replaced deprecated GWT class
    - gwtI18nKeySync.sh, an outdated (?, replaced by a Maven goal) I18n generation script
    

    commit 3ea4e3af253ac6fd1691ff6bb89c964f54802302 Author: Alexander Orlov Date: Thu May 12 22:10:22 2011 +0200

    3. Now using the commit id bfe68bd117e1091c96d2976c99b3bcc8310bebe7 do:

    git checkout bfe68bd117e1091c96d2976c99b3bcc8310bebe7^1 yourDeletedFile.java
    

    As the commit id references the commit where the file was already deleted you need to reference the commit just before bfe68b which you can do by appending ^1. This means: give me the commit just before bfe68b.

    0 讨论(0)
提交回复
热议问题