Want to exclude file from "git diff"

111,879

Solution 1

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'

(On Winodws, replace the single quotes ' by double quotes ".)

Ah, elegance! See the quoted answer and for details this answer by @torek

Solution 2

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).

Solution 3

This method is shorter than the accepted answers.

git diff 987200fbfb 878cee40ba -- ':!*.cs'

For more information about the different inclusion/exclusion possibilities read this other post

Solution 4

Simplest answer

git diff ':!db/irrelevant.php'

It's just ':!<path>' 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.

Solution 5

You can also use filterdiff program of the patchutils program collection to exclude some parts of a diff. For example:

git diff | filterdiff -p 1 -x db/irrelevant.php
Share:
111,879
Michael
Author by

Michael

Updated on July 08, 2022

Comments

  • Michael
    Michael almost 2 years

    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 with the line irrelevant.php -diff and I have also tried creating a file called .git/info/attributes containing db/irrelevant.php.

    In all cases, the db/irrelevant.php file is included in the diff as a Git binary patch. What I want is for the changes to that file to be ignore by the diff command. What am I doing wrong?