Git: Undo last commit

Published:

Sometimes I forget to create a new feature branch, and accidentally commit to master. Keep having to look up how to fix it...

# From master, create new branch, which will include the accidental commits
git branch feature/foobar

# Move master HEAD back, which will remove the commits from there
git reset --hard HEAD~1

# Checkout feature branch, where the commits should still exist
git checkout feature/foobar