I am trying to exclude a file (db/irrelevant.php
) from a Git diff. I have tried putting a file in the db
subdirectory called .gitattributes
Really good answer from KurzedMetal for what I needed to do, which was ability to see that the file has changed, but not to generate the diff, which could have been huge, in my case.
But a colleague of mine suggested approach that was even simpler and worked for me:
adding .gitattributes
file in the directory where the file to be ignored by git diff
resides with the following content:
file-not-to-diff.bin -diff
That still lets git status
"see" if the file changed. git diff
will also "see" that the file changed, but it will not generate the diff
.
That .bin
extension for the file in the example was deliberate. I do realize that this is the default behavior of git for binary files, and it does not require special handling with .gitattributes
. But in my case, these files were recognized as text files by git and "file" utility and this did the trick.
It's very simple, you can exclude what you want using standard unix commands, those commands are available under git bash even under windows environment. Below example shows how to exclude diff for pom.xml files, first check diff to master only for filenames, then using 'grep -v' exclude files which you don't want and then run again diff to master with prepapred list:
git diff master `git diff --name-only master | grep -v pom.xml`
git diff ':!db/irrelevant.php'
It's just ':!<path to file>'
after your diff command. The diff command can be as complicated as you like, and you can use wildcards like *.min.js
or add multiple excludes (space separate the quoted blocks) if you want.
If you want to do this only to visually inspect the diff, a visual diff tool (like Meld) will let you do it with a short command that's easy to remember.
First, set up a diff tool if you haven't already.
$ git config --global diff.tool = meld
Then, you can run a directory diff.
$ git difftool --dir-diff
You'll be able to browse diffs (by file) in Meld (or your tool of choice). Simply don't open the file you want to ignore.
You could set up a custom diff driver with a no op command and assign it to those files that should be ignored.
Create a repository specific diff driver with this command
git config diff.nodiff.command /bin/true
or for all your repos with --global
.
(If /bin/true
doesn't exist in MacOS, alternatives would be using /usr/bin/true
or echo
).
Then, assign the new diff driver to those files you want ignored in your .git/info/attributes
file.
irrelevant.php diff=nodiff
If this state is supposed to be shared with other developers you could use .gitattributes
instead of .git/info/attributes
and share the git config
command with your peers (through a documentation file or something).
Omg, drivers and awk to exclude a lousy file ? Since git 1.9 something you can:
git diff -- . ':(exclude)db/irrelevant.php' ':(exclude)db/irrelevant2.php'
Ah, elegance! See the quoted answer and for details this answer by @torek