2

I. e. 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)

whyer
  • 783
  • 5
  • 16
  • `all branches that are not merged into none of these listed branches.` **not** merged into **none**? you mean "not merged into **any**"? – Kent Jun 30 '20 at 08:52
  • @Kent not sure how to say it correctly in English. probably you're correct. – whyer Jun 30 '20 at 19:16
  • @Kent I've just edited the question as you suggested but now I'm not sure if it is correct. are you sure in your English knowledge in this case? (my English skill is not enough here). imo, "not merged into any" seems like "not merged into A **or** not merged into B", whereas by "not merged into none" i meant "not merged into A **and** not merged into B". what do you think? – whyer Jun 30 '20 at 20:21
  • @Kent fyi, I've just asked it on English.stackexchange: https://english.stackexchange.com/questions/539307/how-to-ask-a-question-and-correctly-tell-its-logic-in-the-following-case – whyer Jun 30 '20 at 20:47

3 Answers3

3

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.

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.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • it is a very interesting solution using `awk` but could you please explain how the last `a[$0]++==2` argument works? looks like it is assumed to count duplicates and filter branches that are duplicated less than `2` times, but not sure, this argument looks like a magic here :) – whyer Jun 30 '20 at 22:02
  • 1
    @whyer the `a[$0]` is a hashtable(awk associative array), the key is the branch name, value is an int. You are right, for the same branch name, we are using the `a[]` to record how many times it shows up in the output. When we print the output, we take the occurrence (the value of a[]) `== 2` – Kent Jul 01 '20 at 08:37
  • thx. imo, it's worth to be appended to the answer! good explanation – whyer Jul 01 '20 at 15:50
1

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:

$ 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 --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), e.g.:

  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, e.g.:

  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, e.g.:

  origin/some-branch-not-merged-into-any-given-branch
whyer
  • 783
  • 5
  • 16
0

Question already asked I guess

How can I know if a branch has been already merged into 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...

Totoc1001
  • 338
  • 2
  • 11
  • that question asks if a branch is merged into a single branch (`master`), this question is about if it is merged into any of multiple branches – whyer Jun 30 '20 at 22:08
  • your suggested solution will filter out branches merged to **all** of given ones. the question is about how to filter out branches merged into **any** of them – whyer Jul 01 '20 at 15:56