Exclude a directory from git diff

Git

Git Problem Overview


I'm wondering how I can exclude an entire directory from my Git diff. (In this case /spec). I'm creating a diff for our entire software release using the git diff command. However the changes to the specs are irrelevant for this procedure, and just create headaches. now I know i can do

git diff previous_release..current_release app/

This would create a diff for all the changes in the app directory, but not for instance, in the lib/ directory. Does anyone know how to accomplish this task? Thanks!

Edit: I just want to be clear, I know I can just string the parameters of all of my directories on the end, minus /spec. I was hoping there was a way to truly exclude a single directory in the command.

Git Solutions


Solution 1 - Git

Based on this answer you can also use:

git diff previous_release..current_release -- . ':!spec'

This is a newish git feature which allows excluding certain paths. It should be more reliable than various shell oneliners.

I'm posting this here because this question is still the #1 hit for "git diff exclude path".

Since Git 2.13 (Q2 2017), you can replace ! with ^. The latter doesn't need quotes. So you can write it as:

git diff previous_release..current_release -- . :^spec

Solution 2 - Git

Assuming you use bash, and you've enabled extended globbing (shopt -s extglob), you could handle that from the shell side:

git diff previous_release current_release !(spec)

Saves you having to list all other things.

Or, shell-agnostic:

git diff previous_release current_release --name-only | grep -v '^spec/' \
    | xargs git diff previous_release current_release --

You could wrap that up in a one-liner shell script to save yourself having to retype the arguments.

Solution 3 - Git

If you want to specify more than one path to exclude in a git diff, you just add additional exclusion parameters to the end, e.g. to exclude everything in vendor and bin directories from the stats:-

git diff --stat previous_release..current_release -- . ':!vendor' ':!bin'

Solution 4 - Git

You can try and unset the diff attribute for any files within the lib directory.
.gitattributes:

lib/* -diff

racl101 adds in the comments:

> Just to add to this, for those of you who want to limit the git diff output of files of a specific type, within a specific directory and its subdirectories, like all the JavaScript generated by Webpack's bundling of your .js files, for example, you can add this to .gitattributes:

dist/js/**/*.js -diff

> Then you don't see all that noise in your git diff output and it just shows as: Binary files ... differ which is more helpful.

Solution 5 - 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"

Some syntactical sugar applied

git diff -- ":!/\$1/"
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 -- ":!/\$1/"
}

Solution 6 - Git

Git diff now accepts a custom exclusion format: git diff -- ':(exclude)lib/*'

Be careful if you have a lot of repetitive folder names ('/app', '/lib', etc.), as this will exclude files relative to the current working directory AND the git root directory.

Solution 7 - Git

git diff app lib

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
QuestionMysrtView Question on Stackoverflow
Solution 1 - GitcdleonardView Answer on Stackoverflow
Solution 2 - GitCascabelView Answer on Stackoverflow
Solution 3 - GitDavid GrahamView Answer on Stackoverflow
Solution 4 - GitVonCView Answer on Stackoverflow
Solution 5 - GitjasonleonhardView Answer on Stackoverflow
Solution 6 - GitMaxPRaffertyView Answer on Stackoverflow
Solution 7 - GitJed SchneiderView Answer on Stackoverflow