For a casual or human-readable answer, use git show
as in TTT's post.
However, despite the clean use of --format
, the output of git show
is allowed to change: it is a "porcelain command" in git
's "plumbing/porcelain separation". (The same is true of git log --format
.) Changes might be unlikely, but you might also see errors and other human-directed output as git show
is intended for human consumption rather than script consumption.
For a more rigorous or spec-guaranteed list of parents, suitable for long-lived shell scripts or tool integration, you'll want to use a plumbing command like git cat-file
, similar to extracting a commit date without git log
.
git cat-file -p [commit-id] | sed -ne '/^$/q;/^parent/p' | wc -l
git cat-file -p
will pretty-print the object with the given ID. Revisions like HEAD
also work here.
sed
here will both filter and truncate the output:
-n
suppresses all output.
-e
uses a script specified on the command line.
/^$/q
tells sed
to quit if it sees a blank line, like the one separating commit metadata from its message. This prevents misinterpreting the word "parent" in your commit body text.
;
separates sed
commands.
/^parent/p
tells sed
to print only the lines that start with parent
, overriding -n
.
wc -l
prints the number of resulting lines.
Once again, for most cases git show
is a more idiomatic way of doing this, since it reads and parses the commit all in one command and has already been written with edge-cases and oddities in mind. However, for developers of long-lived git
tools, plumbing commands might be a more guaranteed solution.