How do I find the next commit in Git? child/children of ref
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Git, every commit stores its parent commit ids, but it does not store a direct list of its children. That means there is no single built-in "next commit" in the general case. A commit can have multiple children across branches, or none at all.
Why "Next Commit" Is Ambiguous
Git history is a graph, not a simple linked list. If commit A is followed by commits B and C on different branches, both of them are children of A.
So before asking for the "next" commit, decide what you really mean:
- any child commit of a given commit
- the next commit along a specific branch
- all descendants reachable from a ref
- the first commit after a point in a linear ancestry path
The command you use depends on that meaning.
Find Direct Children Of A Commit
A practical way to ask "which commits list this commit as a parent" is:
Example output:
This means commit abc1234 has two direct children: def5678 and 1122334.
The reason this works is that git rev-list --children prints each commit followed by its direct children.
Find The Next Commit On A Specific Branch
If you care about one branch only, first restrict the history to that branch and then inspect ancestry:
This prints descendants of abc1234 that are on the ancestry path to main. The first line in reverse order is usually the nearest child on that path:
That is often the closest thing to "the next commit" when you mean "the next commit that eventually led to the current main branch tip".
Visualize The Graph
Sometimes the quickest answer is a graph view:
This shows branch structure and makes it obvious whether a commit has one child, several children, or no visible descendants in the refs you care about.
If the history is complicated, add the commit id and inspect nearby nodes visually.
Find Branches That Contain A Commit
If you are really asking "where does history continue from here", branches are often the useful unit:
This tells you which local or remote branches still include that commit. You can then inspect the branch-specific "next" commit from there.
A Scriptable Approach
If you want direct children in a script, parse rev-list --children output:
This prints one direct child per line and avoids manual graph inspection.
Merge Commits Change The Story
Remember that child relationships are not always linear:
- a normal commit may have zero, one, or many children
- a merge commit has multiple parents
- a branch point creates multiple children for the same parent
That is why Git does not expose a single universal "child commit" command. The graph can branch in either direction.
Common Pitfalls
- Assuming every commit has exactly one next commit.
- Using
^or~syntax, which walks to parents, not children. - Forgetting to limit the search to a specific branch when that is what you actually need.
- Looking only at
git logoutput without--graphand missing the branch structure. - Confusing descendants with direct children. A descendant can be many commits away.
Summary
- Git stores parent links, not child links, so "next commit" is not a primitive concept.
- Use
git rev-list --all --childrento find direct children of a commit. - Use
git rev-list --ancestry-path commit..branch --reverse | head -n 1for the next commit along a branch path. - Use
git log --graph --oneline --allwhen the history shape matters. - Always clarify whether you need direct children, descendants, or the next commit on a specific branch.

