Exclude a directory from git diff

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'

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.


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

Tags:

Git