git refname 'origin/master' is ambiguous

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

I have a git repository that is tracking several remote branches:

$ git branch -a * master   remotes/git-svn   remotes/origin/master   remotes/trunk

When I try to setup a default one I get the following error:

$ git branch --set-upstream-to=origin/master master warning: refname 'origin/master' is ambiguous. fatal: Ambiguous object name: 'origin/master'.

I would like to kremove some of the remote master branches but the master references are still there. How can I remove them to be able to set the default upstream branch to origin/master?

$ git show-ref master cba97a58c99743c355b569bbf35636c8823c2d96 refs/heads/master 6726b4985107e2ddc7539f95e1a6aba536d35bc6 refs/origin/master d83f025cd3800ed7acd76b2e52ae296e33f1cd07 refs/original/refs/heads/master cba97a58c99743c355b569bbf35636c8823c2d96 refs/remotes/origin/master

回答1:

The output of git branch -a shows that you have a remote-tracking branch called origin/master. Perfectly normal.

However, the output of git show-ref master contains

6726b4985107e2ddc7539f95e1a6aba536d35bc6 refs/origin/master

which indicates that you most likely ran something like the following low-level command:

git update-ref refs/origin/master master

This command creates a branch (pointing at the same commit as master) called origin/master, but living directly under refs/, i.e. outside the refs/heads/ namespace, where local branches normally live. Did you mean to do that?

Such a branch won't get listed by git branch -a. However, Git gets confused because it sees two branches whose refnames end with origin/master:

  • refs/remotes/origin/master, your remote-tracking branch, and
  • refs/origin/master, the local branch that you created (by accident) outside refs/heads/.

Solution

If you did not mean to create refs/origin/master

Simply delete it:

git update-ref -d refs/origin/master

Then, there won't be any ambiguity, and Git will comply when you try to set master's upstream.

If you did mean to create refs/origin/master

To avoid ambiguity, simply specify the full refname of the branch you wish to set as master's upstream:

git branch --set-upstream-to=refs/remotes/origin/master master

To fix ideas, here is some code that reproduces the situation in one of my GitHub repos:

$ cd ~/Desktop $ git clone https://github.com/Jubobs/gitdags && cd gitdags  $ git update-ref refs/origin/master  $ git branch -a * master   remotes/origin/HEAD -> origin/master   remotes/origin/master  $ git show-ref master 15b28ec22dfb072ff4369b35ef18df51bb55e900 refs/heads/master 15b28ec22dfb072ff4369b35ef18df51bb55e900 refs/origin/master 15b28ec22dfb072ff4369b35ef18df51bb55e900 refs/remotes/origin/HEAD 15b28ec22dfb072ff4369b35ef18df51bb55e900 refs/remotes/origin/master  $ git branch --set-upstream-to=origin/master master warning: refname 'origin/master' is ambiguous. fatal: Ambiguous object name: 'origin/master'.  $ git update-ref -d refs/origin/master $ git branch --set-upstream-to=origin/master master Branch master set up to track remote branch master from origin.


回答2:

You probably accidentally created a local ref called 'origin/master'

for instance, if you did this

git branch origin/master

It would lead to this problem. This one looks suspect "refs/origin/master". "refs/heads/master" is your local master, "refs/remotes/origin/master" is your remote branch reference, and "refs/origin/master" is probably a mistake that is screwing you up.

You just need to delete that reference and things will start working again.



回答3:

I had a very similar problem due to an accidental tag named "master", showing in git show-ref master as refs/tags/master. The fix in this case was:

git tag -d master


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!