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

前端 未结 27 1139
挽巷
挽巷 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:20

    Find the last commit that affected the given path. As the file isn't in the HEAD commit, this commit must have deleted it.

    git rev-list -n 1 HEAD -- <file_path>
    

    Then checkout the version at the commit before, using the caret (^) symbol:

    git checkout <deleting_commit>^ -- <file_path>
    

    Or in one command, if $file is the file in question.

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

    If you are using zsh and have the EXTENDED_GLOB option enabled, the caret symbol won't work. You can use ~1 instead.

    git checkout $(git rev-list -n 1 HEAD -- "$file")~1 -- "$file"
    
    0 讨论(0)
  • 2020-11-22 02:20

    I came to this question looking to restore a file I just deleted but I hadn't yet committed the change. Just in case you find yourself in this situation, all you need to do is the following:

    git checkout HEAD -- path/to/file.ext

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

    I had to restore a bunch of deleted files from a specific commit, and I managed it with two commands:

    git show <rev> --diff-filter=D --summary --name-only --no-commit-id | xargs git checkout <rev>^ -- 
    git show <rev> --diff-filter=D --summary --name-only --no-commit-id | xargs git reset HEAD 
    

    (Note the trailing space on the end of each command.)

    The files had been added to the .gitignore file and then cleared with git rm. I needed to restore the files, but then unstage them. I had hundreds of files to restore, and typing things manually for each file as in the other examples was going to be far too slow.

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

    I had the same question. Without knowing it, I had created a dangling commit.

    List dangling commits

    git fsck --lost-found

    Inspect each dangling commit

    git reset --hard <commit id>

    My files reappeared when I moved to the dangling commit.

    git status for the reason:

    “HEAD detached from <commit id where it detached>”

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

    Simple and precise-

    First of all, get a latest stable commit in which you have that file by -

    git log 
    

    Say you find $commitid 1234567..., then

    git checkout <$commitid> $fileName
    

    This will restore the file version which was in that commit.

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

    If the deletion has not been committed, the command below will restore the deleted file in the working tree.

    $ git checkout -- <file>
    

    You can get a list of all the deleted files in the working tree using the command below.

    $ git ls-files --deleted
    

    If the deletion has been committed, find the commit where it happened, then recover the file from this commit.

    $ git rev-list -n 1 HEAD -- <file>
    $ git checkout <commit>^ -- <file>
    

    In case you are looking for the path of the file to recover, the following command will display a summary of all deleted files.

    $ git log --diff-filter=D --summary
    
    0 讨论(0)
提交回复
热议问题