Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recover from git reset --hard?

Tags:

git

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

like image 983
Jacob Lyles Avatar asked Apr 26 '11 08:04

Jacob Lyles


People also ask

Can you recover after git reset hard?

We can use the command git fsck to recover the files after a hard reset.

Can git reset be reversed?

So, to undo the reset, run git reset HEAD@{1} (or git reset d27924e ). If, on the other hand, you've run some other commands since then that update HEAD, the commit you want won't be at the top of the list, and you'll need to search through the reflog .

What after git reset hard?

If you do git reset --hard <SOME-COMMIT> then Git will: Make your current branch (typically master ) back to point at <SOME-COMMIT> . Then make the files in your working tree and the index ("staging area") the same as the versions committed in <SOME-COMMIT> .

Does git reset hard remove history?

They all rewrite Git history, and they all move the HEAD back, but they deal with the changes differently: git reset --soft , which will keep your files, and stage all changes back automatically. git reset --hard , which will completely destroy any changes and remove them from the local directory.


Video Answer


2 Answers

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  # assuming you want to get back to 7c49ec7 (restore dependencies to the User model)  $ git reset HEAD@{2} 

You got your day back! :)

like image 105
ken Avatar answered Oct 17 '22 05:10

ken


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

like image 33
sehe Avatar answered Oct 17 '22 04:10

sehe