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
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.
git diff
accepts an optional exclude
git diff -- ":(exclude)thingToExclude"
git diff -- ":(exclude)*/thingToExclude/*"
git diff -- ":(exclude)*/$1/*.png"
.bash_profile
or .zshrc
gde() {
# git diff exclude files or folders
# usage:
# gde fileOrFolderNameToExclude
git diff -- ":(exclude)*/$1/*"
}
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'
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
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
This method is shorter than the accepted answers.
git diff 987200fbfb 878cee40ba --stat -- ':!*.cs'
For more information about the different inclusion/exclusion possibilities read this other post