Is there any way to recover uncommitted changes to the working directory from a git reset --hard HEAD
?
相关问题
- Why does recursive submodule update from github fa
- Extended message for commit via Visual Studio Code
- Emacs shell: save commit message
- Can I organize Git submodules in a flat hierarchy?
- Upload file > 25 MB on Github
相关文章
- 请教Git如何克隆本地库?
- GitHub:Enterprise post-receive hook
- Git Clone Fails: Server Certificate Verification F
- SSIS solution on GIT?
- Is there a version control system abstraction for
- ssh: Could not resolve hostname git: Name or servi
- Cannot commit changes with gitextensions
- git: retry if http request failed
You cannot get back uncommitted changes in general.
Previously staged changes (
git add
) should be recoverable from index objects, so if you did, usegit fsck --lost-found
to locate the objects related to it.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]
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**
answer from this SO
You got your day back! : )
I accidentally ran
git reset --hard
on my repo today too while having uncommitted changes too today. To get it back, I rangit fsck --lost-found
, which wrote all unreferenced blobs to<path to repo>/.git/lost-found/
. Since the files were uncommitted, I found them in theother
directory within the<path to repo>/.git/lost-found/
. From there, I can see the uncommitted files, 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.if you accidentally hard reset a commit, then do this,
assuming
HEAD@{2}
is the state you desire to go back toBy 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.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.git diff
. Save the o/p in a file called diff.patchpatch -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.