日本語

Git “refname is ambiguous” warning: causes and fixes

​​Git​command line,Troubleshooting

warning: refname 'v1.0' is ambiguous. is the warning Git prints when a tag and a branch have the same name (here, v1.0). It happens when you create a fix branch with the same name as the tag you made for a release. If you write only v1.0, Git cannot decide whether you mean the tag or the branch. To fix it at the root, rename the branch with git branch -m, or delete the one you don't need. After I renamed the branch, git log v1.0 no longer printed the warning.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. For the remote repository, I used one created with git init --bare in the same environment. I did not test GitHub or other hosting services, or Windows and macOS. To keep the output consistent, I fixed the commit dates, so the commit hashes will differ when you try it yourself.

Creating a tag and a branch with the same name triggers the warning

This repository has the tag v1.0 on the commit used for the release, and then a later commit that fixes a bug.

console
$ git log --oneline --decorate
12e5fb7 (HEAD -> main, origin/main) 1.0 の不具合を修正
aac61e1 (tag: v1.0) 1.0 をリリース

When I ran git branch v1.0 here, the branch was created without any error or warning, even though a tag with the same name existed.

console
$ git branch v1.0; echo "exit=$?"
exit=0

$ git for-each-ref --format="%(refname) %(objectname:short)" refs/heads refs/tags
refs/heads/main 12e5fb7
refs/heads/v1.0 12e5fb7
refs/tags/v1.0 aac61e1

Listing the refs with git for-each-ref shows that the full names are refs/heads/v1.0 (the branch) and refs/tags/v1.0 (the tag), and they are different objects. The branch points to the latest commit, 12e5fb7, and the tag points to the released commit, aac61e1.

When I then ran a command with v1.0, this warning appeared:

console
$ git log --oneline v1.0; echo "exit=$?"
warning: refname 'v1.0' is ambiguous.
aac61e1 1.0 をリリース
exit=0

The warning means that the name v1.0 (the refname) is ambiguous: it is unclear which ref it refers to. The exit code is 0, and the command runs to the end without stopping. The output showed the history starting from aac61e1, the commit the tag points to. The latest commit 12e5fb7 at the tip of the branch did not appear.

The one Git picks depends on the command

After the warning, which ref was used depended on the command.

When "v1.0" is written, git log, git rev-parse, and git diff choose the tag refs/tags/v1.0 (aac61e1, the commit "Release 1.0"). git switch and git checkout choose the branch refs/heads/v1.0 (12e5fb7, the commit "Fix bug in 1.0"). git push origin v1.0 stops with an error without choosing either. The commit messages in this image are in Japanese; they mean "Release 1.0" and "Fix bug in 1.0."
Which one is chosen for each command when 'v1.0’ is written

git log and git rev-parse choose the tag

When I used git rev-parse to look up the commit that v1.0 points to, it returned the tag's aac61e1. git show did the same. When I wrote the full name refs/heads/v1.0, it pointed to the branch's commit.

console
$ git rev-parse v1.0; echo "exit=$?"
warning: refname 'v1.0' is ambiguous.
aac61e1f7a07ce91b58deca68fdbc56ec8496649
exit=0

$ git show -s --format=%s v1.0
warning: refname 'v1.0' is ambiguous.
1.0 をリリース

$ git show -s --format=%s refs/heads/v1.0
1.0 の不具合を修正

This is how Git is documented to behave. The official reference for revision syntax describes the order in which Git searches when a name is ambiguous. refs/tags/<name> is checked before refs/heads/<name>, so the tag is chosen.

git diff --stat main v1.0 also printed the warning and then showed the difference. main and the branch v1.0 point to the same commit, 12e5fb7. The one-line difference in VERSION appeared because the comparison was made against the tag's aac61e1.

console
$ git diff --stat main v1.0
warning: refname 'v1.0' is ambiguous.
 VERSION | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

git switch and git checkout choose the branch

On the other hand, git switch v1.0 and git checkout v1.0 printed the same warning and then switched to the branch v1.0. The content of VERSION was the one from the latest commit the branch points to (v1.0.1).

console
$ git switch v1.0; echo "exit=$?"
warning: refname 'v1.0' is ambiguous.
Switched to branch 'v1.0'
exit=0

$ git status | head -1; cat VERSION
On branch v1.0
v1.0.1

The history shown by git log v1.0 and the commit you land on after git switch v1.0 are different commits. If you ignore the warning, you end up working on a commit different from the history you checked.

git push stops with an error

git push origin v1.0 was not a warning but an error. The exit code was 1, and nothing was sent to the remote.

console
$ git push origin v1.0; echo "exit=$?"
error: src refspec v1.0 matches more than one
error: failed to push some refs to '../remote.git'
exit=1

$ git ls-remote origin
12e5fb71fd2547b556d86963c5d9c0770a1b7945	HEAD
12e5fb71fd2547b556d86963c5d9c0770a1b7945	refs/heads/main

