Accidentally reverted to master, lost uncommitted changes

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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?

Solution 3 - Git

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 :)

Solution 4 - Git

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.)

Solution 5 - Git

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.

Solution 6 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTristView Question on Stackoverflow
Solution 1 - GitSaileshView Answer on Stackoverflow
Solution 2 - GitChris ThorntonView Answer on Stackoverflow
Solution 3 - GitVishuView Answer on Stackoverflow
Solution 4 - GitMark LongairView Answer on Stackoverflow
Solution 5 - GitArafangionView Answer on Stackoverflow
Solution 6 - GitmatthewdanielView Answer on Stackoverflow