3

When using git diff, how can I ignore changes which start with # ? For normal diff command, I would use something like:

diff <(grep -v '^#' file1) <(grep -v '^#' file2)

But none of the suggested solutions below work!

Is there really no way to see git diff with comments omitted?

UPDATE:

I tries this in my .gitconfig:

[pager]
    cdiff = diff-highlight

[alias] 
    cdiff = "diff -w --ignore-blank-lines -I'^#'"

When I do git cdiff, I get this error:

error: invalid option: -I^#

UPDATE2:

this is so frustrating. The syntax git diff -G'^[^#]' does not work reliably. Example:

$ cat 1.txt
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii
jjj

initialize new git repo:

$ git init && git add . && git commit -m "initial commit" -a

add only comment on top of file:

$ cat 1.txt
# comment
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii
jjj

so far, it works as expected. git diff -G'^[^#]' does not show the comment as change. But if I add a real change at the last line, and then do git diff -G'^[^#]' again, it then shows the added last line (as it should), but also the first line comment which it did not show before.

So basically, as soon as I add any non-comment change, git shows everything, even comments.

8
  • How about a git diff with a regex and adding an alias ‘cdiff’ to that command on your git config?
    – bretonics
    Jul 8, 2021 at 5:15
  • @bretonics - that sounds good. Can you explain how to set this up? Jul 8, 2021 at 5:32
  • Interesting. I haven't used diff-highlight so I just tried your pager configuration...it works on my end and I don't get your non-escaped colors. Do you happen to have any 3/4 bit color output configurations somewhere in your shell? I've experienced before that escaping them differently can cause issues and previously noticed that similar output when I as using color output configs like [[0;0m instead of "\033[0m"
    – bretonics
    Jul 9, 2021 at 16:39
  • @400theCat, and if replace -I'^#' with -G'^[^#]' ?
    – nezabudka
    Jul 11, 2021 at 6:05
  • @nezabudka - now it does not give an error, but is does not filter out comments. Basically no difference to normal diff. Jul 11, 2021 at 13:43

3 Answers 3

4
+500

It’s possible to do this by filtering all files before diffing them.

First, write a small helper, and call it stripcomments (executable, somewhere on your PATH):

#!/bin/sh -
exec grep -ve '^#' -- "$@"

Then configure a git diff mode using this helper:

git config diff.nocomments.textconv stripcomments

(this will configure the current repository).

Finally, set git attributes to have this helper used on all files:

echo '* diff=nocomments' >> .gitattributes

(in the top directory of your repository).

git diff will now ignore any lines starting with #. Note that this will affect line numbering.

If you want this as an alias, rather than overriding all uses of git diff, skip the git config command above and create your alias as follows:

git config alias.cdiff '-c diff.nocomments.textconv=stripcomments diff'

Now git cdiff will run git diff with # comments stripped out.

You can configure all this globally, rather than for each repository, by using git config --global and storing attributes in $XDG_CONFIG_HOME/git/attributes (~/.config/git/attributes) instead of .gitattributes.

See man gitattributes and man git-config for details.

0
2

Maybe you'd like to do a git diff with a regex and adding an alias cdiff to that command on your git config.

git diff -w -G'^[^#]'

Then adding an alias to your ~/.gitconfig for this command

[alias]
    cdiff = "diff -w -G'^[^#]'"

Not sure if that works to your intended purposes, so adjust as needed.

7
  • thank you, but the regex does not work for me. I think it does not match, because I am using diff-highlight coloriser. I will update my original post. Jul 9, 2021 at 1:22
  • diff -I'^#' and diff -G'^[^#] - All this will only work if the differences are only in the comments.
    – nezabudka
    Jul 9, 2021 at 10:50
  • cdiff = "diff -w --ignore-blank-lines -I'^#'"
    – nezabudka
    Jul 9, 2021 at 11:26
  • try rebuilding the config like this: [alias] cdiff = "diff -w --ignore-blank-lines -I'^#'" and replace [pager] diff = diff-highlight with [pager] cdiff = diff-highlight
    – nezabudka
    Jul 10, 2021 at 8:15
  • @nezabudka - this does not work. I get error: invalid option: -I^#. See my UPDATE2. Jul 11, 2021 at 4:16
1

All through an example:

mkdir cdiff && cd cdiff
git init
cat text.txt
# first comment
1
2
# midle comment
3

4
# last comment
git add test.txt
git commit -m 'first'

Now let's change it in the working directory:

cat test.txt
1

2
# first comment
# last comment
4
bye

Create a utility:

cat /usr/local/bin/git-cdiff
#!/bin/sh
diff --color -u <(grep -Ev '^(#|$)' $1) <(git show $2:$1 | grep -Ev '^(#|$)')
chmod 555 /usr/local/bin/git-cdiff

Compare the same file from a specific commit with one in working directory:

git cdiff test.txt HEAD

Or:

git cdiff test.txt <hash commit>
git cdiff test.txt a4ec1
--- /dev/fd/63  2021-07-09 08:09:48.581153921 +0300
+++ /dev/fd/62  2021-07-09 08:09:48.581153921 +0300
@@ -1,4 +1,4 @@
 1
 2
+3
 4
-bye
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .