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

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

    0 讨论(0)
  • 2020-11-22 02:28

    I also have this problem using the below code to retrieve a previous file to a local directory:

    git checkout <file path with name>
    

    The below example is working for me:

    git checkout resources/views/usaSchools.blade.php

    0 讨论(0)
  • 2020-11-22 02:29

    To restore all those deleted files in a folder, enter the following command.

    git ls-files -d | xargs git checkout --
    
    0 讨论(0)
提交回复
热议问题