Previously, I’ve blogged about how to get uncommitted changes off of master onto a new branch and I’ve blogged about how to get committed changes off of master onto a new branch from command line. Today’s tip fixes the oh no! I’ve been committing on master this entire time from within Team Explorer, thanks to blog reader Luke Kolodziej
Let’s say you’re motoring along and you realize that you’ve been checking into master (or some other wrong branch) this entire time.
Don’t delete your .git folder! There’s a better way!
First, create a branch but do not check it out!
Go to Team Explorer – Branches, then Right Click on master and select New Local Branch From. Now in the TE window, give your new branch a name, but uncheck the checkout branch.
Click Create Branch.
Now you’ll go back to your History – Master tab and do a Reset – Delete Changes (–hard) on 2nd to last commit. We’re defaulting to –hard because we have no uncommitted changes in working directory or in staging.
Now hit Refresh on the History – Master tab and you’ll see that your commits are no longer on master.
But where did this commit go?
Remember the visualization from the previous tips. Just because we rolled back the HEAD pointer doesn’t mean the commit is lost. Remember git reflog holds the truth!
Okay, now if you switch to the newline branch (e.g. using the status bar button at the bottom right), and going to View History (either from Branches – Actions – View History or from Status bar – branches list – View History)
I still to this day feel freaked out that this works. One day I’ll be able to conceptualize master, HEAD, branches, etc as pointers versus the other way around (i.e. commits suddenly disappearing if they don’t show up in the git history). One day.