Find all the direct descendants of a given commit

2k Views Asked by At

How can I find all the commits that have a given commit as parent?

For instance, if I have this Git commit graph,

   G   H   I   J
    \ /     \ /
     D   E   F
      \  |  / \
       \ | /   |
        \|/    |
         B     C
          \   /
           \ /
            A

I'd like to get a list of all the direct descendants of B : D, E and F.

1

There are 1 best solutions below

4
On

You can use git rev-list --parents and filter the children of a parent with grep and awk

git rev-list --all --parents | grep "^.\{40\}.*<PARENT_SHA1>.*" | awk '{print $1}'

Replace <PARENT_SHA1> with the sha-1 hash of your B commit.

EDIT

Any time you pipe grep | awk like this, consider just using AWK alone: awk '/<PARENT_SHA1>$/ { print $1 }' should do it, for example. I would go so far as to say this is a "Useless Use of Grep", much like "Useless Use of Cat".

Thanks shadowtalker. Here is the command line that works using just awk:

 git rev-list --all --parents | awk '$0 ~ /^.{40}.*<PARENT_SHA1>.*/ {print $1}'

It's a bit shorter and one less process for the shell.