Which option should I use when restricting git diff to a given set of file extensions?

Is it possible to limit git diff given set of file extensions?

+85
git file-extension
Dec 18 '11 at 21:01
source share
6 answers

Yes, if you make sure that git extends glob, not your shell, then it will correspond to any level, so something like this (quotes are important) should work fine.

 git diff -- '*.c' '*.h' 
+123
Dec 18 '11 at 9:33
source share

Or use shell globstar (which performs a recursive search) 1 2 :

 shopt -s globstar git diff -- *.py **/*.py 

or use find:

 find -name '*.py' -print0 | xargs -0 git diff -- 

Both are special names and proof of spaces. Although you might want to filter out directories with the extension .py :)




1 I like to do git diff -- {.,**}/*.py usually

2 When globstar is enabled, git diff -- **/*.py already includes ./*.py . In the Bash manpage: 'If it is followed by /, two adjacent * s will only correspond to directories and subdirectories.'

+6
Dec 18 '11 at
source share

For simple file templates, this works:

 $ git ls-files -zm '*.txt' | xargs --null git diff 

The space is safe, and you can also have several extensions:

 $ git ls-files -zm '*.h|*.c|*.cpp' | xargs --null git diff 
+1
Dec 18 '11 at 21:12
source share

Command line argument for extension.

 git diff *.py 

Alternatively, you can pass find to git diff :

 find . -name '*.py' -type f | git diff -- 
0
Dec 18 2018-11-18T00:
source share

None of the above answers work for me under git bash on Windows. I'm not sure if this is a thing of version (I'm using 1.8.4) or Windows / bash; also, in my case, I wanted to separate the two branches, where each branch had additional files that were not present in the other branch (the β€œfinds” were based on errors).

Anyway, this worked for me (in my example, looking for the difference between python files):

 git diff branch1 branch2 -- `git diff --summary branch1 branch2 | egrep '\.py$' | cut -d ' ' -f 5` 
0
Nov 08 '14 at 17:25
source share

git diff will only display differences in uninstalled files.

I found this question because I wanted to exclude .info files from git diff . I achieved this by git add *.info it with git add *.info , which reduces the remaining files.

0
Sep 14 '15 at 10:46
source share



All Articles