The first line means that the v1.0 given as the source (src) matches more than one ref. During push, Git does not pick the tag as git log does; it stops with an error.

A quick workaround: write the full name

If you cannot rename right away, write the full name, such as refs/tags/v1.0 or refs/heads/v1.0, and the warning does not appear. As the official reference describes, you can also tell them apart by omitting the leading refs/, writing tags/v1.0 and heads/v1.0.

console
$ git log --oneline tags/v1.0; echo "exit=$?"
aac61e1 1.0 をリリース
exit=0

$ git log --oneline heads/v1.0; echo "exit=$?"
12e5fb7 1.0 の不具合を修正
aac61e1 1.0 をリリース
exit=0

Push also worked when I used the full names. The branch was sent as [new branch], and the tag as [new tag].

console
$ git push origin refs/heads/v1.0; echo "exit=$?"
To ../remote.git
 * [new branch]      v1.0 -> v1.0
exit=0

$ git push origin refs/tags/v1.0; echo "exit=$?"
To ../remote.git
 * [new tag]         v1.0 -> v1.0
exit=0

However, this means writing the name differently every time. If you write v1.0, the warning appears again.

Fixing it at the root: rename the branch

If you remove the duplicate name itself, the warning goes away. The easy approach is to keep the tag as the release marker and rename the branch. As the git branch official reference describes, -m renames a branch. Here I renamed it to fix/v1.0.1.

This section and the two that follow each start from a freshly created repository. In each case, both the tag v1.0 and the branch v1.0 exist, and only main has been pushed to the remote. The one exception is the section on a branch v1.0 that was already pushed, which starts from a state where the branch v1.0 was also pushed.

console
$ git log --oneline -1 v1.0
warning: refname 'v1.0' is ambiguous.
aac61e1 1.0 をリリース

$ git branch -m v1.0 fix/v1.0.1; echo "exit=$?"
exit=0

$ git log --oneline -1 v1.0; echo "exit=$?"
aac61e1 1.0 をリリース
exit=0

$ git log --oneline -1 fix/v1.0.1; echo "exit=$?"
12e5fb7 1.0 の不具合を修正
exit=0

After the rename, running the same git log --oneline -1 v1.0 as before no longer printed the warning. v1.0 now points only to the tag. The git push origin v1.0 that had stopped with an error because of the duplicate name now sent the tag.

console
$ git push origin v1.0; echo "exit=$?"
To ../remote.git
 * [new tag]         v1.0 -> v1.0
exit=0

When the branch v1.0 was already pushed

If you had also pushed the branch v1.0 to the remote, rename it locally, push the new name, and then delete the old branch on the remote. When deleting, write the full name refs/heads/v1.0 so Git does not have to guess which one you mean.

console
$ git branch -m v1.0 fix/v1.0.1; echo "exit=$?"
exit=0

$ git push origin fix/v1.0.1; echo "exit=$?"
To ../remote.git
 * [new branch]      fix/v1.0.1 -> fix/v1.0.1
exit=0

$ git push origin --delete refs/heads/v1.0; echo "exit=$?"
To ../remote.git
 - [deleted]         v1.0
exit=0

$ git ls-remote origin
12e5fb71fd2547b556d86963c5d9c0770a1b7945	HEAD
12e5fb71fd2547b556d86963c5d9c0770a1b7945	refs/heads/fix/v1.0.1
12e5fb71fd2547b556d86963c5d9c0770a1b7945	refs/heads/main
Warning

In a repository shared with others, let them know before you delete the branch on the remote.

When the tag is the one you don't need

If you only attached the tag by mistake, delete it with -d, as described in the official git tag reference. After I deleted it, v1.0 pointed to the branch and no warning appeared.

console
$ git tag -d v1.0; echo "exit=$?"
Deleted tag 'v1.0' (was aac61e1)
exit=0

$ git log --oneline -1 v1.0; echo "exit=$?"
12e5fb7 1.0 の不具合を修正
exit=0

Suppressing the warning is not a fix

core.warnAmbiguousRefs, described in the official git config reference, controls whether Git warns when a name is ambiguous (the default is true). Setting it to false removes the warning, but the tag and the branch with the same name both remain. I tested this too, in the repository that still had both the tag v1.0 and the branch v1.0 before any rename.

console
$ git -c core.warnAmbiguousRefs=false log --oneline -1 v1.0; echo "exit=$?"
aac61e1 1.0 をリリース
exit=0

The tag's commit is shown with no warning. The only thing this removes is the clue that git switch picks the branch while git log picks the tag. If you want to fix it, rename the branch.

For how to create tags and push them to a remote, see the separate article "How to tag a release version with git tag". For how to rename and delete branches, see "How to rename and delete a branch in Git".

​Posted by 86