Hello, my name is: Amy

Oops! I Commited to Master

Sometimes with git I get a bit too heavy on the auto pilot and will only realize I've done something terible after I hit the enter key. Luckily git is fairly forgiving like that. The other day my problem was that I committed a bunch of changes to master that definitely shouldn't have been.

Here's the escape hatch:

(master)$ git branch freshbranch    # create a new branch of the current state
                                    # your latest commit is moved to safety
(master)$ git reset --hard HEAD~1   # bye bye commit from master
(master)$ git co freshbranch        # switch to new branch
(freshbranch)$                      # yaay! my work is safe

git reset

git reset moves the current checked out position to the state that you tell it.

In this case, we are also specifying --hard which means that instead of going into a detached HEAD state it will discard any of the changes you have made to the files that git tracks.

We can use --hard because we have already moved that work to a new branch.

HEAD~1 is referring to the previous commit.

As per usual, Stack Overflow Post a big thanks to this for pointing me in the right direction.