Git development
 help / color / mirror / Atom feed
* [PATCH] doc: git-diff: rewrite EXAMPLES section
@ 2026-11-03 15:33 Julia Evans via GitGitGadget
  2026-11-03 17:16 ` Julia Evans
  2026-11-03 19:44 ` Junio C Hamano
  0 siblings, 2 replies; 7+ messages in thread
From: Julia Evans via GitGitGadget @ 2026-11-03 15:33 UTC (permalink / raw)
  To: git; +Cc: D. Ben Knoble, Julia Evans, Julia Evans

From: Julia Evans <julia@jvns.ca>

The top piece of feedback users always give about man pages is always
that they want better examples that are relevant to them.

The EXAMPLES section's structure has remained mostly unchanged since
2007, which was before we knew how people were going to use `git diff`.
Update it with ways of using `git diff` that are popular with Git
users, gathered by asking Mastodon users for their most commonly used
incantations.

Add a note about the EXAMPLES section in the DESCRIPTION since otherwise
readers are unlikely to know it's there. `git diff` is a command where
it's more important than usual to have examples because the command has
so many different forms.

Some design choices:

- Use `--merge-base` instead of `...` since several users said they
  find .. vs ... confusing. It's much more obvious what --merge-base
  does.
- Use a table format instead of the footnote-style formatting since the
  footnote style is quite difficult to follow. It's easier to follow
  if the description is right next to the command it's describing.
- Link the options in the examples to make them faster to look up
- Use sub-subsection headings for the different parts to make
  them more visually distinct and easy to scan.

Co-Authored-By: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
Signed-off-by: Marie Claire LeBlanc Flanagan <hello@marieflanagan.com>
Signed-off-by: Julia Evans <julia@jvns.ca>
---
    doc: git-diff: rewrite EXAMPLES section
    
    A couple of self-review comments:
    
     * May want to add back the git diff AUTO_MERGE, nobody mentioned it but
       it's often nice to include 1 cool little-known example to try to
       promote it.
     * I'm not 100% sure about the table format, it does make it more
       compact which is really nice and makes it much more readable, but on
       an 80-character terminal there is some wrapping. One potential con is
       that it means we can't include as many details about what the command
       does. From my point of view that's a good thing because in a "cheat
       sheet" format it's very important to be concise, and people can
       always read the rest of the man page for the details.
    
    Here's what one of the tables looks like on an 80-character terminal
    with a little bit of wrapping.
    
       ┌─────────────────────────┬───────────────────────┐
       │Ignore all whitespace    │ git diff              │
       │changes                  │ --ignore-all-space    │
       ├─────────────────────────┼───────────────────────┤
       │Show which individual    │ git diff --word-diff  │
       │words changed instead of │                       │
       │the whole line           │                       │
       └─────────────────────────┴───────────────────────┘
    

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2293%2Fjvns%2Fdiff-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2293/jvns/diff-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2293

 Documentation/diff-options.adoc |   5 ++
 Documentation/git-diff.adoc     | 149 ++++++++++++++++++--------------
 2 files changed, 87 insertions(+), 67 deletions(-)

diff --git a/Documentation/diff-options.adoc b/Documentation/diff-options.adoc
index e605d28678..2dc54fa00e 100644
--- a/Documentation/diff-options.adoc
+++ b/Documentation/diff-options.adoc
@@ -201,6 +201,7 @@ diff" algorithm internally.
 
 include::diff-algorithm-option.adoc[]
 
+[[stat]]
 `--stat[=<width>[,<name-width>[,<count>]]]`::
 	Generate a diffstat. By default, as much space as necessary
 	will be used for the filename part, and the rest for the graph
@@ -318,12 +319,14 @@ Without this option, pathnames with "unusual" characters are quoted as
 explained for the configuration variable `core.quotePath` (see
 linkgit:git-config[1]).
 
+[[name_only]]
 `--name-only`::
 	Show only the name of each changed file in the post-image tree.
 	The file names are often encoded in UTF-8.
 	For more information see the discussion about encoding in the linkgit:git-log[1]
 	manual page.
 
+[[name_status]]
 `--name-status`::
 	Show only the name(s) and status of each changed file. See the description
 	of the `--diff-filter` option on what the status letters mean.
