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

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

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

    $ git checkout -- 
    

    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 -- 
    $ git checkout ^ -- 
    

    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
    

提交回复
热议问题