7

Is there a way to achieve the equivalent of git branch --merged using git plumbing commands?

I know there are commands like git for-each-ref which gives you the commit hashes and their corresponding ref names. Is there a command to tell whether a commit is reachable from another commit (which is basically what --merged do)?

EnToutCas
  • 1,297
  • 2
  • 15
  • 20
  • 1
    Why do you want to do this? –  Feb 27 '13 at 16:15
  • Because I want to do the equivalent of `git branch --merged` in scripts but don't want to deal with the porcelain output (e.g., "* master", "origin/HEAD->origin/master", etc) – EnToutCas Feb 27 '13 at 16:18
  • 1
    Well, you could `strace -f -o /tmp/branch.log -e trace=execve git branch --merged`, and see what `git` actually does. Alternatively, let `git` do it for you: `GIT_TRACE=2 git branch --merged`. – twalberg Feb 27 '13 at 17:11
  • 1
    If you don't want the output, send it to `/dev/null`... or just tell it to be quiet (`-q`) – vonbrand Mar 16 '13 at 18:27

2 Answers2

3

git merge-base --independent X Y Z will tell you which of those are not yet merged to another branch.

In addition, git merge-base --is-ancestor X Y will tell you whether X is an ancestor of Y, but that's an inefficient way to implement git branch --merged because you'll need to run it N^2 times for N branches.

Dan Fabulich
  • 37,506
  • 41
  • 139
  • 175
  • 1
    Apparently this is relatively new functionality; the old way of doing it was to run `git merge-base X Y` and check to see if the merge base was equal to X. – Dan Fabulich Jun 09 '13 at 06:24
0

The "plumbing" equivalent to git branch --merged is using git for-each-ref.

Something like this

git for-each-ref --merged HEAD |
  cut -f 2 |
  sed -n '/^refs\/heads\//{s@@@;p}'

and then if you want remotes you change it to something like

git for-each-ref --merged |
  cut -f 2 |
  sed -n '/^refs\/remotes\//{s@@@;p}'
CervEd
  • 3,306
  • 28
  • 25