Recover from git reset --hard?
GitGit Problem Overview
Is there any way to recover uncommitted changes to the working directory from a git reset --hard HEAD
?
Git Solutions
Solution 1 - Git
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! :)
Solution 2 - Git
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**
Solution 3 - Git
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.
Solution 4 - Git
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.
Solution 5 - Git
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
Then all of my local files deleted because the repo was empty. I thought everything was gone.
This worked for me:
git reflog show
git reset HEAD@{1}
git push
Solution 6 - Git
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!
Solution 7 - Git
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.
Solution 8 - Git
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 rungit gc
. If you're 99.9% sure you'll never need these changes back, thengit stash
is still your friend for the 0.1% case. If you're 100% sure, thengit 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, thengit reset --keep
is your friend. It will do the same thing asgit 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.
Solution 9 - Git
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
Solution 10 - Git
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.
- scroll your terminal and look for the o/p of
git diff
. Save the o/p in a file called diff.patch - Search & Replace all 7 spaces and 8 spaces with tab(\t) character and save the changes.
- Go into your git repository. Apply the diff.patch (
patch -p1 < diff.patch
)
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.
Solution 11 - Git
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
Solution 12 - Git
IntelliJ has a temporary folder accessible through the history command:
- Select the folder to revert files from in your navigation pane
- Double tap the Shift key (shift-shift)
- In the input box that pops up type Local History and press Enter
- Select Show History
- Now you can revert to the version you need.
Solution 13 - Git
If you luckily had the same files opened on another editor (eg. Sublime Text) try a ctrl-z on those. It just saved me..
Solution 14 - Git
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!!
Solution 15 - Git
(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.
Solution 16 - Git
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.
Solution 17 - Git
For later you can use VSCode with ext: GitLens - Git supercharged so you can reverse your code with this extensions
Solution 18 - Git
You can only recover staged (git add
) changes that you have lost.
you can recover easily by running this command
step:1 Go to project root directory and then run this command
npx git-recover
step:2 enter recovery directory path like
/Users/apple/RecoveryDirectory
you will get lost file in RecoveryDirectory