@@ -417,6 +420,7 @@ endif::git-diff[]
 	Ignore changes in amount of whitespace.  This ignores whitespace
 	at line end, and considers all other sequences of one or
 	more whitespace characters to be equivalent.
+[[ignore_all_space]]
 `ignore-all-space`::
 	Ignore whitespace when comparing lines. This ignores differences
 	even if one line has whitespace where the other line has none.
@@ -432,6 +436,7 @@ endif::git-diff[]
 	used to override configuration settings. It is the same as
 	`--color-moved-ws=no`.
 
+[[word_diff]]
 `--word-diff[=<mode>]`::
 	By default, words are delimited by whitespace; see
 	`--word-diff-regex` below.  The _<mode>_ defaults to `plain`, and
diff --git a/Documentation/git-diff.adoc b/Documentation/git-diff.adoc
index 272331afba..8d9de30f64 100644
--- a/Documentation/git-diff.adoc
+++ b/Documentation/git-diff.adoc
@@ -23,6 +23,9 @@ between the index and a tree, changes between two trees, changes resulting
 from a merge, changes between two blob objects, or changes between two
 files on disk.
 
+The <<EXAMPLES,EXAMPLES>> section has examples of the most common ways
+to diff.
+
 `git diff [<options>] [--] [<path>...]`::
 
 	This form is to view the changes you made relative to
@@ -160,78 +163,90 @@ section "3-Way Merge" for detailed information.
 
 include::diff-format.adoc[]
 
+[[EXAMPLES]]
 EXAMPLES
 --------
 
-Various ways to check your working tree::
-+
-------------
-$ git diff            <1>
-$ git diff --cached   <2>
-$ git diff HEAD       <3>
-$ git diff AUTO_MERGE <4>
-------------
-+
-<1> Changes in the working tree not yet staged for the next commit.
-<2> Changes between the index and your last commit; what you
-    would be committing if you run `git commit` without `-a` option.
-<3> Changes in the working tree since your last commit; what you
-    would be committing if you run `git commit -a`
-<4> Changes in the working tree you've made to resolve textual
-    conflicts so far.
-
-Comparing with arbitrary commits::
-+
-------------
-$ git diff test            <1>
-$ git diff HEAD -- ./test  <2>
-$ git diff HEAD^ HEAD      <3>
-------------
-+
-<1> Instead of using the tip of the current branch, compare with the
-    tip of "test" branch.
-<2> Instead of comparing with the tip of "test" branch, compare with
-    the tip of the current branch, but limit the comparison to the
-    file "test".
-<3> Compare the version before the last commit and the last commit.
-
-Comparing branches::
-+
-------------
-$ git diff topic master    <1>
-$ git diff topic..master   <2>
-$ git diff topic...master  <3>
-------------
-+
-<1> Changes between the tips of the topic and the master branches.
-<2> Same as above.
-<3> Changes that occurred on the master branch since when the topic
-    branch was started off it.
+Diff uncommitted changes
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+`git diff` will show the files from the "before" column as deleted and
+from the "after" column as added.
+
+[cols="1,1"]
+|===
+|Show unstaged changes
+|`git diff`
+
+|Show staged changes
+|`git diff --staged`
+
+|Show all changes since the latest commit
+|`git diff HEAD`
+|===
+
+Diff a specific file/directory
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+You can usually put a filename or directory at the end to restrict the
+diff to that file/directory. You don't always need to put a `--`, but it
+makes it unambiguous.
+
+[cols="1,1"]
+|===
+|Show how `file.txt` has changed since `main`
+|`git diff main -- file.txt`
+
+|Show unstaged changes for `file.txt`
+|`git diff -- file.txt`
+
+|Show unstaged changes in `dir/`
+|`git diff -- ./dir`
+|===
+
+Diff branches or commits
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+[cols="1,1"]
+|===
+|Show changes on the `topic` branch since it branched off `main`
+|`git diff --merge-base main topic`
+
+|Show changes between the `topic` branch and the `main` branch
+|`git diff main topic`
+
+|Show changes between commit `ff11cc` and `HEAD`
+|`git diff ff11cc HEAD`
+|===
+
+Options to summarize the output
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+[cols="1,1"]
+|===
+|Show how many lines in each file were changed
+|`git diff` <<stat,`--stat`>>
+
+|Only show names of changed files
+|`git diff`  <<name_only,`--name-only`>>
+
+|Only show filenames and whether they were modified/deleted/added
+|`git diff` <<name_status,`--name-status`>>
+|===
+
+Options to display the diff in another way
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+[cols="1,1"]
+|===
+|Ignore all whitespace changes
+|`git diff` <<ignore_all_space,`--ignore-all-space`>>
+
+|Show which individual words changed instead of the whole line
+|`git diff` <<word_diff,`--word-diff`>>
+|===
 
