[git] Recover from git reset --hard?

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

This question is related to git

The answer is


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!


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}

I just did git reset --hard and lost all my uncommitted changes. Luckily, I use an editor (IntelliJ) and I was able to recover the changes from the Local History. Eclipse should allow you to do the same.


 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.


If you're developing on Netbeans, look between the file tabs and the file edit area. There is a "Source" and "History". On "History" you'll see changes made using version control (git/other), but also changes made locally. In this case, local changes could save you.


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


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.


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.


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.


(answer suitable for a subset of users)

If you're on (any recent) macOS, and even if you're away from your Time Machine disk, the OS will have saved hourly backups, called local snapshots.

Enter Time Machine and navigate to the file you lost. The OS will then ask you:

The location to which you're restoring "file.ext" already contains an
item with the same name. Do you want to replace it with the one you're
restoring?

You should be able to recover the file(s) you lost.


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


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.


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.


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


In case you're using VS Code and happen to have the affected files open, you can undo the changes made by Git on a per-file basis. (so CTRL + Z)


answer from this SO

$ git reflog show

4b6cf8e (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: reset: moving to origin/master
295f07d HEAD@{1}: pull: Merge made by the 'recursive' strategy.
7c49ec7 HEAD@{2}: commit: restore dependencies to the User model
fa57f59 HEAD@{3}: commit: restore dependencies to the Profile model
3431936 HEAD@{4}: commit (amend): restore admin
033f5c0 HEAD@{5}: commit: restore admin
ecd2c1d HEAD@{6}: commit: re-enable settings app

# the commit the HEAD to be pointed to is 7c49ec7 (restore dependencies to the User model)

$ git reset HEAD@{2}

You got your day back! :)


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}'

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!!


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.


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.


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.


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