如何使用git列出远程A但不在远程B中的分支?(How to list branches which are in remote A but not in remote B with git?)

编程入门 行业动态 更新时间:2024-10-26 20:32:44
如何使用git列出远程A但不在远程B中的分支?(How to list branches which are in remote A but not in remote B with git?)

例如,如果我有一个我开发的“主”上游存储库,以及我在完成工作日之后备份我的工作的一个侧存储库 - 现在我想删除侧存储库中不在“main”中的分支“存储库已经存在,因为它们已合并(或其他)。

如何列出存在于“side”远程但不在“main”远程中的分支?

For example if I have a "main" upstream repository where I develop, and a side-repository where I backup my work after a finished work-day - now I want to delete branches on the side-repository which are not in the "main" repository are anymore, because they are merged (or whatever).

How to list branches which exist in the "side" remote but not in the "main" remote?

最满意答案

更新 - 收集来自torek UPDATE 2的反馈 - 对建议的脚本进行小的但可能重要的更改


好吧,git并没有考虑多个回购中存在的分支。 您在本地仓库中有分支,并且您拥有任何给定远程分支的快照...并且本地分支可能与远程分支的名称相同,但它仍然不是“同一分支”饭桶。

只有跟踪提供了一种错觉,即“同一分支”在两个回购中。 (并且仅作为惯例,通常将本地分支命名为与远程分支相同。)这也是有限的,因为您不能有一个本地分支跟踪两个远程分支...

这是一个很长的说法,我不认为git可以做你所要求的; 你可能需要以某种方式后处理一些git输出。

您可以通过说明获取远程分支的列表

git for-each-ref refs/remotes/<remote-name>

你可以使用cut来获得基本的分支名称( cut -d\/ -f 4- )。 (注意尾随- ,如果/只有包含/分支名称,这很重要。)然后排序。 然后输入到comm以查看哪些行出现在两者中,或者只出现在一行中。

UPDATE - incorporated feedback from torek UPDATE 2 - small but potentially important change to suggested scripting


Well, git doesn't think in terms of a branch existing in multiple repos. You have branches in your local repo, and you have snapshots of the branches in any given remote... and the local branch may have the same name as a branch on the remote, but it still isn't "the same branch" to git.

Only tracking provides something of an illusion that "the same branch" is in two repos. (And only as a matter of convention is it typical to have the local branch named the same as the remote branch.) This, too, is limited in that you can't have one local branch track two remote branches...

Which is a long way of saying, I don't think git alone can do what you're asking; you'll likely need to post-process some git output in some way.

You can get the list of branches that are in a remote by saying

git for-each-ref refs/remotes/<remote-name>

You could use cut to get just the basic branch name (cut -d\/ -f 4-). (Note that trailing -, which is important if/only if you have branch names containing /.) Then sort. Then feed to comm to see which lines appear in both, or in only one.

更多推荐

本文发布于:2023-08-07 11:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464191.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   分支   git   remote   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!