git diff, how can I ignore changes which start with
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?
I tries this in my
[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^#
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.
Its possible to do this by filtering all files before
First, write a small helper, and call it
stripcomments (executable, somewhere on your
#!/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'
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
~/.config/git/attributes) instead of
External links referenced by this document: