6

I am using Visual Studio 2019 Community and accidentally clicked in undo changes in the Team Explorer tab for all the files staged and lost all my last days work. Is it possible revert the situation and get back the changed files?

I hope to have explained my problem well. Thanks in advance.

Fernando
  • 91
  • 1
  • 1
  • 5

1 Answers1

5

I am using Visual Studio 2019 Community and accidentally clicked in undo changes in the Team Explorer tab for all the files staged and lost all my last days work.

If you are sure that you have staged your files, the recovery of files content is possible, even if painful! For the file names, the data is lost, due to how git works (i.e. when you stage files, only blob objects are created and stored in git data store but not filetree objects --that stores the matching between content and filename).

If you have not staged your files, all is lost.

Otherwise, download and install Git Extensions.

Then, once your repository is opened in GitExtensions, go to the menu item: "Repository" -> "Git Maintenance" -> "Recover lost objects"...

There, uncheck "Show commits and tags" (that will check "Show other objects")

enter image description here

Now, when you double-click on a line, you should be able to get the content of the files created from the more recent to the oldest.

Good luck!!

lost all my last days work.

PS: for the future, you just learned the hard way that with git you must commit often (you will always be able to recover once your work is committed). And after that, learn how to rewrite your history or to amend the last commit. Learn how to use the reflog also

Other ways to recover staged files in another Stackoverflow question (using the command line): https://stackoverflow.com/a/58853981/717372

Philippe
  • 28,207
  • 6
  • 54
  • 78
  • **Be carefull with GitExtensions it will "destroy" your .gitconfig and [developers are not concerned at all](https://github.com/gitextensions/gitextensions/issues/4389).** – Luizgrs Nov 05 '20 at 01:58
  • @Luizgrs do you have a (handcrafted) .gitconfig that you could share that keeps getting messed up? I'm currently trying to fix that issue how GitExtensions handles gitconfigs and would greatly appreciate it if you could provide a sample, especially with non-obvious issues (like comments getting removed) - Thank you – wischi Nov 09 '20 at 20:31
  • @wischi, it got rid of my proxy entries, is there another issue in githup where I can send you an example? – Luizgrs Nov 11 '20 at 12:20
  • @Luizgrs I guess the simplest way would be directly on the issue you mentioned or on pastbin and comment it here. – wischi Nov 11 '20 at 19:24
  • "Recover selected objects" - to which location will they be recovered? – Gerard Jan 20 '22 at 10:17
  • @Gerard That will create tags (named `LOST_FOUND_n`) directly on the blob recovered and you will be able to see the content with the command `git cat-file -p LOST_FOUND_1` – Philippe Jan 20 '22 at 11:00
  • After 'recover selected objects' they disappear from the obejct list. The git cat-fil command you mention is perhaps advanced, allthough after git cat-file -p LOST_FOUNDx > myfile.cs one has the file on disk. Easier is to first select save files to lost-found! Luckily I saved my lost content manually before 'recover selected objects'. – Gerard Jan 20 '22 at 12:24
  • You, sir, have saved my day and most likely a few others. It's a shame Visual Studio doesn't ask for confirmation before letting you destroy a few days' work... – traxx2012 Apr 12 '23 at 21:57
  • 1
    @traxx2012 Understand that you should **never** have multiple days of work uncommitted. You should commit multiple times a day. After that, you have multiple options the clean the history before pushing/merging a clean commit history: squashing commits, reset soft or amending the last commit. **But commit often...** – Philippe Apr 13 '23 at 07:01