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

前端 未结 27 1674
挽巷
挽巷 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 ^ -- 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.

提交回复
热议问题