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

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

    I had to restore a bunch of deleted files from a specific commit, and I managed it with two commands:

    git show  --diff-filter=D --summary --name-only --no-commit-id | xargs git checkout ^ -- 
    git show  --diff-filter=D --summary --name-only --no-commit-id | xargs git reset HEAD 
    

    (Note the trailing space on the end of each command.)

    The files had been added to the .gitignore file and then cleared with git rm. I needed to restore the files, but then unstage them. I had hundreds of files to restore, and typing things manually for each file as in the other examples was going to be far too slow.

提交回复
热议问题