104

I saw a few people asking the same question on here but it seems none of their advice is applicable to me. I'm getting the warning that is in the title of this but I don't have any tags named "master". This is the result of git branch -a:

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

Any idea what could be going wrong here? I've only been using git for a few months now, so it mostly just worries me that this ambiguity might mess with the repo in the future.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Tim
  • 6,265
  • 5
  • 29
  • 24
  • 2
    what command issues the warning? – CharlesB Oct 25 '12 at 16:33
  • 1
    Would the list of possible 'master' mentioned in http://stackoverflow.com/a/12225704/6309 help? A `git checkout heads/master`should work (without warning) – VonC Oct 25 '12 at 16:36
  • 2
    Please show the output of `git show-ref`. – CB Bailey Oct 25 '12 at 16:44
  • @CharlesBailey Sorry for the delay here, but the output of `git show-ref` is a bunch of stuff that wouldn't fit here. I believe it might be relevant that there are two lines that end in "master": refs/remotes/origin/master, refs/heads/master – Tim Nov 02 '12 at 18:26
  • possible duplicate of [Git: refname 'master' is ambiguous](http://stackoverflow.com/questions/12224773/git-refname-master-is-ambiguous) – K-ballo Jan 15 '13 at 22:07

7 Answers7

114

For me I tracked down the source of this warning to much earlier when I incorrectly issued an "update-ref" command. If you forget to specify the full refs/heads/mybranchname path in the first arg, then a file .git/mybranchname gets created, which later leads to this warning when you try to switch to that branch.

It is solved by simply deleting the .git/mybranchname, eg:

rm .git/master

And for reference, the correct form for the update-ref command is:

git update-ref refs/heads/mybranchname mytargetbranch

Don't forget the "refs/heads" part!

Also, my most common use-case for update-ref is simply manually moving a branch to point to another commit, which I've found a simpler syntax to do:

git branch -f myBranchToMove destinationBranchOrHash

This syntax is easier for me because it doesn't require that error-prone refs/heads path qualifier.

Magnus
  • 10,736
  • 5
  • 44
  • 57
  • 4
    I did the exact same thing. The command didn't seem to do anything, though, so I added the "refs/heads" part and didn't realize it had left residue in the .git directory. – John Alexander Nov 12 '14 at 19:17
  • `git update-ref -d master` also works, once you realize it's the update-ref command that created the duplicate ref. – Dan Berindei Jul 20 '15 at 10:43
  • 2
    If you only want to move a branch between commits it is preferred to delete the branch (branch -d) and re-create it on the other commit (checkout -b). That does not require any fiddling with refs. – Ytsen de Boer Feb 05 '18 at 11:28
32

As detailed in "Git: refname 'master' is ambiguous", that means that, beside heads/master, you have another master in one of the following namespace within the git repo:

refs/<refname> 
refs/tags/<refname>
refs/heads/<refname>
refs/remotes/<refname>
refs/remotes/<refname>/HEAD

Or even ./<refname>, as mentioned in Magnus's answer.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
27

For future reference, I had the same issue and what ended up to work for me was the solution described here. Basically, when you get Git: warning: refname 'xxx' is ambiguous warning, you can use: git show-ref xxx to check the references to xxx branch and validate which ones are having conflict.

In my scenario, it was a tag named xxx and branch name with the same name. The tag had been made by mistake and was removed from server so all I needed to do was to update my local tag to match the server: git fetch -p -P. This command is explained in details here

manman
  • 4,743
  • 3
  • 30
  • 42
9

I had a similar problem (not master) when I created a branch with the same name as a tag.

This helped me remove the tag https://stackoverflow.com/a/5480292/150953

git push --delete origin tagname
Scott Warren
  • 1,581
  • 1
  • 17
  • 30
8
git fetch --prune

git pull origin branch-name

should fix your problem.

clemens
  • 16,716
  • 11
  • 50
  • 65
山茶树和葡萄树
  • 2,050
  • 1
  • 18
  • 18
4

Check your .git/config file. You may run into this problem if you have more than one remote repo configured with the same:

fetch = +refs/heads/*:refs/remotes/origin/*.

The other remote server should have a different name, eg.:

fetch = +refs/heads/*:refs/remotes/another_repo/*

boryn
  • 726
  • 9
  • 10
  • 2
    Whoa... real necromancer here. I asked this 8 years ago... back in the days when I was still figuring out what the hell git was. I had probably twisted my commit tree into a knot. Who knows what it was. Thanks for still offering new avenues of investigation to those in the future! – Tim Mar 04 '21 at 15:27
1

I got that error after creating a new branch locally with the same name as an existing one on remote.

The error was something like this

warning: refname <<branch-name> is ambiguous 

I had to (force) delete the local branch and then create a new one with a different name to the remote existing one, like this:

$ git branch -D <local-branch>
Leonardo
  • 1,501
  • 2
  • 9
  • 6