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

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

    You could always git revert your commit which deleted the file. (This assumes that the deletion was the only change in the commit.)

    > git log
    commit 2994bda49cd97ce49099953fc3f76f7d3c35d1d3
    Author: Dave 
    Date:   Thu May 9 11:11:06 2019 -0700
    
        deleted readme.md
    

    And if you've continued work, and realized later that you didn't want to commit that deletion commit, you could revert it using:

    > git revert 2994bd
    

    Now git log shows:

    > git log
    Author: Dave 
    Date:   Thu May 9 11:17:41 2019 -0700
    
        Revert "deleted readme"
    
        This reverts commit 2994bda49cd97ce49099953fc3f76f7d3c35d1d3.
    

    And readme.md has been restored into the repository.

提交回复
热议问题