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

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

    In many cases, it can be useful to use coreutils (grep, sed, etc.) in conjunction with Git. I already know these tools quite well, but Git less so. If I wanted to do a search for a deleted file, I would do the following:

    git log --raw | grep -B 30 $'D\t.*deleted_file.c'
    

    When I find the revision/commit:

    git checkout <rev>^ -- path/to/refound/deleted_file.c
    

    Just like others have stated before me.

    The file will now be restored to the state it had before removal. Remember to re-commit it to the working tree if you want to keep it around.

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

    Find the commit that deleted your file:

    git log --diff-filter=D --oneline -- path/to/file | cut -f -d ' '
    

    Sample output:

    4711174
    

    As of Git 2.23 there is actually a restore command. It is still experimental but in order to restore something you removed in a commit (4711174 in this case) you can then type:

    git restore --source=4711174^ path/to/file
    

    Note the ^ after the commit id as we want to restore something from the commit before the one that deleted the file.

    The --source argument tells the restore command where to look for the file(s) to restore and it can be any commit and even the index.

    See: git-restore doc for git 2.23.0

    0 讨论(0)
  • 2020-11-22 02:09
    user@bsd:~/work/git$ rm slides.tex
    user@bsd:~/work/git$ git pull 
    Already up-to-date.
    user@bsd:~/work/git$ ls slides.tex
    ls: slides.tex: No such file or directory
    

    Restore the deleted file:

    user@bsd:~/work/git$ git checkout
    D       .slides.tex.swp
    D       slides.tex
    user@bsd:~/work/git$ git checkout slides.tex 
    user@bsd:~/work/git$ ls slides.tex
    slides.tex
    
    0 讨论(0)
  • 2020-11-22 02:10

    If you’re insane, use git-bisect. Here's what to do:

    git bisect start
    git bisect bad
    git bisect good <some commit where you know the file existed>
    

    Now it's time to run the automated test. The shell command '[ -e foo.bar ]' will return 0 if foo.bar exists, and 1 otherwise. The "run" command of git-bisect will use binary search to automatically find the first commit where the test fails. It starts halfway through the range given (from good to bad) and cuts it in half based on the result of the specified test.

    git bisect run '[ -e foo.bar ]'
    

    Now you're at the commit which deleted it. From here, you can jump back to the future and use git-revert to undo the change,

    git bisect reset
    git revert <the offending commit>
    

    or you could go back one commit and manually inspect the damage:

    git checkout HEAD^
    cp foo.bar /tmp
    git bisect reset
    cp /tmp/foo.bar .
    
    0 讨论(0)
  • 2020-11-22 02:11

    If you know the commit that deleted the file(s), run this command where <SHA1_deletion> is the commit that deleted the file:

    git diff --diff-filter=D --name-only <SHA1_deletion>~1 <SHA1_deletion> | xargs git checkout <SHA1_deletion>~1 --
    

    The part before the pipe lists all the files that were deleted in the commit; they are all checkout from the previous commit to restore them.

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

    You could always git revert your commit which deleted the file. (This assumes that the deletion was the only change in the commit.)

    > git log
    commit 2994bda49cd97ce49099953fc3f76f7d3c35d1d3
    Author: Dave <dave@domain.com>
    Date:   Thu May 9 11:11:06 2019 -0700
    
        deleted readme.md
    

    And if you've continued work, and realized later that you didn't want to commit that deletion commit, you could revert it using:

    > git revert 2994bd
    

    Now git log shows:

    > git log
    Author: Dave <dave@domain.com>
    Date:   Thu May 9 11:17:41 2019 -0700
    
        Revert "deleted readme"
    
        This reverts commit 2994bda49cd97ce49099953fc3f76f7d3c35d1d3.
    

    And readme.md has been restored into the repository.

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