How can I undo git reset --hard HEAD~1?

后端 未结 18 3109
逝去的感伤
逝去的感伤 2020-11-22 00:21

Is it possible to undo the changes caused by the following command? If so, how?

git reset --hard HEAD~1
18条回答
  •  鱼传尺愫
    2020-11-22 00:43

    I know this is an old thread... but as many people are searching for ways to undo stuff in Git, I still think it may be a good idea to continue giving tips here.

    When you do a "git add" or move anything from the top left to the bottom left in git gui the content of the file is stored in a blob and the file content is possible to recover from that blob.

    So it is possible to recover a file even if it was not committed but it has to have been added.

    git init  
    echo hello >> test.txt  
    git add test.txt  
    

    Now the blob is created but it is referenced by the index so it will no be listed with git fsck until we reset. So we reset...

    git reset --hard  
    git fsck  
    

    you will get a dangling blob ce013625030ba8dba906f756967f9e9ca394464a

    git show ce01362  
    

    will give you the file content "hello" back

    To find unreferenced commits I found a tip somewhere suggesting this.

    gitk --all $(git log -g --pretty=format:%h)  
    

    I have it as a tool in git gui and it is very handy.

提交回复
热议问题