为什么 git Branch -t 会失败并显示“未跟踪:信息不明确”?

发布于 2024-08-04 17:44:46 字数 401 浏览 3 评论 0原文

当我尝试创建一个跟踪远程分支的新分支时,我得到:

$ git branch -t test origin/foo
error: Not tracking: ambiguous information for ref refs/remotes/origin/foo

源代码 似乎以某种方式搜索要跟踪的分支并将我抛出,因为它发现 less 超过一个,但我并没有完全明白它在寻找什么,因为我已经告诉它要跟踪什么在命令行上。

谁能告诉我发生了什么事以及如何解决它?

When I try to create a new branch tracking a remote branch, I get this:

$ git branch -t test origin/foo
error: Not tracking: ambiguous information for ref refs/remotes/origin/foo

The source seems to somehow search for branches to track and throws me out because it finds less more than one, but I don't exactly get what it's looking for since I already told it what to track on the command line.

Can anybody tell me what's going on and how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

○闲身 2024-08-11 17:44:46

当我有两个具有相同(默认)获取模式(fetch = +refs/heads/*:refs/remotes/origin/*)和相同分支的远程存储库时,我也看到了这一点。

最初的错误是创建两个具有相同 fetch 配置的遥控器。新的遥控器具有不同的名称,因此其文件夹的名称也应该更改。以下是正确的多远程配置的示例:

[remote "origin"]
    url = <primary-source>
    fetch = +refs/heads/*:refs/remotes/origin/*
[remote "alt"]
    url = <alternate-source>
    fetch = +refs/heads/*:refs/remotes/alt/*

用您选择的任何名称代替 alt

说明:

refs/heads/*:refs/remotes/origin/*的含义是:获取refs/heads中的所有远程引用在远程端,并将 refs/heads/origin 放在我们这边。 refs/heads 是存储分支的路径,因此如果远程有分支 foo,它将被提取到 origin/foo在您本地的存储库中。开头的 + 表示目标分支应始终被覆盖(无需进行一些额外的检查)。

或者:

您还可以手动将信息添加到项目的.git/config中,例如:

[branch "mybranch"]
    remote = origin
    merge = refs/heads/mybranch

I saw this also when I had two remote repo's with the same (default) fetch pattern (fetch = +refs/heads/*:refs/remotes/origin/*) and the same branches.

The original mistake was in creating two remotes with the same fetch configuration. The new remote has a different name, and therefore the name of its folder should have been changed as well. Here is an example of a proper multiple-remote configuration:

[remote "origin"]
    url = <primary-source>
    fetch = +refs/heads/*:refs/remotes/origin/*
[remote "alt"]
    url = <alternate-source>
    fetch = +refs/heads/*:refs/remotes/alt/*

In place of alt put any name of your choosing.

Explanation:

The meaning of refs/heads/*:refs/remotes/origin/* is: take all the remote reference in refs/heads on the remote side, and put the to refs/heads/origin on our side. refs/heads is the path where branches are stored, so if you have branch foo on the remote, it will be fetched to origin/foo in your local repo. The + on the beginning means that the destination branches should be always overwritten (w/o that there are some additional checks).

Alternatively:

You can also manually add the info to the project's .git/config, e.g.:

[branch "mybranch"]
    remote = origin
    merge = refs/heads/mybranch
善良天后 2024-08-11 17:44:46

因为它发现少于一个

否:因为它找到多于一个匹配的远程分支,这意味着函数 remote_find_tracking() 对于给定的本地分支返回多个跟踪分支参考号

some_remote_branch 是否尚未被您的本地分支机构之一跟踪?
git config -l 可以让您检查当前设置的内容)。
git Branch -r 还可以帮助列出当前的远程跟踪分支。)

另请参阅“复制从一个存储库到另一个存储库的分支”,了解 Git 2.36(2022 年第 2 季度)的更多详细信息。


远程分支,我认为它与远程跟踪分支不同。

错误的,如此帖子所示:

远程分支是“真正的”远程跟踪分支。您无需在本地提交它们,它们本质上是远程存储库中发生的情况的只读副本。
如果您尝试“git-checkout”远程跟踪分支,您将得到一个分离的 HEAD。

本地分行:
您可以向其提交更改的分支。或者,可以将分支配置为“跟随”您的远程跟踪分支之一。这意味着不带参数的“git-pull”(当签出本地分支时)将自动“git-fetch”,然后“git-合并'远程跟踪分支。

现在:

git-fetch 的工作是使用远程存储库中发现的任何更改来更新远程跟踪分支。
Git-pull 运行 git-fetch,然后运行 ​​git-merge 来更新当前签出的分支。

问题是,对于 git-merge:

发生这种情况时,git-merge 必须决定将哪个 remote-tracking-branch 合并到当前签出的本地分支。
您可以使用 --track 选项设置在这种情况下选择哪个remote-tracking-branch

--track 设置本地跟随分支来引用远程分支,而不是跟踪分支

考虑 remote_find_tracking() 采用单个遥控器和填充了 src 的 refspec,并在之后返回给定的 refspec如果为远程配置了适当的跟踪(即 git),则填充其 dst。

/*
 * For the given remote, reads the refspec's src and sets the other fields.
 */
int remote_find_tracking(struct remote *remote, struct refspec *refspec);

可能它认为它已经有一个与 some_remote_branch 匹配的本地后续分支。您在当地有同名的分支机构吗?
或者,相反:您当前的分支有一个具有相似名称的远程分支,这使其成为任何 git-merge 的自然候选者:尝试使其跟踪另一个分支> 远程分支将使 git-merge 无法选择要更新/合并远程更改的本地分支。

because it finds less than one

Nope: because it finds more than one matching remote branch, which means the function remote_find_tracking() returns more than one tracking branch for a given local branch ref.

Is some_remote_branch not already tracked by one of your local branches?
(a git config -l would allow you to check what you have currently have set up).
(a git branch -r can also help to list your current remote-tracking branches. )

See also "copy a branch from one to another repo" for more details with Git 2.36 (Q2 2022).


remote branches, which I thought are something different that remote-tracking branches.

Wrong, as illustrated by this thread:

remote-branches are the "real" remote-tracking-branches. You don't commit to them locally, they are essentially read-only copies of exactly what is happening in a remote repository.
If you try to 'git-checkout' a remote-tracking branch, you will get a detached HEAD.

Local branch:
A branch to which you may commit changes. Optionally, the branch can be configured to "follow" one of your remote-tracking branches. This means that a 'git-pull' without arguments (when your local branch is checked out), will automatically 'git-fetch' and then 'git-merge' the remote-tracking branch.

Now:

it's the job of git-fetch to update remote-tracking branches with any changes found in the remote repository.
Git-pull runs git-fetch and then runs a git-merge to update the currently-checked-out branch.

The problem is, for git-merge:

When this happens, git-merge must decide which remote-tracking-branch to merge into the currently checked out local branch.
You can set which remote-tracking-branch will be selected in this situation with the --track option.

--track sets up a local following branch to refer to a remote's branch, not to the tracking branch

Consider that remote_find_tracking() takes a single remote and a refspec with src filled, and returns the given refspec after filling its dst, if an appropriate tracking was configured for the remote, meaning git.

/*
 * For the given remote, reads the refspec's src and sets the other fields.
 */
int remote_find_tracking(struct remote *remote, struct refspec *refspec);

May be it considers it already has a local following branch matching some_remote_branch. Do you have any local branch with that exact same name?
Or, the other way around: your current branch has a remote branch with a similar name, which makes it a natural candidate for any git-merge: trying to make it track another remote branch would make the git-merge unable to choose which local branch to update/merge with changes of a remote.

谁的新欢旧爱 2024-08-11 17:44:46

知道了!问题是我之前已经使用 --mirror 设置了远程,以便拥有我的存储库的备份/公共副本。

如果你运行

git remote add --mirror <name> <url>

它,它不仅将远程标记为镜像(这就是我想要的推送),而且还将镜像的 remote..fetch 选项配置为 +refs /*:refs/*,这意味着您的所有分支突然“跟踪”您的镜像存储库,并且任何创建跟踪分支的尝试都将失败。

(作为额外的好处,运行 git fetch将会用备份存储库中的旧引用覆盖所有引用。)

似乎解决此问题的解决方案是设置 remote ..fetch: (我希望这意味着“从不获取任何内容”)。这显然解决了跟踪问题并消除了致命的获取。

Got it! The problem was that I have previously set up a remote with --mirror, for the purpose of having a backup / public copy of my repository.

If you run

git remote add --mirror <name> <url>

it does not only flag the remote as mirror (which is what I wanted for pushes), but also configures remote.<mirror>.fetch option for the mirror to +refs/*:refs/*, which means that all of your branches suddenly "track" your mirror repository and any attempt to create a tracking branch is going to fail.

(As an added bonus, running git fetch <mirror> is going to overwrite all your refs with old ones from your backup repo.)

The solution that seems to fix this issue is setting remote.<mirror>.fetch to : (which, I hope, means "never fetch anything"). This apparently fixes the tracking issue and eliminates the deadly fetch.

自在安然 2024-08-11 17:44:46

这里提到的其他修复都不适合我。最终工作的是这样的:

$ git branch -t test origin/test
error: Not tracking: ambiguous information for ref refs/remotes/origin/test

运行上面的代码后,尽管 git 抱怨,它最终还是创建了一个本地分支 test 但没有上游集。

现在,我打开 .git/config 文件(其中没有任何分支 test 的记录)并手动添加以下内容:

[branch "test"]
        remote = origin
        merge = refs/heads/test

之后一切正常。

None of the other fixes mentioned here worked for me. The one that ended up working was this:

$ git branch -t test origin/test
error: Not tracking: ambiguous information for ref refs/remotes/origin/test

After running the above, even though git complained, it did end up creating a local branch test but no upstream set.

Now, I opened the .git/config file (which did not have any record of a branch test) and added the following manually:

[branch "test"]
        remote = origin
        merge = refs/heads/test

After which everything worked fine.

揪着可爱 2024-08-11 17:44:46

我遇到了这样的情况,但我不知道怎么办。 gitbranch-av 的列表只显示了我关心的分支的一个远程跟踪分支(origin/dev)。

我修复它的方法是使用十六进制提交哈希而不是 origin/dev

git checkout -b dev abc123
git push -u origin dev

当我使用 -u 进行推送时,Git 说 Branch dev set up跟踪来自原点的远程分支开发。 随后的拉取和推送按我的预期工作。

I got in a situation like this, but I do not know how. The listing from git branch -av showed me only a single remote tracking branch for the branch I cared about (origin/dev).

What I did to fix it was to use the hexadecimal commit hash instead of origin/dev:

git checkout -b dev abc123
git push -u origin dev

When I did the push with -u Git said Branch dev set up to track remote branch dev from origin. Subsequent pulls and pushes worked as I expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文