149

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
  • 18,845
  • 10
  • 77
  • 85
bsky
  • 19,326
  • 49
  • 155
  • 270

2 Answers2

268

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
  • 27,446
  • 10
  • 49
  • 54
  • 102
    Yes, but slightly simpler is `git checkout -b my-new-branch`. – torek Mar 13 '14 at 00:05
  • 32
    @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
8

You can also use:

git switch -c <new branch name>
Roy Salazar
  • 81
  • 1
  • 2