代码之家  ›  专栏  ›  技术社区  ›  nh2

git:推送跟踪特定远程服务器的所有本地分支

  •  0
  • nh2  · 技术社区  · 5 年前

    我在找一个相当于 git push --all 仅限于特定的 remote 并且只推送跟踪的本地分支 那个 遥远的

    git push --all myremote 不起作用,例如:

    % git push nh2github --all --dry-run
    To github.com:nh2/myrepo.git
     + f4165c1e160...6882a13b8c1 branch1-tracking-nh2github -> branch1-tracking-nh2github
     + e5998342793...010aa57c786 branch2-tracking-nh2github -> branch2-tracking-nh2github
     * [new branch]              branch3-tracking-otherremote -> branch3-tracking-otherremote
    

    这不好,因为它会推动 全部的 本地分支机构,包括跟踪不同客户的分支机构 遥远的 nh2github 我已经详细说明了。

    如何做到这一点?

    0 回复  |  直到 5 年前
        1
  •  2
  •   nh2    4 年前

    列出我推荐的分支机构及其上游分支机构 git for-each-ref :

    git for-each-ref refs/heads --format='%(refname:short) %(upstream)'
    

    按所选的遥控器筛选列表 awk (打印分行名称):

    git for-each-ref refs/heads --format='%(refname:short) %(upstream)' |
        awk '$2 ~ /^refs\/remotes\/<MYREMOTE>\// {print $1}'
    

    现在开始推:

    git for-each-ref refs/heads --format='%(refname:short) %(upstream)' |
        awk '$2 ~ /^refs\/remotes\/<MYREMOTE>\// {print $1}' |
        xargs git push <MYREMOTE>
    

    (在上述情况下,更换 <MYREMOTE> 按遥控器的名字。)

        2
  •  1
  •   LeGEC    5 年前

    你可以通过grep走出困境 git config -l 要将链接到特定远程服务器的分支作为目标,请执行以下操作:

    # each branch linked to a remote will have two lines :
    #   branch.[branch name].remote = trackedremote
    #   branch.[branch name].merge = refs/heads/remote/name
    # you can keep the local branches that track a ref from targetremote :
    git config -l | grep '^branch\.' | grep '\.remote = targetremote$'
    
    # use another command to extract the actual branch name :
    # awk would work
    ... | awk -F . '{ print $2 }'
    

    然后,您可以将分支列表提供给git push:

    branches=$(command above)
    git push targetremote $branches