Recover from git reset --hard?

后端 未结 22 2534
盖世英雄少女心
盖世英雄少女心 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:33

    You cannot get back uncommitted changes in general.

    Previously staged changes (git add) should be recoverable from index objects, so if you did, use git fsck --lost-found to locate the objects related to it. (This writes the objects to the .git/lost-found/ directory; from there you can use git show <filename> to see the contents of each file.)

    If not, the answer here would be: look at your backup. Perhaps your editor/IDE stores temp copies under /tmp or C:\TEMP and things like that.[1]

    git reset HEAD@{1}
    

    This will restore to the previous HEAD

    [1] vim e.g. optionally stores persistent undo, eclipse IDE stores local history; such features might save your a**

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

    I ran into same issue and I was almost going insane....initially i committed the project and merged.. later when i try running git push --set-upstream origin master i was getting this error

      fatal: refusing to merge unrelated histories
    

    so i ran git reset --hard HEAD and it deleted a 3 weeks project but these few commands below save the day:

    git reset HEAD@{1}         //this command unstage changes after reset
    git fsck --lost-found      //I got the dangling commit fc3b6bee2bca5d8a7e16b6adaca6a76e620eca4b
    git show <dangling commit something like-> fc3b6bee2bca5d8a7e16b6adaca6a76e620eca4b>
    git rebase fc3b6bee2bca5d8a7e16b6adaca6a76e620eca4b
    

    hope this helps

    0 讨论(0)
  • 2020-11-22 01:33
     git reset HEAD@{4}
    

    4 is changes before 4 steps ago. if you select a correct step, it should show the list of files that you removed from hard. then do:

    $ git reflog show
    

    it's going to show you local commit history we've already created. now do:

    $ git reset --hard 8c4d112
    

    8c4d112 is a code you want to reset your hard there. let's look at https://www.theserverside.com/video/How-to-use-the-git-reset-hard-command-to-change-a-commit-history to get more information.

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

    I did git reset --hard on the wrong project by mistake (I know...). I had just worked on one file and it was still open during and after I ran the command.

    Even though I had not committed, I was able to retrieve the old file with the simple COMMAND + Z.

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

    You can get back a commit after doing a reset --hard HEAD.

    Make use of "git reflog" to check the history of the HEAD in the branch.

    You will see your commit and its id here.

    Do a

    git reset {commit Id of the commit you want to bring back}
    
    0 讨论(0)
  • 2020-11-22 01:36

    If you luckily had the same files opened on another editor (eg. Sublime Text) try a ctrl-z on those. It just saved me..

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