In git, a branch is basically a named reference to a commit that is automatically updated, for example when you do git commit
.
(and a tag is a named reference to a commit that stays the same)
And "HEAD" is a reference to what is currently checked out.
So when you tell git checkout mybranch
, it makes the "mybranch" reference current, so if you did a commit you'd update that. The "HEAD" reference is also updated to that branch.
When you do git checkout a9993d60c300f
, that's not a branch, that's a simple commit. Notably, there isn't a 1-to-1 mapping from commits to branches - you even have multiple branches pointing to that commit here.
Now, git could ask you if you meant these branches pointing to that commit, but instead it does the easy thing and gives you just the commit, and points the HEAD to that commit. Because you are now no longer attached to a branch, this is called the "detached HEAD" state.
When you do git checkout HEAD
, git follows the HEAD reference, sees a branch and checks out the branch. If you first checked out a commit and then did git checkout HEAD
, it would still give you the commit, and you would still have a detached HEAD.
And when you make a commit in detached HEAD state, git creates the commit and moves the HEAD reference, but no branch is pointing at it.
So this would be a "branch" if you looked at your commit graph, it just wouldn't be one to git because it doesn't have a name. There's no formal branch referencing it.
You could then create a branch, for example with git branch foo; git checkout foo
(or the shorthand git checkout -b foo
).