-Limiting the diff output::
-+
-------------
-$ git diff --diff-filter=MRC            <1>
-$ git diff --name-status                <2>
-$ git diff arch/i386 include/asm-i386   <3>
-------------
-+
-<1> Show only modification, rename, and copy, but not addition
-    or deletion.
-<2> Show only names and the nature of change, but not actual
-    diff output.
-<3> Limit diff output to named subtrees.
 
-Munging the diff output::
-+
-------------
-$ git diff --find-copies-harder -B -C  <1>
-$ git diff -R                          <2>
-------------
-+
-<1> Spend extra cycles to find renames, copies and complete
-    rewrites (very expensive).
-<2> Output diff in reverse.
 
 CONFIGURATION
 -------------

base-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] doc: git-diff: rewrite EXAMPLES section
  2026-11-03 15:33 [PATCH] doc: git-diff: rewrite EXAMPLES section Julia Evans via GitGitGadget
@ 2026-11-03 17:16 ` Julia Evans
  2026-11-03 19:44 ` Junio C Hamano
  1 sibling, 0 replies; 7+ messages in thread
From: Julia Evans @ 2026-11-03 17:16 UTC (permalink / raw)
  To: Julia Evans, git; +Cc: D. Ben Knoble

>      * I'm not 100% sure about the table format, it does make it more
>        compact which is really nice and makes it much more readable, but on
>        an 80-character terminal there is some wrapping. One potential con is
>        that it means we can't include as many details about what the command
>        does. From my point of view that's a good thing because in a "cheat
>        sheet" format it's very important to be concise, and people can
>        always read the rest of the man page for the details.

While working on something else immediately after sending this,
I learned that Asciidoc has support for horizontal definition lists
which I think would make more sense than a table here.
And they can be easily changed to a vertical definition list by
just removing the [horizontal] if there's an issue in the future.
https://docs.asciidoctor.org/asciidoc/latest/lists/horizontal/ 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] doc: git-diff: rewrite EXAMPLES section
  2026-11-03 15:33 [PATCH] doc: git-diff: rewrite EXAMPLES section Julia Evans via GitGitGadget
  2026-11-03 17:16 ` Julia Evans
@ 2026-11-03 19:44 ` Junio C Hamano
  2026-11-03 19:52   ` Julia Evans
  1 sibling, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2026-11-03 19:44 UTC (permalink / raw)
  To: Julia Evans via GitGitGadget; +Cc: git, D. Ben Knoble, Julia Evans

"Julia Evans via GitGitGadget" <gitgitgadget@gmail.com> writes:

>     Here's what one of the tables looks like on an 80-character terminal
>     with a little bit of wrapping.
>     
>        ┌─────────────────────────┬───────────────────────┐
>        │Ignore all whitespace    │ git diff              │
>        │changes                  │ --ignore-all-space    │
>        ├─────────────────────────┼───────────────────────┤
>        │Show which individual    │ git diff --word-diff  │
>        │words changed instead of │                       │
>        │the whole line           │                       │
>        └─────────────────────────┴───────────────────────┘

Personally, I would not want to see these line-wrapped sentences in
tables, which makes it impossible to cut-and-paste.  But preferences
vary from person to person, of course, on these things.

> -Various ways to check your working tree::
> -+
> -------------
> -$ git diff            <1>
> -$ git diff --cached   <2>
> -$ git diff HEAD       <3>
> -$ git diff AUTO_MERGE <4>
> -------------
> -+
> -<1> Changes in the working tree not yet staged for the next commit.
> -<2> Changes between the index and your last commit; what you
> -    would be committing if you run `git commit` without `-a` option.
> -<3> Changes in the working tree since your last commit; what you
> -    would be committing if you run `git commit -a`
> -<4> Changes in the working tree you've made to resolve textual
> -    conflicts so far.
> ...
> +[cols="1,1"]
> +|===
> +|Show unstaged changes
> +|`git diff`
> +
> +|Show staged changes
> +|`git diff --staged`

Do not sneak this change into an unrelated "change the format to use
table to make it readable" topic.  Doing this change to just this
single example would be a severe regression.

Look at SYNOPSIS of "git help diff" and notice there is no mention
of "--staged".  The description on "--cached" explains it as a
synonym for it.  If we wanted to promote "--staged" (which I would
not personally support), we'd need to do matching changes to the
main part of the text.

> +|Show all changes since the latest commit
> +|`git diff HEAD`
> +|===

Yes, as you mentioned in the commentary, loss of AUTO_MERGE is a
regression.  I do not see a reason why you needed to drop it?  Is it
because some external limitation like you cannot have a table with
more than 3 rows or something?

Thanks.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] doc: git-diff: rewrite EXAMPLES section
  2026-11-03 19:44 ` Junio C Hamano
