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

后端 未结 18 3100
逝去的感伤
逝去的感伤 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.

    0 讨论(0)
  • 2020-11-22 00:49

    git reflog

    • Find your commit sha in the list then copy and paste it into this command:

    git cherry-pick <the sha>

    0 讨论(0)
  • 2020-11-22 00:50

    This has saved my life: https://medium.com/@CarrieGuss/how-to-recover-from-a-git-hard-reset-b830b5e3f60c

    Basically you need to run:

    for blob in $(git fsck --lost-found | awk ‘$2 == “blob” { print $3 }’); do git cat-file -p $blob > $blob.txt; done
    

    Then manually going through the pain to re-organise your files to the correct structure.

    Takeaway: Never use git reset --hard if you dont completely 100% understand how it works, best not to use it.

    0 讨论(0)
  • 2020-11-22 00:53

    The answer is hidden in the detailed response above, you can simply do:

    $> git reset --hard HEAD@{1}
    

    (See the output of git reflog show)

    0 讨论(0)
  • 2020-11-22 00:54

    If you're really lucky, like I was, you can go back into your text editor and hit 'undo'.

    I know that's not really a proper answer, but it saved me half a day's work so hopefully it'll do the same for someone else!

    0 讨论(0)
  • 2020-11-22 00:54

    Example of IRL case:

    $ git fsck --lost-found

    Checking object directories: 100% (256/256), done.
    Checking objects: 100% (3/3), done.
    dangling blob 025cab9725ccc00fbd7202da543f556c146cb119
    dangling blob 84e9af799c2f5f08fb50874e5be7fb5cb7aa7c1b
    dangling blob 85f4d1a289e094012819d9732f017c7805ee85b4
    dangling blob 8f654d1cd425da7389d12c17dd2d88d318496d98
    dangling blob 9183b84bbd292dcc238ca546dab896e073432933
    dangling blob 1448ee51d0ea16f259371b32a557b60f908d15ee
    dangling blob 95372cef6148d980ab1d7539ee6fbb44f5e87e22
    dangling blob 9b3bf9fb1ee82c6d6d5ec9149e38fe53d4151fbd
    dangling blob 2b21002ca449a9e30dbb87e535fbd4e65bac18f7
    dangling blob 2fff2f8e4ea6408ac84a8560477aa00583002e66
    dangling blob 333e76340b59a944456b4befd0e007c2e23ab37b
    dangling blob b87163c8def315d40721e592f15c2192a33816bb
    dangling blob c22aafb90358f6bf22577d1ae077ad89d9eea0a7
    dangling blob c6ef78dd64c886e9c9895e2fc4556e69e4fbb133
    dangling blob 4a71f9ff8262701171d42559a283c751fea6a201
    dangling blob 6b762d368f44ffffd441e5b8eae6a7b611335b49a2
    dangling blob 724d23914b48443b19eada79c3eb1813c3c67fed
    dangling blob 749ffc9a412e7584245af5106e78167b9480a27b
    dangling commit f6ce1a403399772d4146d306d5763f3f5715cb5a    <- it's this one
    

    $ git show f6ce1a403399772d4146d306d5763f3f5715cb5a

    commit f6ce1a403399772d4146d306d5763f3f5715cb5a
    Author: Stian Gudmundsen Høiland <stian@Stians-Mac-mini.local>
    Date:   Wed Aug 15 08:41:30 2012 +0200
    
        *MY COMMIT MESSAGE IS DISPLAYED HERE*
    
    diff --git a/Some.file b/Some.file
    new file mode 100644
    index 0000000..15baeba
    --- /dev/null
    +++ b/Some.file
    *THE WHOLE COMMIT IS DISPLAYED HERE*
    

    $ git rebase f6ce1a403399772d4146d306d5763f3f5715cb5a

    First, rewinding head to replay your work on top of it...
    Fast-forwarded master to f6ce1a403399772d4146d306d5763f3f5715cb5a.
    
    0 讨论(0)
提交回复
热议问题