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

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

    If you know the filename, this is an easy way with basic commands:

    List all the commits for that file.

    git log -- path/to/file
    

    The last commit (topmost) is the one that deleted the file. So you need to restore the second to last commit.

    git checkout {second to last commit} -- path/to/file
    

提交回复
热议问题