Sometimes there are files that you commit into your git repo that you don’t to
show up in a diff. One example could be text files that, while being text
files, are still not human readable. When executing a diff from the command
line using git diff
it may be desirable to remove these files from showing in
the diff.
Author Preferred Method
To do a permanent git diff exclude on one or more files:
-
Create a git repo specific diff driver:
git config diff.nodiff.command /bin/true
In the absence of
/bin/true
, i.e., on OS X you can use/usr/bin/true
orecho
. As long as whatever you execute returns a truthy (0
) exit code. If you want to make this change across the whole system, add the--global
flag at the end. -
Assign the newly created
nodiff
driver to the file(s) you wish to exclude. This can be done in either the.git/info/attributes
file, or if you’re feeling like you want to share this setting with the rest of the world you can add it into.gitattributes
in the source root, either way it should look like this:example-file.xml diff=nodiff
That’s it! Next time you run a git diff
it will show you the diff of
everything except the files listed in your attributes file as nodiff
.
Alternative Method
If you want something quick for a one-off situation there’s the non-permanent
alternative option of piping the diff output through filterdiff
:
git diff | filterdiff -p 1 -x example-file.xml
I hate this one because it’s harder to remember the options. The -p
and -x
options are the same as those from diff
. Read man diff
for more info
on diff
and the options you can pass to filterdiff
.
Sources
Read more on the original StackOverflow post.