97

While working on Master branch, I forgot to create new branch. Made changes to files then inadvertently reverted to the master, loosing all updates. I didn't commit the updated files.

How can I retrieve them?

Akshay
  • 926
  • 9
  • 21
Trist
  • 1,306
  • 1
  • 11
  • 17
  • In a comment below you mention being able to recover your work via the stash, as saved automatically via the GitHub GUI. I haven't seen the GUI in action, and have always been worried about losing stuff, wondering what warning messages look like etc. Can you edit your question to clarify what actually happened a bit more so we can all benefit? – nealmcb Mar 12 '12 at 02:04
  • 2
    If you are using any JetBrains software, right click and go to "Local History > Show History". From here, you should see a list of local edit history separate from the Git history. You can choose any version to revert to. – sbrk Sep 08 '21 at 23:28

7 Answers7

111

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 changes, 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.

Mike G
  • 4,232
  • 9
  • 40
  • 66
Sailesh
  • 25,517
  • 4
  • 34
  • 47
  • 5
    Your `stash` tip led me to find the right course of action. Many thanks indeed – Trist Aug 22 '11 at 13:23
  • 2
    You might want to edit your answer to point out that you can also get back files if they'd only been staged, since they're still put into the object database by that operation. (See the second paragraph of my answer.) – Mark Longair Aug 22 '11 at 13:41
  • 27
    Some IDEs keep track of local file history, quite independently of source control. – Konrad Morawski Nov 07 '13 at 14:34
  • I use Github Desktop and I swear it dropped my uncommitted changes. Luckily it runs a regular stash that I didn't know about. `git stash apply` saved my life. Thank you for this!! – winston Mar 08 '17 at 18:40
  • 1
    @KonradMorawski: I had forgotten about Android Studio's file history feature in my panic. You just saved my life, sir! Many, many thanks. – suomi35 Mar 29 '17 at 04:15
  • @suomi35 yeah isn't that a neat feature! It's awful to lose one's work, I'm glad my hint was of help to you (and several years after I posted it) : ) Happy coding – Konrad Morawski Apr 05 '17 at 08:43
  • 1
    If you are working on a web project and happened to load the file in question in your browser, a copy of that file might still be available in the cache (don't reload the page or you'll lose it!) I was able to recover a lost file using this way. See http://www.sensefulsolutions.com/2012/01/viewing-chrome-cache-easy-way.html – JDB Oct 10 '17 at 15:05
  • 2
    I had been on this very same situation. When I was about to lose all my hopes, I realized that I _did_ have a compiled version of my recently modified project, because I runned it once to test the new feature. I then had the idea of decompiling my `.exe` file using [ILSpy](https://github.com/icsharpcode/ILSpy). It wasn't perfect, but I was able to fully recover all of my changes without having to restart coding everything from the beginning. – Pedro Corso Jan 16 '19 at 18:20
  • 1
    @KonradMorawski Thanks! You saved my day. I was accidentally deleting uncommitted branch. I almost crazy because those deleted branch contains 100+ files with thousands of lines of code. Those literally my work for a month. I forgot that Android Studio does have local history of files. – Wachid Susilo Dec 29 '22 at 16:27
59

Two longshots: Some IDEs, such as Delphi, keep an editor history. You may have some recourse there.
Next, if you had your local working directory located in the MyDocuments folder, it may have been automatically backed up by Windows Home Server, Carbonite, MozyPro, etc.. these are usually 'set it and forget it'. maybe you forgot it?

Chris Thornton
  • 15,620
  • 5
  • 37
  • 62
  • 13
    Eclipse calls this feature "local history". You might be able to recover even files that have not been staged, stashed or committed. – Mikko Rantalainen Nov 22 '13 at 13:05
  • 1
    Thank you for putting the idea into my head!! I was able to recover "deleted" files in the Dropbox web interface! You saved my bacon. [Recover deleted files in dropbox](https://www.dropbox.com/help/296/en) – lyonsinbeta Mar 20 '14 at 05:57
  • 4
    I did the same stupid mistake on Eclipse. I did right click->Restore from local history... on the changed file which showed no history. But when I do right click->replace with->local history... I see a list of changes. That saved me. Hope this helps someone. – Rahul Dabas Aug 28 '14 at 19:32
  • 5
    PHPStorm has a VCS -> Local History for individual files that are independent of Git. – theGreenCabbage Jan 29 '15 at 07:16
  • Command+Z on PyCharm saved my two days worth of work :) – Farhan Haider Feb 03 '22 at 13:27
  • So glad I had VS Code editor open, and could press ctrl-z to restore open text file content! – trusktr Mar 29 '22 at 19:25
  • Thanks! I was able to restore some hours of work in pycharm using [Local History](https://www.jetbrains.com/help/pycharm/local-history.html#restore-changes-from-local-history) – Cristian F. Jun 12 '22 at 09:02
  • There used to be a Visual Studio extension called Local History that did the same thing but it has never been upgraded to 2020 unfortunately – msteel9999 Dec 19 '22 at 09:17
41

I was stuck in the same situation.

Had a list of the changed files in the terminal before the --hard reset, went into my editor (Sublime 2, doesn't matter though), opened each file and hit cmd+z (undo) once, this effectively undid the changes done by the hard reset and I got my uncommitted changes back :)

Vishu
  • 787
  • 5
  • 9
13

The critical question here is what you did after making changes to the files. If you created a commit which contained the new state of the files, then you should be able to get them back by looking through the recent entries in git reflog, finding the SHA1sum of the commit and then creating a new branch from that with git branch recovered <SHA1sum>, or similar. There's an example of doing this in this answer.

If you did git add on any of the files to stage them, you should also be able to get them back, but this is rather more work - Jakub describes how to do this in this answer.

If you happened to do a git stash to give yourself a clean status, then of course you can get it back as you would any other stash.

Otherwise, I'm afraid that the news is not good.

I hope it's not infuriating to point this out post-hoc, but to just switch back to the master branch, you shouldn't have needed to use any command that might lose you data - git checkout master would have told you that you were already on the master branch, and show any uncommitted changes. (Arguably git reset --hard should have a "Yes, I really mean this" confirmation if there are uncommitted changes, given how often people^WI lose data this way.)

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • bash command line didn't ask me anything when I did `git reset --hard HEAD~1`. All unstaged uncommitted changes were gone. No questions asked. – Robert Koritnik Jan 26 '17 at 21:16
  • 2
    @RobertKoritnik I think you might have misunderstood the last sentence of my answer - I was saying that an "are you sure?" confirmation when `git reset --hard` might lose data *would be* a good feature, not that that is what git does. – Mark Longair Jan 31 '17 at 15:31
3

Use the reflog. git reflog will show you a history of all the commits you have been on, in chronological order.

If you lost your changes by 'checking out master', then you were probably working headless. git status will tell you if you are working without a head. (As does git branch).

Working headless isn't that bad (I do it all the time, deliberately), but you will have a greater reliance on the reflog.

If you didn't commit your changes, in any way, however, then there is no way to retrieve those files. The only realistic way this could have happened is if you did a hard reset, or explicitly forced a checkout. Do not force changes unless you are sure that you are comfortable with losing data.

Usually, in git, 'forcing' is done by specifying -f.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Arafangion
  • 11,517
  • 1
  • 40
  • 72
  • Thanks for quick reply, couldn't find ref in reflog, but more than likely I was doing it wrong. Used GitX and found a stash, popped that. – Trist Aug 22 '11 at 13:06
2

I've done the same thing, more than once unfortunately. Without committing anything git has no idea what you wrote. Even if you did commit then when backward I'm not sure reflex would help.

matthewdaniel
  • 1,446
  • 12
  • 16
  • 1
    Thanks for quick reply, I'd been trying out the GitHub GUI, silly thing to do on production stuff but there you go. It seems that it carried out a `stash` before I inflicted the damage. – Trist Aug 22 '11 at 13:09
  • @Trist This is what did it for me. I was about to curse GitHub for making it so easy to switch branch without committing (and no visible stash function in the GUI) when I saw your comment. `git stash apply` saved the day. – John Aug 29 '16 at 18:46
1

Most IDEs have a "Local History" feature, including all JetBrains products. You could also try ctrl + Z in all the affected files.

As a last resort, you can check Time Machine backups / any other backup software that runs periodically

Alec
  • 8,529
  • 8
  • 37
  • 63