简体   繁体   English

如何列出未合并到给定分支的`git`分支?

[英]How to list `git` branches not merged to given branches?

I. e. IE。 something like git branch --remotes --no-merged origin/master origin/prod origin/dev - but so that it lists all branches that are not merged into any of these given branches (list branches that are merged into none of given ones)git branch --remotes --no-merged origin/master origin/prod origin/dev这样的东西 - 但是它列出了所有没有合并到这些给定分支中的分支(列出没有合并到任何给定分支的分支)

If you need to do it once:如果您需要执行一次:

git branch --no-merged branch1; git branch --no-merged branch2|awk 'a[$0]++==2'

You should count how many branches command you write, and adjust the 2 in the awk line.您应该计算您编写的分支命令的数量,并调整 awk 行中的2

If you have to do this operation often, you can write a little shell loop to accept the branch names, and do the same with a dynamic "2" in awk.如果您必须经常执行此操作,您可以编写一个小 shell 循环来接受分支名称,并在 awk 中使用动态“2”执行相同操作。

Question already asked I guess问题已经问了我猜

How can I know if a branch has been already merged into master? 我怎么知道一个分支是否已经合并到 master 中?

if you want to aggregate branches, just:如果要聚合分支,只需:

git branch --no-merged origin/master > file
git branch --no-merged origin/xxx >> file
git branch --no-merged origin/yyy >> file

and count the one that appears 3 times...并计算出现3次的那个...

Solution解决方案

I ended up with a solution similar to the following:我最终得到了类似于以下的解决方案:

git branch --remotes --no-merged origin/master | \
grep --fixed-strings --invert-match --file=<( \
    echo prod branch2 branch3 branchN | \
    tr ' ' '\n' | \
    xargs --replace \
    git branch --remotes --merged origin/{} \
)

Explanation解释

Below is the explanation of what different parts of the command above do and their output:以下是上述命令的不同部分及其 output 的说明:

$ echo prod branch2 branch3 branchN
prod branch2 branch3 branchN

- simply outputs names of given branches separated by spaces. - 简单地输出由空格分隔的给定分支的名称。

$ echo prod branch2 branch3 branchN | \
>     tr ' ' '\n'
prod
branch2
branch3
branchN

- outputs the same but each branch on a new line. - 输出相同,但每个分支都在新行上。

echo prod branch2 branch3 branchN | \
>     tr ' ' '\n' | \
>     xargs --replace \
>     git branch --remotes --merged origin/{}

- will call the git branch command for each of listed branches, like this: - 将为每个列出的分支调用git branch命令,如下所示:

git branch --remotes --merged origin/prod;
git branch --remotes --merged origin/branch2;
git branch --remotes --merged origin/branch3;
git branch --remotes --merged origin/branchN;

- that is, it'll list all remote branches that are merged into given ones (separated by new lines), eg: - 也就是说,它将列出合并到给定分支中的所有远程分支(由新行分隔),例如:

  origin/some-branch-merged-into-prod
  origin/branch2A-merged-into-branch2
  origin/branch2B-merged-into-branch2
  origin/some-other-branch-merged-into-branchN
git branch --remotes --no-merged origin/master

- this'll list all branches that aren't merged into origin/master , eg: - 这将列出所有合并到origin/master的分支,例如:

  origin/some-branch-not-merged-into-any-given-branch
  origin/branch2A-merged-into-branch2
  origin/some-other-branch-merged-into-branchN

Finally, the entire command will exclude the list of branches merged into any of branches given in the echo command from the list of branches not merged into master - thus giving us the list of branches not merged into any of given branches, eg:最后,整个命令将从未合并master的分支列表中排除合并echo命令中给出的任何分支的分支列表 - 从而为我们提供未合并到任何给定分支的分支列表,例如:

  origin/some-branch-not-merged-into-any-given-branch

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

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