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

git diff -G: Regex pattern to exclude a word

From
RNRaghavendra N <nraghavendra89@gmail.com>
Date
Jan 29, 2025, 06:05 UTC
Message-ID
<CA+U2-7GiHq9qhiyLPLgzOd1JK072gzkmZ+4vQ11V0QpKgPif=Q@mail.gmail.com>
Dear Members,
TL;DR: Question is - How to use the -G option in git diff to exclude a word?

I'm trying to understand how the -G option in git diff works. Specifically I'm trying to test a Regex which excludes a word from the git diff.

Consider the following setup:
    > git diff
    first-file.txt
    @@ -1 +1 @@
    -Lorem Ipsum is simply dummy text.
    +Lorem Ipsum is simply dummy text. tree
    second-file.txt
    @@ -1 +1 @@
    -Lorem Ipsum is simply dummy text.
    +Lorem Ipsum is simply dummy text. ball

Word "tree" is added in the first-file.txt and word "ball" is added in the second-file.txt.

If I run this `git diff -G "^.*tree.*"`, it shows only the first-file.txt.
    > git diff -G "^.*tree.*"
    first-file.txt
    @@ -1 +1 @@
    -Lorem Ipsum is simply dummy text.
    +Lorem Ipsum is simply dummy text. tree

This is correct because the Regex is checking if textual diff has an added or a deleted line that includes a word "tree".

Now if I try to exclude the word "tree" with this Regex "^([^t]|(t[^r])|(tr[^e])|(tre[^e]))*($|(t($|(r($|e$)))))", it should only show the second-file.txt. But the diff lists both the files.

    > git diff -G "^([^t]|(t[^r])|(tr[^e])|(tre[^e]))*($|(t($|(r($|e$)))))"
    first-file.txt
    @@ -1 +1 @@
    -Lorem Ipsum is simply dummy text.
    +Lorem Ipsum is simply dummy text. tree
    second-file.txt
    @@ -1 +1 @@
    -Lorem Ipsum is simply dummy text.
    +Lorem Ipsum is simply dummy text. ball

So the question is how does the -G option work in this case? And how to use the -G option to exclude a word?

Thanks, Raghavendra

Next: Chris Torek
Message 1 of 2 in “git diff -G: Regex pattern to exclude a word”
  1. Raghavendra NJan 29, 2025
  2. Chris TorekJan 29, 2025

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.