@ 2026-11-03 19:52   ` Julia Evans
  2026-11-03 20:33     ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Julia Evans @ 2026-11-03 19:52 UTC (permalink / raw)
  To: Junio C Hamano, Julia Evans; +Cc: git, D. Ben Knoble


>> +[cols="1,1"]
>> +|===
>> +|Show unstaged changes
>> +|`git diff`
>> +
>> +|Show staged changes
>> +|`git diff --staged`
>
> Do not sneak this change into an unrelated "change the format to use
> table to make it readable" topic.  Doing this change to just this
> single example would be a severe regression.

I can change it back, I didn't realize it would be an issue.

I changed it because several people said that they find `--cached`
confusing and that `--staged` was easier to understand because
that's the language Git normally uses in its UI when talking about
staged changes.

> Look at SYNOPSIS of "git help diff" and notice there is no mention
> of "--staged".  The description on "--cached" explains it as a
> synonym for it.  If we wanted to promote "--staged" (which I would
> not personally support), we'd need to do matching changes to the
> main part of the text.
>
>> +|Show all changes since the latest commit
>> +|`git diff HEAD`
>> +|===
>
> Yes, as you mentioned in the commentary, loss of AUTO_MERGE is a
> regression.  I do not see a reason why you needed to drop it?  Is it
> because some external limitation like you cannot have a table with
> more than 3 rows or something?

I dropped it because I wanted to start with just the commands that
people said they found useful, and it can be hard to introduce
too many "new" commands that aren't well-known already.

That said I think `git diff AUTO_MERGE` seems useful and I'm happy
to put it back.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] doc: git-diff: rewrite EXAMPLES section
  2026-11-03 19:52   ` Julia Evans
@ 2026-11-03 20:33     ` Junio C Hamano
  2026-11-03 20:45       ` Julia Evans
  0 siblings, 1 reply; 7+ messages in thread
From: Junio C Hamano @ 2026-11-03 20:33 UTC (permalink / raw)
  To: Julia Evans; +Cc: Julia Evans, git, D. Ben Knoble

"Julia Evans" <julia@jvns.ca> writes:

>>> +|Show staged changes
>>> +|`git diff --staged`
>>
>> Do not sneak this change into an unrelated "change the format to use
>> table to make it readable" topic.  Doing this change to just this
>> single example would be a severe regression.
>
> I can change it back, I didn't realize it would be an issue.

Changing this example alone without checking its fit in the wider
context is what made it problematic.  I do not personally support
"cached to staged" because I consider it mostly a meaningless
churn, but that is different from saying I will veto it.

I just wanted to see a bit more consistency; without changing the
main text, the change to the example made it look like a very odd
thing to have there.  You see the option named "--staged" but it
is explained nowhere other than being a synonym to another option
that is much more widely explained.

