How to get the deleted files back from a git repo

This question already has an answer here:

  • Find and restore a deleted file in a Git repository 19 answers

  • Files are rm

    If you know which files are removed:

    git check -- filename
    

    or

    git checkout-index filename
    

    If you don't know which files are removed or there are too many removed files:

    git ls-files -d | xargs git checkout --
    

    Files are git rm

    Use reset to roll back the index first

    git reset HEAD
    

    And use the commands listed above

    git ls-files -d | xargs git checkout-index
    

    Files are git rm and git commit

    If you know which commit(ex: 2ae853) you remove the files, you can checkout files from the previous commit(2ae853^) of that commit:

    git checkout 2ae853^ -- filename
    

    If you forget which commit you removed the files, use rev-list to find the commit first:

    git rev-list -n 1 HEAD -- filename
    

    And use the previous command to get the files back.

    链接地址: http://www.djcxy.com/p/18932.html

    上一篇: git:如何恢复已被删除的文件?

    下一篇: 如何从git回购中获取已删除的文件