Emacs Feature Branch Tip

Like many of us, I like to work on feature branches. Branch, commit-commit-commit, push, pull-request. It's clean and easy.

But sometimes I forget a step and screw my workflow up. I get to the end of a round of commits and realise I forgot to git branch before I started coding. Now all my work is on local master.

That's fixable, but it's a pain. You've probably been there, and there are two solutions:

  • Create a new branch, stash, reset master to origin/master, checkout the new branch and pop the stash.
  • Or git push origin master:feature, and hope that origin/master doesn't move forward before the feature gets merged.

The former's a pain and the latter relies on luck. Neither is ideal.

Step forward Magit, the premium Emacs git client. magit-branch-spinoff automatically puts you back where you want to be. It checks out a new branch based on your current HEAD, and resets master back to origin/masterdocs. So, so easy.

Unsurprisingly, it's available from a M-x near you, or from Magit's branch menu.

Footnotes


  1. You won't ben surprised to learn it's a bit more flexible than that, if you need it to be. See the docs.