使用 git fetch 配置中未列出的分支获取并设置远程跟踪

问题描述 投票:0回答:1

我在一个非常大的 monorepo 中工作,因此默认情况下 fetch 配置受到限制,以避免拉下数千个分支并减慢 git 的速度(即

fetch = +refs/heads/master:refs/remotes/origin/master

如果我想拉下别人的分支,我仍然可以通过调用轻松做到这一点

git fetch origin <branch-name>:<branch-name>

它在当前状态下创建一个远程分支和一个本地分支。但是,它没有在两者之间设置远程跟踪,因此我无法将更改拉到本地分支。

我尝试使用

git branch -u origin/<branch-name>
手动设置远程跟踪,但出现以下错误:

fatal: cannot set up tracking information; starting point 'origin/<branch-name>' is not a branch

当我调用

origin/<branch-name>
时,我看到
git branch -r --list
并且
git show
可以处理它,所以我对这个错误感到困惑。有没有另一种方法可以解决这个问题而不必更改该分支的获取配置

抱歉,如果这是一个重复的问题,我进行了搜索,但找不到明确的答案。大多数不解决不包括所需分支的获取配置。我尝试了here列出的方法。但我仍然遇到同样的错误。

git git-fetch
1个回答
0
投票

这是我尝试做的事情的概要,您可以使用 webpack 重现它:

git clone https://github.com/webpack/webpack.git --single-branch

git config --list # should show “remote.origin.fetch=+refs/heads/main:refs/remotes/origin/main”

git fetch --refmap="+refs/heads/<some-other-branch>:refs/remotes/origin/<some-other-branch>" origin <some-other-branch>

git branch -r # should show new remote branch

git checkout -b <some-other-branch> origin/<some-other-branch> # Checks out new branch at same commit, but not tracking

git branch -u origin/<some-other-branch> # get error “fatal: cannot set up tracking information; starting point 'origin/<some-other-branch>' is not a branch”

但我确实发现以下内容可以满足我的需要。在别人的分支上调用 Push 让我很紧张,但如果我不强迫的话应该没问题。

git fetch origin <branch-name>:<branch-name>
git push -u origin <branch-name>
© www.soinside.com 2019 - 2024. All rights reserved.