Exclude file from "git diff"

GitDiffGit Diff

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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 - Git

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 3 - Git

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 4 - Git

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 - Git

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

Solution 6 - Git

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.

Solution 7 - Git

This one-line solution requires no other utils/downloads:

git diff `git status -s |grep -v ^\ D |grep -v file/to/exclude.txt |cut -b4-`

Where file/to/exclude.txt is the name of the file you would like to exclude, of course.

Edit: credit ksenzee for fixing deleted files breaking the diff.

Solution 8 - Git

Relative to the git root directory

git diff accepts an optional exclude

git diff -- ":(exclude)thingToExclude"

You might want to add some wild cards

git diff -- ":(exclude)*/thingToExclude/*"

Target specific file types

git diff -- ":(exclude)*/$1/*.png"
Or drop a little script for your dotfiles
Such as .bash_profile or .zshrc
gde() {
    # git diff exclude files or folders 
    # usage: 
    # gde fileOrFolderNameToExclude
    git diff -- ":(exclude)*/$1/*"
}

Solution 9 - Git

Building on existing answers, if you want to exclude a file pattern no matter where it is in the directory, use the corresponding git pathspec value of **:

git diff -- ':!**/yarn.lock'

Solution 10 - Git

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`

Solution 11 - Git

I do the following:

git add *pattern_to_exclude*
git diff
git reset HEAD .

I know it's not an elegant solution and it sometimes canot be used (eg. if you already have stuff staged) , but it does the trick without having to type a complicated command

Solution 12 - Git

similar to Ben Roux's solution, but sharing anyway:

git status --porcelain | grep -v $PATTERN_TO_EXCLUDE | awk '{print $2}' | xargs git diff

or, if the changes have already be committed locally:

git diff --name-only origin/master | grep -v $PATTERN_TO_EXCLUDE | xargs git diff origin/master

Examples:

git status --porcelain | grep -v docs | awk '{print $2}' | xargs git diff origin/master

git diff --name-only origin/master | grep -v docs | xargs git diff origin/master

Solution 13 - Git

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.

Solution 14 - Git

git diff remote/master..master --name-only -- ':!README.rst'

Gave me (git version 2.11.0)

fatal: There is nothing to exclude from by :(exclude) patterns.
Perhaps you forgot to add either ':/' or '.' ?
git diff remote/master..master --name-only -- './*' ':!README.rst'

Worked nicely, source: https://stackoverflow.com/a/30084612/5155484

Solution 15 - Git

My workaround

git add .
git reset <list of files to ignore>
git diff --cached

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichaelView Question on Stackoverflow
Solution 1 - GitMr_and_Mrs_DView Answer on Stackoverflow
Solution 2 - GitchtenbView Answer on Stackoverflow
Solution 3 - GitKurzedMetalView Answer on Stackoverflow
Solution 4 - GitdlssoView Answer on Stackoverflow
Solution 5 - GitSzilárd PfeifferView Answer on Stackoverflow
Solution 6 - Gituser3589608View Answer on Stackoverflow
Solution 7 - GitBen RouxView Answer on Stackoverflow
Solution 8 - GitjasonleonhardView Answer on Stackoverflow
Solution 9 - GitCarl GView Answer on Stackoverflow
Solution 10 - GitNieznany AnonimiecView Answer on Stackoverflow
Solution 11 - GitBlueMagmaView Answer on Stackoverflow
Solution 12 - GitChris MontoroView Answer on Stackoverflow
Solution 13 - GitmkasbergView Answer on Stackoverflow
Solution 14 - GitWilliam DesportesView Answer on Stackoverflow
Solution 15 - Gito01View Answer on Stackoverflow