git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 11/12] doc: diff-tree: add Examples section and mention --stdin

From
Kkristofferhaugsbakk@fastmail.com <kristofferhaugsbakk@fastmail.com>
Date
Dec 2, 2026, 22:14 UTC
Message-ID
<Examples_--stdin.dfa@m5gid.xyz>
In-Reply-To
<CV_diff-tree_NUL_termination.def@m5gid.xyz>
From: Kristoffer Haugsbakk <code@khaugsbakk.name>

We added option -Z a few commits ago.[1] This option can be used by any command that uses revision.c:handle_revision_opt(...), but I had in mind git-diff-tree(1) in particular. That is because 'git diff-tree --stdin' outputs each record (commit or diff or both) immediately after it is processed. That is unlike git-rev-list(1)/git-show(1)/git-log(1) which wait until stdin is closed to and then outputs everything.

† 1: commit “revision: learn -Z (NUL termination)”

And this behavior is useful for feeding commit IDs, getting back some output, acting on it, and then feeding the next commit ID. This is facilitated by using -z so that you know a record is done after you read it. However, this does not work well for the default separator case; the delimiter is added *between* records, an in turn you have to give the Nth commit in order to get the NUL delimiter for the N-1 commit. Also, the last commit is not terminated.

However, with the new -Z option you can pretty-print commits on demand just like 'git format-rev --stdin-mode=revs':

    git cat-file --batch-command='%(objectname)' |
    git diff-tree --no-patch --format=<fmt> --always -Z --stdin

And you also get log-tree.c formatted output (e.g. 'Notes:' in --format=medium) just like commands like git-log(1). And you can do the same for diffs, or combine commits and diffs with --format=<fmt> --patch.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 Documentation/git-diff-tree.adoc | 40 ++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
diff --git a/Documentation/git-diff-tree.adoc b/Documentation/git-diff-tree.adoc
index 084be99e7cf..b73cf4d268d 100644
--- a/Documentation/git-diff-tree.adoc
+++ b/Documentation/git-diff-tree.adoc
@@ -25,6 +25,8 @@ This command outputs a line with the commit ID when applicable. Use
 
 Note that 'git diff-tree' can use the tree encapsulated in a commit object.
 
+See the "EXAMPLES" section at the end for a few examples.
+
 OPTIONS
 -------
 include::diff-options.adoc[]
@@ -129,6 +131,44 @@ include::pretty-formats.adoc[]
 
 include::diff-format.adoc[]
 
+EXAMPLES
+--------
+
+The default behavior is to show the commit ID and then the raw diff (see
+`--raw`):
+
+----
+$ git diff-tree HEAD
+00645eb9f4e515533cdf67376d481ffb4a796bcc
+:040000 040000 ac1844364599b5671e9e4da68eab0817cab2ac2d b39de8e87a84787fb7bc2b38e015540e6d336eae M	Documentation
+----
+
+But you can use this command to show normal diffs (`--patch`) and commit
+messages as well. You can for example get the same output as `git log`
+with:
+
+----
+$ git rev-list HEAD |
+git diff-tree --no-patch --stdin --always --format=medium
+----
+
+The above is of course longer to type and uses two processes instead
+of two. But this longer form with `--stdin` is more flexible than `git
+log --stdin`. That is because the linkgit:git-log[1] form only outputs
+anything after you close standard in (stdin). Meanwhile, `git diff-tree
+--stdin ...` outputs each commit or diff (or both) immediately. This
+means that you can use `-Z` to feed the commit, handle the output, and
+then continue with the next commit. The above can be tweaked to for
+example show the diff as well with `--patch`.
+
+----
+$ git diff-tree --no-patch --stdin --always --format=<fmt> -Z
+     [rev-list options]
+----
+
+The command `git diff-tree --stdin --patch` can be used together with
+linkgit:git-patch-id[1] to create a map from commit IDs to pach IDs.
+
 GIT
 ---
 Part of the linkgit:git[1] suite
-- 
2.56.1.5.g155936fbb2a
Previous: kristofferhaugsbakk@fastmail.comNext: kristofferhaugsbakk@fastmail.com
Message 12 of 13 in “revision: learn -Z for NUL termination”
  1. 00/12 revision: learn -Z for NUL terminationkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  2. 01/12 t4205: use correct 'setup' keywordkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  3. 02/12 t4205: add test coverage for NUL separator and --patchkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  4. 03/12 t4205: test blank line between commit and diff explicitlykristofferhaugsbakk@fastmail.com, Dec 2, 2026
  5. 04/12 t4013-diff-various.sh: test --stdin -z --patchkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  6. 05/12 log-tree: terminate cmt+diff when -z --patchkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  7. 06/12 t4205: test --diff-mergeskristofferhaugsbakk@fastmail.com, Dec 2, 2026
  8. 07/12 revision: learn -Z for NUL terminationkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  9. 08/12 revision: split out termination characterkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  10. 09/12 diff-tree: flush each diff when --stdinkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  11. 10/12 doc: diff-tree: tell about --no-commit-id in Descriptionkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  12. 11/12 doc: diff-tree: add Examples section and mention --stdinkristofferhaugsbakk@fastmail.com, Dec 2, 2026
  13. 12/12 doc: rev-list: complete diff-tree by mentioning --alwayskristofferhaugsbakk@fastmail.com, Dec 2, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.