Exclude a directory from git diff

0 votes
asked Dec 7, 2010 by mysrt

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.

6 Answers

0 votes
answered Dec 7, 2010 by jed-schneider
git diff app lib
0 votes
answered Dec 7, 2010 by vonc

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

lib/* -diff
0 votes
answered Dec 7, 2010 by jefromi

Assuming you use bash, and you've enabled 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.

0 votes
answered Dec 31, 2015 by cdleonard

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

0 votes
answered Dec 4, 2016 by david-graham

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'
0 votes
answered Sep 15, 2017 by maxprafferty

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.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter