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)
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)
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.
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/{} \
)
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
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...