How to restore a whole directory from history of git repository?

后端 未结 3 1649
滥情空心
滥情空心 2020-12-07 14:30

I would like to restore a whole directory (recursively) from the history of my git repository.

There is only 1 branch (master).

I know the commit where error

相关标签:
3条回答
  • 2020-12-07 14:41

    try adding '--' between revisions and paths:

    git checkout 348ce0aa02d3738e55ac9085080028b548e3d8d3 -- path/to/the/folder/ 
    

    And if you want to recover a directory from the previous commit, you can replace the commit hash by HEAD~1, for example:

    git checkout HEAD~1 -- path/to/the/folder/ 
    
    0 讨论(0)
  • 2020-12-07 14:45

    If you simply do git checkout <SHA-ID> then it will temporarily move you to that sha-commit.

    Each commit object holds the entire structure of the disk at that time, so if you have files there and need to copy them out, you can do so. Warning though, you will not be in any branch, so you'll have to move back to master before copying the file into your working tree and commit it.

    0 讨论(0)
  • 2020-12-07 14:56

    There are two easy ways to do this:

    If the commit that included the errors only included the errors, use git revert to invert the effects of it.

    If not, the easy path is this:

    1. git checkout 348…
    2. cp -a path/to/the/folder ../tmp-restore-folder
    3. git checkout HEAD # or whatever
    4. rm -rf path/to/the/folder
    5. mv ../tmp-restore-folder path/to/the/folder
    6. git add path/to/the/folder
    7. git commit -m "revert …"
    0 讨论(0)
提交回复
热议问题