Search code examples
gittortoisegit

After reset, recover branch that was committed but not pushed


I started a new branch from a commit that had been pushed (call it commit A) and went on to make changes and commit about three times to this new branch (call them B, C and D) without pushing. Then I spotted a bug that I couldn't immediately see the source of and I decided I wanted to go back to commit A to see if the bug had been present at that point.

I'm using TortoiseGit, so I found commit A and right clicked it. I know now that the correct command to just temporarily use the project as it was at that point would have been "Switch/Checkout", but I mistakenly chose "Reset [branch name] to this".

Thankfully I did a mixed reset as opposed to a hard one which meant I still had the changes implemented in commits B, C and D in my working directory. I committed and pushed these for safety after realising my error.

In the TortoiseGit log, commits A, B and C are no longer visible. It's not the end of the world because the progress I made has been condensed into the one commit I did after the reset. But still, I'd like to recover these commits because they were more incremental than the one big and had descriptive messages. Is it possible?

I'm fairly new to git and have only used the TortoiseGit interface so far. I'd prefer a solution using that, but will use the command line if necessary. Thanks a lot.


Solution

  • Did it using 'Reflog'. To do it in TortoiseGit, shift+right click in the directory (you have to hold shift to get an extended menu). Go to TortoiseGit -> Show Reflog and all previous commits will be shown. From there I was able to switch to my lost branch.

    Thanks to the people who told me about reflog.