Recover from git reset --hard?

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

    The information is lost.

    Since you did not commit, your .git never stored this information. So, basically git cannot recover it for you.

    But, If you just did git diff, there is a way you can recover using the terminal output with the following 3 simple steps.

    1. scroll your terminal and look for the o/p of git diff. Save the o/p in a file called diff.patch
    2. Search & Replace all 7 spaces and 8 spaces with tab(\t) character and save the changes.
    3. Go into your git repository. Apply the diff.patch (patch -p1 < diff.patch)

    You are saved! :)

    Note : While you are copying the data from terminal to a file, be careful and clearly see that the data is continuous output and did not contain any redundant data(due to pressing up and down arrows). Otherwise you might mess it up.

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

    I accidentally ran git reset --hard on my repo today too while having uncommitted changes too today. To get it back, I ran git fsck --lost-found, which wrote all unreferenced blobs to <path to repo>/.git/lost-found/. Since the files were uncommitted, I found them in the other directory within the <path to repo>/.git/lost-found/. From there, I can see the uncommitted files using git show <filename>, copy out the blobs, and rename them.

    Note: This only works if you added the files you want to save to the index (using git add .). If the files weren't in the index, they are lost.

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

    While I was working on a local project, I wanted to move it to GitHub and then created a new repository. While I was trying to add all these files to the new repository with .gitignore, I accidentally added a wrong file and then tried to clear it.

    I ran git reset --hard origin/master :P

    Then all of my local files deleted because the repo was empty. I thought everything was gone.

    This saved my life:

    git reflog show
    git reset HEAD@{1} 
    git push 
    

    Hope it saves another life.

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

    I found out the hard way that any uncommitted files before a git reset --hard <commit> gets removed from git history. However, I was lucky enough to have kept my code editor session open during the entire time I was pulling my hair out, that I discovered that a simple control + z in each of the affected files returned the state of the file back to the version before Git so obligingly reset everything I didn't ask it to specifically. Hooray!!

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

    If you use something like IntelliJ:

    On the context menu, choose Local History, and click Show History on the submenu:

    The local history view for a project or folder shows you everything that you have done during the last few days. In the Action column of the lower part of the dialog box, select the action you want to roll back. [...] So doing, the upper part of the dialog box shows the tree view of changed files. If you want to restore the deleted file only, regardless of the other changes that have been done since then, you can select the file Lost.txt in the tree view and click the Revert button.

    http://blog.jetbrains.com/idea/2008/01/using-local-history-to-restore-deleted-files/

    This just got my arse out the fire!

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

    If you had a IDE open with the same code, try doing a ctrl+z on each individual file that you have made changes to. It helped me recover my uncommited changes after doing git reset --hard.

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