> I dropped it because I wanted to start with just the commands that
> people said they found useful, and it can be hard to introduce
> too many "new" commands that aren't well-known already.
>
> That said I think `git diff AUTO_MERGE` seems useful and I'm happy
> to put it back.

FWIW, what is in the current document was added by choosing what
were found useful.  So you would want to think twice (note: this
is very different from "do not change") before removing existing
ones or replacing them with something else.  After spending a
significant amount of effort on these documentation updates you are
making, if a new person came and rewrote them again without
understanding or paying much attention to your reasoning behind how
you arrived at your version, I would imagine that you would feel
terrible.  Please give the last person who wrote these documents
the same courtesy and respect.

Thanks.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] doc: git-diff: rewrite EXAMPLES section
  2026-11-03 20:33     ` Junio C Hamano
@ 2026-11-03 20:45       ` Julia Evans
  2026-11-03 21:42         ` Junio C Hamano
  0 siblings, 1 reply; 7+ messages in thread
From: Julia Evans @ 2026-11-03 20:45 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Julia Evans, git, D. Ben Knoble



On Tue, Nov 3, 2026, at 3:33 PM, Junio C Hamano wrote:
> "Julia Evans" <julia@jvns.ca> writes:
>
>>>> +|Show staged changes
>>>> +|`git diff --staged`
>>>
>>> Do not sneak this change into an unrelated "change the format to use
>>> table to make it readable" topic.  Doing this change to just this
>>> single example would be a severe regression.
>>
>> I can change it back, I didn't realize it would be an issue.
>
> Changing this example alone without checking its fit in the wider
> context is what made it problematic.  I do not personally support
> "cached to staged" because I consider it mostly a meaningless
> churn, but that is different from saying I will veto it.
>
> I just wanted to see a bit more consistency; without changing the
> main text, the change to the example made it look like a very odd
> thing to have there.  You see the option named "--staged" but it
> is explained nowhere other than being a synonym to another option
> that is much more widely explained.
>
>> I dropped it because I wanted to start with just the commands that
>> people said they found useful, and it can be hard to introduce
>> too many "new" commands that aren't well-known already.
>>
>> That said I think `git diff AUTO_MERGE` seems useful and I'm happy
>> to put it back.
>
> FWIW, what is in the current document was added by choosing what
> were found useful.  So you would want to think twice (note: this
> is very different from "do not change") before removing existing
> ones or replacing them with something else.  After spending a
> significant amount of effort on these documentation updates you are
> making, if a new person came and rewrote them again without
> understanding or paying much attention to your reasoning behind how
> you arrived at your version, I would imagine that you would feel
> terrible.  Please give the last person who wrote these documents
> the same courtesy and respect.

It's very hard to understand from the Git history the reasons for
adding examples that were added 20 years ago, for example
in 803f498c03b where these were added, especially since the commit
messages from 20 years ago tend to be a lot more terse.

+Limiting the diff output::
++
+------------
+$ git diff --diff-filter=MRC <1>
+$ git diff --name-status -r <2>
+$ git diff arch/i386 include/asm-i386 <3>

I do hear that AUTO_MERGE was added more recently.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] doc: git-diff: rewrite EXAMPLES section
  2026-11-03 20:45       ` Julia Evans
@ 2026-11-03 21:42         ` Junio C Hamano
  0 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2026-11-03 21:42 UTC (permalink / raw)
  To: Julia Evans; +Cc: Julia Evans, git, D. Ben Knoble

"Julia Evans" <julia@jvns.ca> writes:

> It's very hard to understand from the Git history the reasons for
> adding examples that were added 20 years ago, for example
> in 803f498c03b where these were added, especially since the commit
> messages from 20 years ago tend to be a lot more terse.

Sure.  But that is why you send patches here to invite people to
comment on.

> I do hear that AUTO_MERGE was added more recently.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-03 21:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-03 15:33 [PATCH] doc: git-diff: rewrite EXAMPLES section Julia Evans via GitGitGadget
2026-11-03 17:16 ` Julia Evans
2026-11-03 19:44 ` Junio C Hamano
2026-11-03 19:52   ` Julia Evans
2026-11-03 20:33     ` Junio C Hamano
2026-11-03 20:45       ` Julia Evans
2026-11-03 21:42         ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox