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

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

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

    git bisect start
    git bisect bad
    git bisect good 
    

    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 
    

    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 .
    

提交回复
热议问题