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

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

    In our case we accidentally deleted files in a commit and some commits later we realized our mistake and wanted to get back all the files that were deleted, but not those that were modified.

    Based on Charles Bailey's excellent answer, here is my one-liner:

    git co $(git rev-list -n 1 HEAD -- )~1 -- $(git diff --name-status $(git rev-list -n 1 HEAD -- )~1 head | grep '^D' | cut -f 2)
    

提交回复
热议问题