我可以在 .git/config 中指定获取多个 refspecs 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15507264/
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-19 08:20:37  来源:igfitidea点击:

Can I specify in .git/config to fetch multiple refspecs?

git

提问by user716468

I do not want to fetch every branch from origin because there are many. I just want to track a few (e.g., master) and my branches (organized under my_namesub-directory). I can do the following:

我不想从原点获取每个分支,因为有很多。我只想跟踪一些(例如,master)和我的分支(在my_name子目录下组织)。我可以执行以下操作:

$ git fetch origin refs/heads/my_name/*:refs/remotes/origin/my_name/* refs/heads/master:refs/remotes/origin/master refs/heads/some_branch:refs/remotes/origin/some_branch

I want to specify the above "set" of refspecs to be the default of git fetch. I have tried

我想将上述“集合”的 refspecs 指定为git fetch. 我试过了

$ git config remote.origin.fetch refs/heads/my_name/*:refs/remotes/origin/my_name/*
$ git config --add remote.origin.fetch refs/heads/master:refs/remotes/origin/master

It fails:

它失败:

$ git config remote.origin.fetch
refs/heads/my_name/*:refs/remotes/origin/my_name/*
error: More than one value for the key remote.origin.fetch: refs/heads/master:refs/remotes/origin/master

I also try the following but it also fails:

我也尝试了以下但它也失败了:

$ git config remote.origin.fetch 'refs/heads/my_name/*:refs/remotes/origin/my_name/* refs/heads/master:refs/remotes/origin/master refs/heads/some_branch:refs/remotes/origin/some_branch'
$ git fetch
fatal: Invalid refspec 'refs/heads/my_name/*:refs/remotes/origin/my_name/* refs/heads/master:refs/remotes/origin/master refs/heads/some_branch:refs/remotes/origin/some_branch'

Note: Git 1.7.11

注意:Git 1.7.11

回答by Tuxdude

You can add the following lines in your .git/configto specify multiple refspecs for fetch:

您可以在您的添加以下行.git/config以指定多个 refspecs 用于获取:

[remote "origin"]
       fetch = refs/heads/my_name/*:refs/remotes/origin/my_name/*
       fetch = refs/heads/master:refs/remotes/origin/master
       fetch = refs/heads/some_branch:refs/remotes/origin/some_branch

You can add the prefix +before the refspec, if you would like to override fetching non-fast-forward references as well, like this:

+如果您还想覆盖获取非快进引用,您可以在 refspec 之前添加前缀,如下所示:

[remote "origin"]
       fetch = +refs/heads/my_name/*:refs/remotes/origin/my_name/*
       fetch = +refs/heads/master:refs/remotes/origin/master
       fetch = +refs/heads/some_branch:refs/remotes/origin/some_branch

Note that partial globbing is not supported (i.e. a/b/ca*is not supported, but a/b/*is).

请注意,不支持部分通配符(即a/b/ca*不支持,但支持a/b/*)。

10.5 Git Internals - The Refspec

10.5 Git 内部原理——参考规范

回答by Shaun Cox

To overwrite the existing fetch refspec(s), without having to manually edit .git/config, you can use --unset-allfollowed by as many --addas needed.

要覆盖现有的 fetch refspec(s),而无需手动编辑.git/config,您可以根据需要使用--unset-all后跟--add

For the example desired refspecs in the question, it would be:

对于问题中所需的 refspecs 示例,它将是:

git config --unset-all remote.origin.fetch
git config --add remote.origin.fetch +refs/heads/my_name/*:refs/remotes/origin/my_name/*
git config --add remote.origin.fetch +refs/heads/master:refs/remotes/origin/master

Then use git config --get-all remote.origin.fetchto verify the result.

然后使用git config --get-all remote.origin.fetch来验证结果。

回答by VonC

Note: if you want to fetch from a different refspec on a single invocation(overriding temporarily the fetch refspec registered in the config), you can do so since Git 2.1 (August 2014).

注意:如果您想在单个调用中从不同的 refspec 获取(临时覆盖配置中注册的 fetch refspec),您可以从 Git 2.1(2014 年 8 月)开始这样做。

See commit c5558f8by Junio C Hamano (gitster):

提交c5558f8通过JUNIOÇ滨野(gitster

Since the introduction of opportunistic updates of remote-tracking branches, started at around f269048(fetch: opportunistically update tracking refs, 2013-05-11) with a few updates in v1.8.4 era, the remote.*.fetchconfiguration always kicks in even when a refspec to specify what to fetch is given on the command line, and there is no way to disable or override it per-invocation.

Teach the command to pay attention to the --refmap=<lhs>:<rhs>command-line options that can be used to override the use of configured remote.*.fetchas the refmap.

自从引入远程跟踪分行的机会更新,在各地开始的f269048fetch:投机更新追踪裁判,2013年5月11日),在v1.8.4时代的一些更新,remote.*.fetch配置总是踢即使的Refspec指定命令行上给出了要获取的内容,并且无法在每次调用时禁用或覆盖它

教导命令注意--refmap=<lhs>:<rhs>命令行选项,这些选项可用于覆盖配置remote.*.fetch为 refmap 的使用。

That gives you the new option:

这为您提供了新的选择:

--refmap=<refspec>

When fetching refs listed on the command line, use the specified refspec (can be given more than once) to map the refs to remote-tracking branches, instead of the values of remote.*.fetchconfiguration variables for the remote repository.
See section on "Configured Remote-tracking Branches" for details.

在获取命令行上列出的 refs 时,使用指定的 refspec(可以多次给出)将 refs 映射到远程跟踪分支,而不是remote.*.fetch远程存储库的配置变量的值。
有关详细信息,请参阅“配置的远程跟踪分支”部分。

(That Git "Configured Remote-tracking Branches" section also dates from Git 2.1: see "Having a hard time understanding git fetch")

(那个 Git“配置的远程跟踪分支”部分也可以追溯到 Git 2.1:参见“很难理解git fetch”)



With Git 2.25.1 (Feb. 2020), "git fetch --refmap=" option has got a better documentation.

在 Git 2.25.1(2020 年 2 月)中,“git fetch --refmap=”选项获得了更好的文档。

See commit b40a502(21 Jan 2020) by Derrick Stolee (derrickstolee).
(Merged by Junio C Hamano -- gitster--in commit 4b69f29, 30 Jan 2020)

请参阅Derrick Stolee ( )提交的 b40a502(2020 年 1 月 21 日)(由Junio C Hamano合并-- --提交 4b69f29 中,2020 年 1 月 30 日)derrickstolee
gitster

fetch: document and test --refmap=""

Signed-off-by: Derrick Stolee

To prevent long blocking time during a 'git fetch' call, a user may want to set up a schedule for background 'git fetch' processes.
However, these runs will update the refs/remotes branches due to the default refspec set in the config when Git adds a remote.
Hence the user will not notice when remote refs are updated during their foreground fetches. In fact, they may wantthose refs to stay put so they can work with the refs from their last foreground fetch call.

This can be accomplished by overriding the configured refspec using '--refmap=' along with a custom refspec:

git fetch --refmap='' <remote> +refs/heads/*:refs/hidden/<remote>/*

to populate a custom ref space and download a pack of the new reachable objects.
This kind of call allows a few things to happen:

  1. We download a new pack if refs have updated. 2. Since the refs/hidden branches exist, GC will not remove the newly-downloaded data.
  2. With fetch.writeCommitGraphenabled, the refs/hidden refs are used to update the commit-graph file.

To avoid the refs/hidden directory from filling without bound, the --pruneoption can be included. When providing a refspec like this, the --pruneoption does not delete remote refs and instead only deletes refs in the target refspace.

Update the documentation to clarify how '--refmap=""' works and create tests to guarantee this behavior remains in the future.

fetch: 文档和测试 --refmap=""

签字人:德里克·斯托利

为防止在“ git fetch”调用期间阻塞时间过长,用户可能需要为后台“ git fetch”进程设置时间表。
但是,由于 Git 添加远程时在配置中设置的默认 refspec,这些运行将更新 refs/remotes 分支。
因此,用户不会注意到在他们的前台获取期间远程 refs 何时更新。事实上,他们可能希望这些 refs 保持原样,以便他们可以使用上次前台 fetch 调用中的 refs。

这可以通过使用“ --refmap=”和自定义 refspec覆盖配置的 refspec 来实现:

git fetch --refmap='' <remote> +refs/heads/*:refs/hidden/<remote>/*

填充自定义参考空间并下载一组新的可访问对象。
这种调用允许发生一些事情:

  1. 如果 refs 更新,我们会下载一个新包。2. 由于refs/hidden branch存在,GC不会移除新下载的数据。
  2. fetch.writeCommitGraph启用,裁判/隐藏裁判被用来更新提交-图形文件。

为了避免 refs/hidden 目录无限制填充,--prune可以包含该选项。当提供这样--prune的引用规范时,该选项不会删除远程引用,而是只删除目标引用空间中的引用。

更新文档以阐明“ --refmap=""”的工作原理并创建测试以保证此行为在未来仍然存在。

So the git fetchoption man pagenow includes:

所以git fetch选项手册页现在包括:

--refmap=<refspec>:

When fetching refs listed on the command line, use the specified refspec (can be given more than once) to map the refs to remote-tracking branches, instead of the values of remote.*.fetchconfiguration variables for the remote repository.

Providing an empty <refspec>to the --refmapoption causes Git to ignore the configured refspecs and rely entirely on the refspecs supplied as command-line arguments.
See section on "Configured Remote-tracking Branches" for details.

--refmap=<refspec>:

在获取命令行上列出的 refs 时,使用指定的 refspec(可以多次给出)将 refs 映射到远程跟踪分支,而不是remote.*.fetch远程存储库的配置变量的值。

<refspec>为该--refmap选项提供一个空值会导致 Git 忽略配置的 refspecs 并完全依赖作为命令行参数提供的 refspecs。
有关详细信息,请参阅“配置的远程跟踪分支”部分。



Note that the more aggressive updates to remote-tracking branches we had for the past 7 years or so were not reflected in the documentation, which has been corrected with Git 2.27 (Q2 2020).

请注意,我们在过去 7 年左右对远程跟踪分支进行的更积极的更新并未反映在文档中,该文档已在 Git 2.27(2020 年第二季度)中得到纠正。

See commit a440884, commit f6a65de(05 Apr 2020) by Philippe Blain (phil-blain).
(Merged by Junio C Hamano -- gitster--in commit fdee8b1, 22 Apr 2020)

请参阅Philippe Blain ( )提交的 a440884f6a65de(2020 年 4 月 5 日(由Junio C Hamano合并-- --fdee8b1 提交中,2020 年 4 月 22 日)phil-blain
gitster

pull doc: correct outdated description of an example

Signed-off-by: Philippe Blain

Since f269048754("fetch: opportunistically update tracking refs", 2013-05-11, Git v1.8.4-rc0 -- mergelisted in batch #0), the underlying git fetchin [git pull](https://git-scm.com/docs/git-pull) <remote> <branch>updates the configured remote-tracking branch for .

However, an example in the 'Examples' section of the git pulldocumentation still states that this is not the case.

Correct the description of this example.

pull doc: 更正过时的示例描述

签字人:菲利普·布莱恩

由于f269048754(“ fetch:机会性地更新跟踪参考文献”,2013年5月11日,GIT中v1.8.4-RC0 -合并中列出批次#0),底层git fetch[GIT中拉](https://git-scm.com/docs/git-pull) <remote> <branch>更新配置的远程跟踪分支为。

但是,git pull文档“示例”部分中的一个示例仍然指出情况并非如此。

更正此示例的描述。

So instead of, for git pull origin next:

所以,而不是,对于git pull origin next

This leaves a copy of nexttemporarily in FETCH_HEAD, but does not update any remote-tracking branches.
Using remote-tracking branches, the same can be done by invoking fetch and merge:

这会next在 FETCH_HEAD中临时保留一个副本,但不会更新任何远程跟踪分支。
使用远程跟踪分支,同样可以通过调用 fetch 和 merge 来完成:

You now have:

你现在有

This leaves a copy of nexttemporarily in FETCH_HEAD, and updates the remote-tracking branch origin/next.
The same can be done by invoking fetch and merge:

这会next在 FETCH_HEAD中临时保留一个副本,并更新远程跟踪分支origin/next
同样可以通过调用 fetch 和 merge 来完成:

回答by Paul

Answering my own complaint about lack of examples for refmap. Here's an example of checking out a pull request from VSO (Visual Studio Online) without interacting with the config.

回答我自己关于缺少refmap. 这是在不与配置交互的情况下从 VSO (Visual Studio Online) 检出拉取请求的示例。

$ git fetch --refmap='+refs/pull/*/merge:refs/remotes/origin/pr/*' origin refs/pull/1415/merge $ git checkout pr/1415

$ git fetch --refmap='+refs/pull/*/merge:refs/remotes/origin/pr/*' origin refs/pull/1415/merge $ git checkout pr/1415