Is it possible to revert the undo changes done in Visual Studio -> Team Explorer -> Git?
Asked Answered
E

1

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.

Esperance answered 8/7, 2019 at 12:36 Comment(3)
Did you commit your work at any point during these days of work?Jonejonell
@RomainValeri No, unfortunately noEsperance
If so, out of local storage features your IDE might have, I'm afraid you won't be able to recover your files through git.Jonejonell
I
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://mcmap.net/q/12488/-in-git-how-can-i-recover-a-staged-file-that-was-reverted-prior-to-committing

Interlinear answered 8/7, 2019 at 14:49 Comment(9)
Be carefull with GitExtensions it will "destroy" your .gitconfig and developers are not concerned at all.Sequestrate
@Sequestrate 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 youHae
@wischi, it got rid of my proxy entries, is there another issue in githup where I can send you an example?Sequestrate
@Sequestrate I guess the simplest way would be directly on the issue you mentioned or on pastbin and comment it here.Hae
"Recover selected objects" - to which location will they be recovered?Pseudocarp
@Pseudocarp 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_1Interlinear
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'.Pseudocarp
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...Iguana
@Iguana 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...Interlinear

© 2022 - 2025 — McMap. All rights reserved.