164

I am new to Git and I seem to have one branch too many if I execute the following command:

warning: refname 'HEAD' is ambiguous.

I get the following output:

warning: refname 'HEAD' is ambiguous.
From github.com:dagda1/hornget
 * branch            master     -> FETCH_HEAD
warning: refname 'HEAD' is ambiguous.
warning: refname 'HEAD' is ambiguous.

If I execute git branch -a

I get the following:

HEAD
* master
remotes/emmekappa/master
remotes/origin/HEAD -> origin/master
remotes/origin/master

I am confused by the remotes/origin/HEAD -> origin/master.

What is this and how can I get rid of the ambiguous branch.

I got to this stage by performing a merge where I think I added the ambiguous branch.

user229044
  • 232,980
  • 40
  • 330
  • 338
dagda1
  • 26,856
  • 59
  • 237
  • 450
  • 11
    What is the first command you try to execute? Surely it is not `warning: refname 'HEAD' is ambiguous.` – Ben James Nov 07 '09 at 12:41
  • 1
    `remotes/origin/HEAD -> origin/master` is not a problem: it just shows which branch is default in remote `origin`. The branch named **`HEAD`** is. – Jakub Narębski Nov 07 '09 at 16:19
  • 3
    I think you need to edit the question to show the command you where trying to execute :) – Kris Aug 17 '10 at 09:52
  • To reproduce this, run "git fetch origin HEAD:HEAD". Apparently it tries to merge the current branch into a new branch called, literally, HEAD. – Brain2000 Jun 16 '20 at 05:04

5 Answers5

254

The problem is that you have a branch called HEAD which is absolutely dangerous, since that's the symbolic name for whatever branch is the current branch.

Rename it:

git branch -m HEAD newbranch

then you can examine it and decide what to do (delete it, or save under a descriptive branch name)

(The origin/HEAD remote branch is not a problem)

u0b34a0f6ae
  • 48,117
  • 14
  • 92
  • 101
  • This can also happen if you happen to have a file or directory with the same name as a branch. – Matt Connolly Jun 14 '11 at 11:55
  • 2
    While `origin/HEAD` might not be a problem, if you accidentally create a branch called `origin/somebranch` that IS a problem (and will result in the same "ambiguous" error message). When you try to pull from "somebranch", you'll wind up pulling from your local (accidental) branch rather than the remote. In that case, I found that flickerfly's suggestion of doing `git branch -d origin/somebranch` works great. – machineghost Oct 25 '12 at 00:46
  • That fixed it, thanks. Worth to note, the "HEAD" branch is created automatically, not by me. It contained a single merge commit and I failed to revert to a clean state using reflog. So, I renamed the branch, deleted it, rebased my "1 ahead" local master to the origin/master, pushed without a problem – Yunus Nedim Mehel Feb 10 '15 at 07:51
  • 3
    The same can happen for tags, not only branches. – sebix May 09 '17 at 13:17
  • I restarted the App I was using and no luck. In desperation, I turned my computer off and back on and all is fine. I'm going to the pub, this shit is crazy. –  Oct 03 '17 at 15:14
  • 1
    hah! i work with robots which have a head and had created a branch named `head` (lowercase). – orion elenzil Mar 01 '18 at 21:17
  • For me, a series of mistakes produced a tag `HEAd`, which produced this problem from `git rev-parse --abbrev-ref HEAD`. The match here is definitely case-insensitive. – Steven Kalt Jan 22 '19 at 15:35
  • I don't have a 'head' branch on my local system. I still get the same ambiguous warning. Why would that be? – mantri Jan 27 '21 at 08:26
  • @mantri - check tags if they contains "head" ```git tag | grep -i head``` – Arnis Juraga Feb 16 '22 at 11:54
  • I get the same error but don't have branch HEAD local nor remote – Macilias Feb 19 '22 at 22:53
  • Use `git show-ref` or `git for-each-ref` to dump all refs. Also, look in the Git directory (`ls .git` or similar) to see if there's some stray file that got created that should not exist - I had this happen once, and I still don't know what created the file. (This was a different case, where I got an ambiguous name `master` warning.) – torek Feb 20 '22 at 02:12
41

Also, this will delete the branch, if you just don't want it.

git branch -d HEAD

Use a capital -D to force the deletion:

git branch -D HEAD
zb226
  • 9,586
  • 6
  • 49
  • 79
Josiah
  • 2,666
  • 5
  • 30
  • 40
40

If you have created a tag named HEAD using...

git tag HEAD

...you can just delete that tag using:

git tag -d HEAD

See this case: kerneltrap.org/git-tag HEAD

zb226
  • 9,586
  • 6
  • 49
  • 79
Bengt
  • 14,011
  • 7
  • 48
  • 66
  • This happens when you add tag with name HEAD by mistake, so you have to delete it as suggested here – user1610308 Apr 24 '19 at 07:07
  • solves my problem! there's no branch named HEAD in my repo, don't know how is this tag created.. – mzoz Oct 02 '20 at 07:42
4

This means that you have a branch named "head". I had the same issue, I solved by doing the following command.

git branch -d head
4

Check references available in your git repository. You will observe two HEAD in your repository. This makes your branch with refname HEAD ambiguous.

git show-ref

Solution:

  • Rename the branch

    git branch -m HEAD <new_branch_name>
    

    OR

  • Delete the branch

    git branch -d HEAD
    
m00am
  • 5,910
  • 11
  • 53
  • 69
Darshan
  • 119
  • 1
  • 4