繁体   English   中英

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

[英]git: Push all local branches that track a specific remote

我正在寻找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不同的remote的分支。

如何才能做到这一点?

要列出分支及其上游分支,我推荐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}'

现在 go 推:

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

(在上面,将<MYREMOTE>替换为您的遥控器名称。)

您可以 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 推送:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM