【问题标题】:Get default remote push and default remote branch获取默认远程推送和默认远程分支
【发布时间】:2015-12-04 23:55:34
【问题描述】:

我想从一个脚本中获取默认的推送远程和默认的推送分支。

对于召回,git 将在这些设置之间选择远程,按以下顺序:

  • branch.<name>.pushRemote
  • remote.pushDefault
  • branch.<name>.remote
  • 最后一个默认值
    • 起源于config push.default current
    • 如果config push.default upstream 则为空

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

默认远程分支可以是:

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

现在,我想要一种安全的方式来获取默认推送远程和默认推送分支。

  1. 我可以使用git config --get,但我必须根据config push.default来解决自己的行为,这似乎有点冒险。
  2. 我更喜欢使用以下之一:
    • git for-each-ref --format='%(push:short)' refs/heads/mybranch
    • git rev-parse --abbrev-ref=loose mybranch@{push}

最后,我有两个问题:

  1. git for-eachgit rev-parse 返回类似 origin/mybranch 的路径。如何在远程名称和分支名称之间进行拆分(远程名称可以包含/'s)。
  2. 两者之间有更安全的功能吗? (在我所有的测试中,它们总是返回相同的输出)。

【问题讨论】:

    标签: git push


    【解决方案1】:

    在 Git 2.16(2018 年第一季度)中,有一种新语法,其中“git for-each-ref”的“--format=...”选项学会了显示 'remote' 存储库的名称和远程端的 ref 通过“%(push:remotename)”影响“upstream”和“push” 和朋友。

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

    for-each-ref:让上游/推送可选地报告远程名称

    有时候,例如scripts 不仅想知道远程仓库的上游分支的名字,还想知道远程的名字。

    此补丁为上游提供了新的后缀: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
    

    实现选择 notDWIM 如果没有明确的推送远程 推送远程已配置;原因是可以对这个进行 DWIM 通过使用

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

    虽然不可能“取消 DWIM”信息,以防万一 调用者实际上只对显式推送遥控器感兴趣。

    虽然:remote 会更短,但也会更模糊, 它也会关上门,例如对于:remoteref(这将 明显是指远程仓库中对应的ref)。


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

    有时脚本想要知道不仅是 在远程推送分支,还有已知的分支名称 通过远程存储库

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

    此补丁为upstreampush 提供了新的后缀:remoteref 原子,可以准确地表明这一点。示例:

    $ 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
    

    【讨论】:

      猜你喜欢
      • 2016-09-05
      • 2011-04-05
      • 2011-11-19
      • 1970-01-01
      • 2014-01-05
      • 2022-01-09
      • 2018-11-28
      • 2011-06-18
      • 2021-06-03
      相关资源
      最近更新 更多