suku
suku

Reputation: 10937

Recover accidently discarded uncommitted files

I accidentally discarded all my uncommitted changes. I did git reflog show.

f1bb72c HEAD@{0}: discard: [1d5f4f1a481165c24a46addbef7d74a5de3ff9c6]

How can I recover it?

Upvotes: 1

Views: 1364

Answers (3)

Piroxiljin
Piroxiljin

Reputation: 621

Based on @Stanislav Bashkyrtsev answer I could propose the folowing command lines.

Iterate over all git objects and search all git-tree entries where my file was included

for /F %A in ('dir /b .git\objects\* ') do for /F %B in ('dir /b .git\objects\%A\*') do (git cat-file tree %A%B | findstr <MY_FILENAME> ) && echo %A%B >> found.txt

Content of found.txt

18421274e7fd0938bae3f5132ac7779cbd22b21f 
44b488f6f30042cbbd7675419461afda234260a0 
650552061edc4907a9e5983afadba1f8e61b9687 

Then I check every entry with git cat-file -p <TREE_HASH> and found hash of blob with content of files

λ git cat-file -p 18421274e7fd0938bae3f5132ac7779cbd22b21f
100644 blob a108783cf61693824e9b62d16211cad1e85f68d0    Components.xml
100644 blob d1329eb64792abdbd919b9b7336e0feaa9452a87    TS_ButtonItem.dgx
100644 blob ce9c4fbd5c32f2691c8d15b90b9416c3e1afeb2c    TS_ButtonItemBase.dgx
100644 blob 0cc665b0fa8cdcc7c5ce4f51e4c47f9ca540c72d    TS_CheckBox.dgx
100644 blob f696ad9bb7ec7b963593ea8fded4fe8acd0167c9    TS_IconButtonItem.dgx

After that again git cat-file -p <BLOB_HASH> and check the content of file

Upvotes: 0

LuFFy
LuFFy

Reputation: 9307

If you had not commited, staged, or stashed the changes you made, there is no way you can recover those changes.

EDIT: Recovering lost changes. Adding this on Mark Longair's suggestion (in the comment). This also includes a couple of SO links from his answer below(*), that I found quite informative.

  • If you have ever committed some change and have lost that commit (like committing in a detached state), you can find that commit using reflog. See this SO question*.

  • If you have lost your last staged changed, you can also recover that. See this SO question*. (I have never used or tried it myself).

  • If you have stashed a change, you can also recover that using pop or apply. (I am not sure if the popped/dropped stashes are also recoverable that were not committed). You may find this Recover dropped stash in git useful.

If there are any other methods that anyone can suggest, I'd edit this answer further to add them.

Upvotes: 0

Stanislav Bashkyrtsev
Stanislav Bashkyrtsev

Reputation: 15318

If the files were not added to Git at all (if you didn't do git add), then Git doesn't know anything about those files at all. So you won't be able to resurrect them.

If you added the files to Git but accidentally removed them and updated the Index to the current state of the directories, you'll have hard time finding your files. The problem is - while the files are in Git there are no references to them from your current history, all you have is the number of zipped git objects.

But if those files were so precious to you, I'd do this:

  • Find all files in .git/objects directory that were created between now and some known date when you added them (ignoring info and pack dirs).
  • Go through each git object (concatenating 2 symbols of directory and 38 symbols of the file) and do git cat -p [hash] until you find your blob. You can use grep if you remember some unique words from those files.

Upvotes: 1

Related Questions