Boumbles
Boumbles

Reputation: 2523

Our git repository has a branch called HEAD

I recently noticed that our git server has a branch called HEAD. I've tried doing this locally and git warns me that this is ambiguous. Are there any potential horrible problems we could encounter by deleting/renaming this branch?

Upvotes: 7

Views: 2788

Answers (3)

ctrueden
ctrueden

Reputation: 6982

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.

Upvotes: 2

Bert F
Bert F

Reputation: 87493

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.

Upvotes: 2

user229044
user229044

Reputation: 239220

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

Upvotes: 6

Related Questions