119

I tried something like this:

git branch temp

to create a new branch but don't move the HEAD. But I get:

# Not currently on any branch.

I don't want to merge anything, I just want a new branch at the current HEAD.

Paulo Mattos
  • 17,605
  • 10
  • 69
  • 79
bsky
  • 17,746
  • 43
  • 135
  • 250

2 Answers2

223

You're sitting on a detached HEAD:

git checkout <sha>

You want to make a branch at that commit:

git branch my-new-branch

And now switch to that branch:

git checkout my-new-branch
redhotvengeance
  • 26,026
  • 10
  • 48
  • 54
  • 89
    Yes, but slightly simpler is `git checkout -b my-new-branch`. – torek Mar 13 '14 at 00:05
  • 26
    @torek Yeah, that shorthand is quicker. I broke it into separate pieces because it looked like the OP was expecting `branch` to not only make the branch, but switch to it too. I wanted to make clear the difference between `branch` and `checkout`. – redhotvengeance Mar 13 '14 at 00:13
  • 10
    That shorthand is quicker to type, but slower to remember. I like the solution given. – Dogweather Nov 02 '14 at 06:08
3

You can also use:

git switch -c <new branch name>