Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude a directory from git diff

Tags:

git

People also ask

How to exclude a file from git diff?

We can exclude the specific file with the git diff -- . ':(exclude)FILENAME .

Does git diff use diff?

Comparing changes with git diffgit diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.


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

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.


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'

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.


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.


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/"
}