Recover from git reset --hard?

后端 未结 22 2515
盖世英雄少女心
盖世英雄少女心 2020-11-22 00:52

Is there any way to recover uncommitted changes to the working directory from a git reset --hard HEAD?

相关标签:
22条回答
  • 2020-11-22 01:36

    Correct answers. OK, now I like git. :-) Here's a simpler recipe.

    git log HEAD@{2}
    git reset --hard  HEAD@{2}
    

    Where "2" is the number of back to where you committed your changes. In my case, interrupted by colleague and boss to help debug some build issue; so, did a reset --hard twice; so, HEAD and HEAD@{1} were over-writes. Whew, would have lost an our of hard work.

    0 讨论(0)
  • 2020-11-22 01:38

    If you are trying to use the code below:

    git reflog show
    # head to recover to
    git reset HEAD@{1} 
    

    and for some reason are getting:

    error: unknown switch `e'

    then try wrapping HEAD@{1} in quotes

    git reset 'HEAD@{1}'
    
    0 讨论(0)
  • 2020-11-22 01:41

    By definition, git reset --hard will throw away uncommitted changes without any way for Git to recover them (your backup system may help, but not Git).

    Actually, there are very few cases where git reset --hard is a good idea. In most cases, there's a safer command to do the same thing:

    • If you want to throw away your uncommitted changes, then use git stash. It will keep a backup of these changes, which will expire after some time if you run git gc. If you're 99.9% sure you'll never need these changes back, then git stash is still your friend for the 0.1% case. If you're 100% sure, then git stash is still your friend because these 100% have a measurement error ;-).

    • If you want to move your HEAD and the tip of the current branch in history, then git reset --keep is your friend. It will do the same thing as git reset --hard, but will not discard your local changes.

    • If you want to do both, then git stash && git reset --keep is your friend.

    Teach your fingers not to use git reset --hard, it will pay back one day.

    0 讨论(0)
  • 2020-11-22 01:43

    Yes, YOU CAN RECOVER from a hard reset in git.

    Use:

    git reflog
    

    to get the identifier of your commit. Then use:

    git reset --hard <commit-id-retrieved-using-reflog>
    

    This trick saved my life a couple of times.

    You can find the documentation of reflog HERE.

    0 讨论(0)
  • 2020-11-22 01:43

    if you accidentally hard reset a commit, then do this,

    git reflog show
    git reset HEAD@{2} // i.e where HEAD used to be two moves ago - may be different for your case
    

    assuming HEAD@{2} is the state you desire to go back to

    0 讨论(0)
  • 2020-11-22 01:44

    This is what I usually do if I lose some changes.

    git reflog
    git checkout <commit id> // now you are in where you want but you cannot push from detached branch to master
    manually copy and paste changes from detached branch to master or working branch
    git reset --hard HEAD // if needed
    git add ... > git commit ... > git push ...
    

    to move the pointer back to your previous commits but keeping the changes you made so far in your latest commits checkout git reset --soft dadada

    0 讨论(0)
提交回复
热议问题