简体   繁体   English

git branch-不合并<list of branches>

[英]git branch --no-merged <list of branches>

I've got 2 main branches master & archive . 我有2个主要分支masterarchive If some work was put on hold and I might need this in the future I archive that work by merging into archive branch. 如果某些工作被搁置,以后我可能会需要,我可以通过合并到archive分支中来归档该工作。 Labeling of unfinished work which needs to be kept is not an option for me because of some issues with labeling. 对于未完成的工作,需要保留标签,这对我来说是一种选择,因为存在一些标签问题。

I wanna find out the branches which are not merged in any of the 2 branches(branch1 & branch2). 我想找出两个分支(branch1&branch2)中没有合并的分支。 I would like to run something like 我想运行类似

git branch -a --no-merged branch1 branch2

When I run this way it kind'a works but the result is not what I need. 当我以这种方式运行时,它可以工作,但结果不是我所需要的。 I know one branch which was not merged in any of those 2 branches and it doesn't return me anything at all. 我知道一个分支没有合并到这两个分支中的任何一个中,它根本不会返回任何内容。 When I start playing with some other 2 branches it returns some results. 当我开始使用其他2个分支时,它会返回一些结果。

A more detailed example 更详细的例子

FC-o-o(b3)-o-o(b1)
  \         /
   o-o-o(b4)
   |\
   | o-o(b6)-o-o-o(b2)
   |  \       /
   |   o-o(b7)
   |     
   o-o(b5)             

FC - first commit
o - a commit
o(bN) - a branchN(one of the branch1,...,branch7) at a certain commit

So, if I run statement 所以,如果我运行语句

git branch -a --no-merged branch1 branch2

I wanted to get one single branch5, because it was not merged in a any of the 2 branches branch1 and branch2. 我想要一个单一的branch5,因为它没有合并到2个分支的任何一个branch1和branch2中。 Please note I have not found anything in documentation what should happen when I specify 2 branches after --no-merged. 请注意,我没有在文档中找到任何内容,当我在--no-merged之后指定2个分支时会发生什么。 It is just what I have tried to run to check if this will give me what I expect. 这就是我试图检查的结果是否可以满足我的期望。

branch3, branch4, branch6 and branch7 are not in the output because branch3,branch4,branch6和branch7不在输出中,因为

  • branch3 changes are in branch1 branch3的更改在branch1中
  • branch4 was merged into branch1 branch4合并到branch1
  • branch6 changes are in the branch2 branch6的更改在branch2中
  • branch7 was merged into branch2 branch7合并为branch2

There might be a simpler way to do this, but I have found none last time I checked, so I came up with this piece of script. 可能有一种更简单的方法来执行此操作,但是上次我没有找到任何方法,因此我想到了这段脚本。 It's formatted as 'alias' to be put into gitconfig file. 它的格式为'alias'放入gitconfig文件。 If you're not interested in that, remove the obvious alias prefix, and the '!' 如果您对此不感兴趣,请删除明显的别名前缀和“!” at beginning and && true at the end of it. 在开头,&&在结尾处为true。

[alias]
    unmer-excl = !git rev-list --all --no-merges --not `git rev-parse HEAD` $* | xargs -L1 git name-rev | grep -oE '[0-9a-f]{40}\\s[^\\~\\^]*' | grep -vE '\\stags/' && true

The name of it stands for " git unmer ged- excl usive". 它的名称代表“ git unmer ged- excl usive”。

If added to aliases, this command works like this: 如果添加到别名,则此命令的工作方式如下:

git unmer-excl
# ^ give me all not yet merged into current branch
111111 feature1
444444 feature1
222222 remotes/origin/experiment2
555555 remotes/origin/infrastructure
888888 remotes/origin/experiment2
333333 feature1
999999 remotes/origin/experiment2

as you see, this command lists commits along with their single best- guessed branch name. 如您所见,此命令列出了提交及其唯一的最佳猜测分支名称。 You can add branches to exclude: 您可以添加分支以排除:

git unmer-excl feature1 origin/experiment2
# ^ ...excluding anything that was already merged into feature1 or experiment2
555555 remotes/origin/infrastructure

This excludes not only fresh content on given branches, but everything that was already merged into that branch. 这不仅排除给定分支上的新鲜内容,还排除已经合并到该分支中的所有内容。

Output is listed in-order, from the most-recent commit to the oldest one. 输出从最新提交到最旧提交按顺序列出。 This is why it usually looks ugly if you're not used to it, but the advantages are: 这就是为什么如果您不习惯它通常看起来很难看的原因,但是优点是:

  • most recent first! 最近的第一!
  • shows commits, so you see "how much work" is pending to merge (few lines of commit IDs? fifteen screens?) 显示提交,因此您会看到“有多少工作”有待合并(几行提交ID?十五个屏幕?)
  • simple output, easy to cut | cut -d' ' -f2 | sort | uniq 输出简单,易于切割| cut -d' ' -f2 | sort | uniq | cut -d' ' -f2 | sort | uniq | cut -d' ' -f2 | sort | uniq and you get list of unmerged branches | cut -d' ' -f2 | sort | uniq ,您会得到未合并分支的列表

    git unmer-excl | git unmer-excl | cut -d' ' -f2 | 切-d''-f2 | sort | 排序 uniq feature1 remotes/origin/experiment2 remotes/origin/infrastructure uniq Feature1遥控器/起源/实验2遥控器/起源/基础设施

As for the latter, you can even add it before && true to the alias, I didn't want to it by default, as it doing it that simple damages sorting by most-recent and uses simple name sorting instead. 至于后者,您甚至可以在&& true之前将其添加到别名中,我默认情况下不希望使用它,因为这样做是为了避免最近发生的简单损坏排序,而是使用简单的名称排序。

For your original example: 对于原始示例:

FC-01-02(b3)-03-04(b1)
  \            /
   05-06-07(b4)
   |\
   | 08-09(b6)-10-11-12(b2)
   |   \         /
   |    13-14(b7)
   |     
   15-16(b5)

I changed o with commit numbers for better identification. 我用提交号更改了o以便更好地识别。 Examples below may differ in their output in terms of item ordering and branch names (is commit #14 could be labelled as B2 or B4, etc) 下面的示例在项目排序和分支名称方面的输出可能有所不同(提交14可以标记为B2或B4等)

git checkout (somewhere not on B5)
git unmer-excl b1 b2    | cut -d' ' -f2 | sort | uniq
b5

git checkout b4
git unmer-excl          | cut -d' ' -f2 | sort | uniq
b1
b2
b5

git checkout FC
git unmer-excl b1 b2
15 b5
16 b5

git checkout b6
git unmer-excl b3 b4
03 b1
04 b1
10 b2
11 b2
12 b2
13 b2
14 b2
15 b5
16 b5

我不知道这对您有帮助,但是您也可以尝试通过以下命令显示来自branch1和branch2的提交差异: git log branch1..branch2

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

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