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

时间:2013-03-19 18:10:27

标签: git

我不想从原点获取每个分支,因为有很多分支。我只想跟踪一些(例如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

我想将refspecs的上述“set”指定为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

失败了:

$ 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

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

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

注意:Git 1.7.11

4 个答案:

答案 0 :(得分:34)

您可以在.git/config中添加以下行,为fetch指定多个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

你可以在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

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

10.5 Git Internals - The Refspec

答案 1 :(得分:9)

要覆盖现有的提取refspec,而无需手动修改.git/config,您可以根据需要使用--unset-all后跟多个--add

对于问题中所需的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

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

答案 2 :(得分:4)

注意:如果您想在单个调用中从其他refspec 获取(暂时覆盖在配置中注册的fetch refspec),您可以从Git 2。1(2014年8月)开始。< / p>

commit c5558f8Junio C Hamano (gitster)

  

自引入远程跟踪分支的机会更新以来,在f269048fetch:机会主义更新跟踪参考,2013-05-11)开始,并在v1.8.4时代进行了一些更新, remote.*.fetch配置总是会启动,即使在命令行上给出了指定要获取内容的refspec,并且无法在每次调用时禁用或覆盖它

     

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

这为您提供了新选项:

--refmap=<refspec>
  

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

(Git&#34;配置远程跟踪分支&#34;部分也可以从Git 2.1开始:参见&#34; Having a hard time understanding git fetch&#34;)

答案 3 :(得分:1)

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

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

相关问题