获取默认远程推送和默认远程分支

时间:2015-12-04 23:55:35

标签: git push

从脚本中,我想获得默认的push remote和默认的push分支。

对于召回,git将按以下顺序选择这些设置之间的遥控器:

  • repl *= 2
  • branch.<name>.pushRemote
  • remote.pushDefault
  • 最后一次默认
    • 来源branch.<name>.remote
    • 如果config push.default current
    • ,则为NULL

我找不到任何参考git如何选择最后一个默认原点,但它似乎是静态的。

默认远程分支可以是:

  • config push.default upstream if branch.<name>.merge
  • 当前分支名称<否则

现在,我想要一种安全的方法来获得默认的push remote和默认的push分支。

  1. 我可以使用config push.default upstream,但我必须根据git config --get解决自己的好处,这似乎有点冒险。
  2. 我更愿意使用以下其中一项:
    • config push.default
    • git for-each-ref --format='%(push:short)' refs/heads/mybranch
  3. 最后,我有两个问题:

    1. git rev-parse --abbrev-ref=loose mybranch@{push}git for-each会返回git rev-parse之类的路径。如何在远程名称和分支名称之间进行拆分(远程名称可以包含origin/mybranch)。
    2. 两者之间是否有更安全的功能? (在我的所有测试中,它们总是返回相同的输出)。

1 个答案:

答案 0 :(得分:0)

使用Git 2.16(2018年第一季度),有一种新的语法,其中&#34; --format=...&#34; &#34; git for-each-ref&#34;的选项学会了展示 &#39; remote&#39;的名称存储库和远程端的ref 这会影响&#39; upstream&#39;和&#39; push&#39;通过&#34; %(push:remotename)&#34; 和朋友们。

commit 1b58686(2017年11月7日)和commit cc72385(2017年10月5日)Johannes Schindelin (dscho)。 请commit 9700fae查看J Wyman (whoisj)(2017年11月7日) Junio C Hamano -- gitster --于2017年11月15日commit 093048b合并)

  

for-each-ref:让upstream / push可选地报告远程名称

     

有时候例如脚本不仅要知道远程存储库上游分支的名称,还要知道远程名称。

     

此补丁为上游和for提供新的后缀:remotename   推动原子,允许准确显示。例如:

$ cat .git/config
...
[remote "origin"]
    url = https://where.do.we.come/from
    fetch = refs/heads/*:refs/remote/origin/*
[remote "hello-world"]
    url = https://hello.world/git
    fetch = refs/heads/*:refs/remote/origin/*
    pushURL = hello.world:git
    push = refs/heads/*:refs/heads/*
[branch "master"]
    remote = origin
    pushRemote = hello-world
...

$ git for-each-ref \
  --format='%(upstream) %(upstream:remotename) %(push:remotename)' \
  refs/heads/master
refs/remotes/origin/master origin hello-world
  

如果没有明确的话,实施选择 DWIM 推送遥控器   push remote已配置;原因是DWIM可以这样做   使用

%(if)%(push:remotename)%(then)
    %(push:remotename)
%(else)
    %(upstream:remotename)
%(end)
  

虽然不可能&#34; un-DWIM&#34;如果是这样的信息   调用者真的只对显式推送遥控器感兴趣。

     

虽然:remote会更短,但也会更加模棱两可,   并且它也会关闭门,例如对于:remoteref(这会   显然,请参考远程存储库中的相应引用。)

  

for-each-ref:让upstream / push报告远程引用名称

     

有时候脚本想知道不仅仅是名字   在远程推送分支,但也知道分支的名称   由远程存储库

     

这方面的一个例子是工具想要推送到同一个分支   它会自动拉出,即<remote><to>   在git push <remote> <from>:<to>中将提供   分别为%(upstream:remotename)%(upstream:remoteref)

     

此修补程序为:remoterefupstream提供新后缀push   原子,允许准确显示。例如:

$ cat .git/config
...
[remote "origin"]
    url = https://where.do.we.come/from
    fetch = refs/heads/*:refs/remote/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "develop/with/topics"]
    remote = origin
    merge = refs/heads/develop/with/topics
...

$ git for-each-ref \
    --format='%(push) %(push:remoteref)' \
    refs/heads
refs/remotes/origin/master refs/heads/master
refs/remotes/origin/develop/with/topics refs/heads/develop/with/topics