git merge-base --independent XYZ will tell you which ones are not merged with another branch yet.
In addition, git merge-base --is-ancestor XY will tell you if X is an ancestor of Y, but this is an inefficient way to implement git branch --merged because you need to run N ^ 2 times for N branches.
source share