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

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

    git undelete path/to/file.ext

    1. Put this in your .bash_profile (or other relevant file that loads when you open a command shell):

      git config --global alias.undelete '!sh -c "git checkout $(git rev-list -n 1 HEAD -- $1)^ -- $1" -'
      
    2. Then use:

      git undelete path/to/file.ext
      

    This alias first checks to find the last commit where this file existed, and then does a Git checkout of that file path from that last commit where this file existed. Source.

提交回复
热议问题