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

Re: confused git diff -G

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 25, 2023, 17:21 UTC
Message-ID
<xmqqfs479gwx.fsf@gitster.g>
In-Reply-To
<ZOfgKQfqvef9FXZ6@nand.local>
Taylor Blau <me@ttaylorr.com> writes:
Show 14 quoted lines
> Then you can see `-G` has the effect of limiting the output of 'git
> diff' to just those file(s) whose diff matches the regular expression
> given to `-G`, like so:
>
>     $ git -C repo diff --stat HEAD^
>      a | 1 +
>      b | 1 +
>      2 files changed, 2 insertions(+)
>     $ git -C repo diff --stat HEAD^ -G a
>      a | 1 +
>      1 file changed, 1 insertion(+)
>     $ git -C repo diff --stat HEAD^ -G b
>      b | 1 +
>      1 file changed, 1 insertion(+)
All true.

As this feature is primarily designed to help "git log" to choose which commit to show and which commit to omit in its output, readers would not appreciate the usefulness of the feature, when shown in the context of "git diff".

Even more puzzling is how the "--full-diff" option works in combination with "-G" or "-S" (or --diff-filter=... for that matter)..

They make perfect sense as ingredients of a mechanism to choose which commit to show in the context of "git log", but their value is not immediately apparent in the context of "git diff". Continuing with your example [*1*], comparing what these two do would be illuminating:

    $ git -C repo diff --full-diff --stat -G b HEAD^
    $ git -C repo diff --full-diff --stat -G c HEAD^

The former should show changes to both a and b, and the latter should show nothing.

While the way how each of these behaves makes perfect sense at the logical level, it would be very puzzling why anybody may even want to use such a feature in the first place. Until you realize that comparing the previous commit and the current state (which happens to be identical to the current commit) is more or less a degenerated form of running "log -n <n> HEAD" with <n> set to 1, that is.

[Footnote]
*1* By the way, as an experienced mentor, avoid giving a command
    line example that has dashed options after revs and paths.  It
    may happen to do what you intended, but is confusing to readers.
Previous: Alexei Podtelezhnikov
Message 5 of 5 in “confused git diff -G”
  1. Alexei PodtelezhnikovAug 24, 2023
  2. Taylor BlauAug 24, 2023
  3. Alexei PodtelezhnikovAug 25, 2023
  4. Alexei PodtelezhnikovAug 25, 2023
  5. Junio C HamanoAug 25, 2023

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.