Max
Max

Reputation: 22315

Refname 'master' is ambiguous

I've looked at all of the other ambiguous refname questions and none of them seem to help. Why am I getting this warning?

$ git checkout master
warning: refname 'master' is ambiguous.
$ git show-ref master
eef61c00da690f093063ac5a728e22fd21648104 refs/heads/master
$ git branch -a
  checkers
  exercises
* master
$ git remote -v
$ 

Upvotes: 75

Views: 82563

Answers (4)

VonC
VonC

Reputation: 1323343

TL;DR: save and delete the tag, as Ashutosh Jindal comments (see "Rename a tag in git?"):

git tag tag-master master
git tag -d master

Original answer:

Most of the sources I see (like this FAQ) point to the same cause:

When you try to checkout a local branch, you get a

warning: refname 'branch-name' is ambiguous

This can happen if you've created a local branch with the same name as a remote tag.
Git should be checking out your local branch, but instead it's trying to checkout the tag, and it gets confused.

The initial import of several trees were problematic, since they contained identically named branches and tags. We have since addressed a lot of these issues, by renaming away the tags.

In your case, you don't have a remote, but local tags named like your branch could be enough.

See also idbrii's answer, which includes using git show-ref:

git show-ref | grep master

The ambiguity is specified in gitrevision

<refname>, e.g., master, heads/master, refs/heads/master

A symbolic ref name. E.g. master typically means the commit object referenced by refs/heads/master.
If you happen to have both heads/master and tags/master, you can explicitly say heads/master to tell git which one you mean.
When ambiguous, a <refname> is disambiguated by taking the first match in the following rules:

If $GIT_DIR/<refname> exists, that is what you mean (this is usually useful only for HEAD, FETCH_HEAD, ORIG_HEAD, MERGE_HEAD and CHERRY_PICK_HEAD);

  • otherwise, refs/<refname> if it exists;
  • otherwise, refs/tags/<refname> if it exists;
  • otherwise, refs/heads/<refname> if it exists;
  • otherwise, refs/remotes/<refname> if it exists;
  • otherwise, refs/remotes/<refname>/HEAD if it exists.

So check where master can be found in your repo.

And git checkout heads/master would always work.
Warning: by default, this would checkout the branch in a DETACHED HEAD mode. See "Why does git checkout with explicit 'refs/heads/branch' give detached HEAD?".

To avoid that, and still use an unambiguous ref, type:

git checkout -B master heads/master

Upvotes: 95

boryn
boryn

Reputation: 876

This message will appear as well if you had erroneously configured two remote servers with the same name which gives the ambiguity.

Check your .git/config file. If you have more than one remote repo configured with the same:

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

you should change one of them to a different name, eg.:

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

Upvotes: 1

Trebor Rude
Trebor Rude

Reputation: 1944

Although this doesn't apply to the OP's situation, I got myself a refname is ambiguous warning after accidentally doing a git branch origin/branch instead of a git checkout origin/branch. This created a local branch named origin/branch, which made it ambiguous with the remote branch. Solving the problem was as simple as git branch -D origin/branch (safe because -D operates on local branches).

Upvotes: 44

GaryO
GaryO

Reputation: 6338

This just happened to me. I somehow had a file .git/master containing a sha. Not sure how that got there, but when I deleted it, the error went away. If you read the accepted answer carefully, this is "expected behavior" but you won't see that .git/master if you do e.g. git show-ref master because it follows slightly different rules.

Upvotes: 26

Related Questions