Find all the direct descendants of a given commit [duplicate]
Asked Answered
W

1

8

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.

Womble answered 15/1, 2015 at 9:45 Comment(3)
you are looking for "child commits", not siblings... D is a child of B, but a sibling of ECliff
You're absolutely right, english is not my native langage and I mixed up the words. That explains why Google could not help me with this question :DWomble
you're welcome. FYI: sibling means "brother or sister"Cliff
R
6

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.

Reformism answered 15/1, 2015 at 11:6 Comment(4)
Why not use single quotes for the middle part and save the backslashes for escaping the curly braces? Easier to read, even shorter and somewhat "safer" when being extended.Convection
@DanielBöhmer #26783719Malamute
Oh, thank you very much for enlightening me! I had actually expected that bash would handle curly braces specially even inside double quotes.Convection
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".Cunnilingus

© 2022 - 2024 — McMap. All rights reserved.