Is there an option to restrict git diff
to a given set of file extensions?

- 30,145
- 48
- 175
- 286

- 19,193
- 21
- 73
- 92
10 Answers
Yes, if you ensure that git expands a glob rather than your shell then it will match at any level so something like this (quotes are important) should work fine.
git diff -- '*.c' '*.h'

- 755,051
- 104
- 632
- 656
-
1Ok, my git version was too old. Works as advertised with 1.7.8. Excellent. – Mat Dec 19 '11 at 05:57
-
6Mine worked with brace expansion, a la `git diff -- *.{c,h,etc}` – Matt Fletcher Nov 25 '13 at 16:03
-
9the double dash is important eg. `git diff master HEAD -- "*filename.txt"` also useful is the `git diff master HEAD --name-only` – neaumusic Mar 02 '15 at 22:36
-
The single quotes (`''`) are also important! `git diff -- src/*.js` should be `git diff -- 'src/*.js'` – Nickofthyme Mar 18 '19 at 14:42
-
3Also must do this at the root directory of the git repository. – John Jiang Mar 21 '19 at 01:25
-
1Might be an interesting note: For use on Windows double quotes are required. – lcnittl Apr 25 '19 at 22:23
-
@CBBailey, why are the quotes important? I had a problem when i didn't use them, but I didn't understand why. my command was: `git diff --stat my-branch..master -- *.m` – FLemos Jun 28 '19 at 19:30
-
@lcnittl in Windows, you don't need quotes at all - I've tested it in Powershell v.5.1-7.3.1 – Kajsa Gauza Jan 17 '23 at 10:39
To include files recursively (including current dir) this worked for me:
git diff -- '***.py'

- 2,450
- 28
- 23
-
3For me it is the best solution. You could also exclude some files/directories. For example: ```git diff -- '***.py' ':!.Trashes'``` – Bartosz Mar 26 '19 at 12:23
-
-
IIRC the double asterisk means (possibly) nested directories (even empty) and the single asterisk and .py means the file name. So it should be a *.py file in the current or any nested directory. – Bohumir Zamecnik Jul 09 '20 at 07:51
-
`'*.py'` also includes py files in nested directories (tested on git version 2.28) – Blaise Nov 15 '22 at 09:59
Either use your shell's globstar (which does a recursive search)1,2:
shopt -s globstar
git diff -- *.py **/*.py
or use find:
find -name '*.py' -print0 | xargs -0 git diff --
Both of these are special-names and whitespace proof. Although you might want to filter for directories having the .py extension :)
1 I like to do git diff -- {.,**}/*.py
usually
2 When globstar is enabled, git diff -- **/*.py
already includes ./*.py
. In Bash's manpage: 'If followed by a /, two adjacent *s will match only directories and subdirectories.'
-
The `find` version doesn't work if there are no python files in the repo. So if you have a global hook, you will start diffing non python files :( – Matthieu Brucher May 28 '21 at 09:08
As tested on git version 2.18.0, the file extension should be quoted with double quotes. If you want to find the last differences between your local repository and the remote one, after pulling, you can use:
git diff YourBranchName@{1} YourBranchName --name-only "*.YourFileExtionsion"
For example:
git diff master@{1} origin/master --name-only "*.cs"

- 4,266
- 3
- 37
- 62

- 91
- 1
- 4
-
1It worked for me when I provided -- before the extension, like this, `git diff master@{1} origin/master --name-only -- "*.cs"` – 333 Dec 13 '18 at 12:13
For simple file patterns, this seems to work:
$ git ls-files -zm '*.txt' | xargs --null git diff
Whitespace safe, and you can have multiple extensions too:
$ git ls-files -zm '*.h|*.c|*.cpp' | xargs --null git diff

- 202,337
- 40
- 393
- 406
Command line argument for extension.
git diff *.py
In the alternative, you can pipe find
into git diff
:
find . -name '*.py' -type f | git diff --

- 47,733
- 20
- 85
- 108
-
please make the answer a bit more readable. You could have sufficed with `git diff *.py` and without the shouting headings – sehe Dec 18 '11 at 21:16
-
1
-
This is the only solution that worked for me, the quotes (etc.) did not work on Windows command prompt. – Ed Bayiates May 09 '19 at 20:17
-
Latest git version has now broken / removed this feature. As per @marjan.javid's answer below, it now requires double-quotes. No warning on this, git-diff just stopped working :(. Took a while to figure out what had changed (and why git-diff now returned zero results for the same inputs that always worked previously). – Adam Jun 19 '21 at 13:12
None of the answers above seem to work for me under git bash
on Windows. I am not sure if it is a version thing (I'm using 1.8.4) or Windows/bash thing; also, in my case, I wanted to diff two branches where each branch had additional files not present in the other branch (thus the 'find' based ones are remiss).
Anyway this worked for me (in my example, looking for a diff between python files):
git diff branch1 branch2 -- `git diff --summary branch1 branch2 | egrep '\.py$' | cut -d ' ' -f 5`

- 1,187
- 15
- 33
-
This https://stackoverflow.com/a/8554987/4233229 works, but one has to use double quotes instead of single for Windows – lcnittl Apr 25 '19 at 22:34
Attention that params order makes difference...for example:
git diff master --name-only --relative -- "**/*.ts" "**/*.tsx" "**/*.js" "**/*.jsx" "**/*.vue"
'diff' need to be followed with 'master'

- 317
- 3
- 10
-
Using `**/*.ext` doesn't capture `foo.ext` (not in a folder). So it should be `***.ext` etc. instead. – Matthew D. Scholefield Nov 03 '21 at 06:47
git diff
will only show differences in unstaged files.
I found this question because I wanted to exclude .info
files from git diff
. I achieved this by staging it with git add *.info
, which reduces the files left.

- 16,171
- 9
- 34
- 51
I wound up with this:
commit=<the_commit_hash_goes_here> && git diff --name-only $commit | grep -i Test | egrep -v '\.sql$' | xargs git diff $commit --
This shows diffs for the specified commit only if the filename contains the word 'test' (case insensitive) and does not end with .sql
, modify the pipeline as necessary for your case.

- 8,023
- 1
- 50
- 61