Our git repository has a branch called HEAD

Your server should have a branch pointer called HEAD, which will point to your default branch. By default, git branch -r will show you this:

origin/HEAD -> origin/master

It is normal for a bare repo to have a 'HEAD'. Keep in mind that HEAD is not a normal branch, but rather it is a pointer to a branch.

  1. For a non-bare the 'HEAD' "branch" is points to the checked out branch.
  2. For a bare repo, it points to the default branch, i.e. the branch checked out as the working dir when the bare repo is cloned to a non-bare repo. Often it points at "master", but you can point it to a different branch.

Creating a remote branch called HEAD is possible, and does not seem particularly harmful:

~/code/foo/bar (master) $ git push origin master:HEAD
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:foo/bar
 * [new branch]      master -> HEAD
~/code/foo/bar (master) $ git branch -a
* master
  remotes/origin/HEAD
  remotes/origin/master
~/code/foo/bar (master) $ git push origin :HEAD
To [email protected]:foo/bar
 - [deleted]         HEAD
~/code/foo/bar (master) $ git branch -a
* master
  remotes/origin/master

Creating a local branch called HEAD has nastier effects:

~/code/foo/bar (master) $ git checkout -b HEAD
Switched to a new branch 'HEAD'
Your branch is up-to-date with 'origin/master'.
~/code/foo/bar (HEAD) $ git checkout -b fubar
warning: refname 'HEAD' is ambiguous.
fatal: Ambiguous object name: 'HEAD'.
~/code/foo/bar (HEAD) $ git branch -a
* HEAD
  master
  remotes/origin/master
~/code/foo/bar (HEAD) $ rm .git/refs/heads/HEAD
~/code/foo/bar (HEAD*) $ git checkout master
Switched to branch 'master'

All of the above was with git version 2.3.0 installed via Homebrew on OS X.

It is easy to type many of the above invocations by accident, and unfortunately Git does not fail fast when HEAD (or FETCH_HEAD) is specified in a place where the special meaning does not apply. Edit: Newer versions of Git do now fail fast with HEAD. For example, with git version 2.22.0:

~/code/foo/bar (master) $ git checkout -b HEAD
fatal: 'HEAD' is not a valid branch name.

But a branch called FETCH_HEAD is still allowed.

Tags:

Git

Git Branch