Git:refname 'master' 不明确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12224773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:30:19  来源:igfitidea点击:

Git: refname 'master' is ambiguous

git

提问by Max

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

我已经查看了所有其他模棱两可的 refname 问题,但似乎都没有帮助。为什么我会收到此警告?

$ 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
$ 

回答by VonC

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

TL;DR:保存并删除标签,正如Ashutosh Jindal评论的那样(请参阅“在 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:

我看到的大多数来源(如这个 FAQ)都指向相同的原因:

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.

如果您创建了一个与远程标签同名的本地分支,就会发生这种情况
Git 应该检查你的本地分支,但它试图检查标签,它变得混乱。

最初导入的几棵树是有问题的,因为它们包含同名的分支和标签。从那以后,我们通过重命名标签解决了很多这些问题。

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

在您的情况下,您没有遥控器,但是像您的分支一样命名的本地标签就足够了。

The ambiguity is specified in gitrevision

歧义在gitrevision 中指定

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

<refname>, 例如master, heads/master,refs/heads/master

A symbolic ref name. E.g. mastertypically means the commit object referenced by refs/heads/master.
If you happen to have both heads/masterand tags/master, you can explicitly say heads/masterto 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_HEADand 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>/HEADif it exists.

符号引用名称。例如,master通常表示由 引用的提交对象refs/heads/master
如果你碰巧同时拥有heads/masterand tags/master,你可以明确地说heads/masterto tell git 你的意思是哪一个。
当有歧义时, a<refname>通过在以下规则中取第一个匹配项来消除歧义:

如果$GIT_DIR/<refname>存在,那就是你的意思(这通常只对HEAD, FETCH_HEAD, ORIG_HEAD,MERGE_HEAD和有用CHERRY_PICK_HEAD);

  • 否则,refs/<refname>如果存在;
  • 否则,refs/tags/<refname>如果存在;
  • 否则,refs/heads/<refname>如果存在;
  • 否则,refs/remotes/<refname>如果存在;
  • 否则,refs/remotes/<refname>/HEAD如果它存在。

So check where mastercan be found in your repo.

因此,请检查master可以在您的回购中找到的位置。

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

并且git checkout heads/master会一直工作。
警告:默认情况下,这将以DETACHED HEAD 模式检出分支。请参阅“为什么git checkout使用显式 ' refs/heads/branch' 给出分离的 HEAD?”。

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

为了避免这种情况,并且仍然使用明确的引用,请输入:

git checkout -B master heads/master

回答by Trebor Rude

Although this doesn't apply to the OP's situation, I got myself a refname is ambiguouswarning after accidentally doing a git branch origin/branchinstead 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 -Doperates on local branches).

尽管这不适用于 OP 的情况,但refname is ambiguous在不小心执行了 agit branch origin/branch而不是git checkout origin/branch. 这创建了一个名为 的本地分支origin/branch,这使得它与远程分支不明确。解决问题就像git branch -D origin/branch(安全,因为-D在本地分支上运行)一样简单。

回答by GaryO

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.

这只是发生在我身上。我不知何故有一个包含 sha 的文件 .git/master。不确定它是如何到达那里的,但是当我删除它时,错误就消失了。如果您仔细阅读已接受的答案,这是“预期行为”,但如果您执行 git show-ref master 等操作,则不会看到 .git/master ,因为它遵循略有不同的规则。