How to filter git diff based on file extensions?
Asked Answered
B

12

248

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

Bashaw answered 18/12, 2011 at 21:1 Comment(0)
V
341

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'
Vincenty answered 18/12, 2011 at 21:33 Comment(8)
Ok, my git version was too old. Works as advertised with 1.7.8. Excellent.Dilapidation
Mine worked with brace expansion, a la git diff -- *.{c,h,etc}Celestinacelestine
the double dash is important eg. git diff master HEAD -- "*filename.txt" also useful is the git diff master HEAD --name-onlyHumble
The single quotes ('') are also important! git diff -- src/*.js should be git diff -- 'src/*.js'Spinthariscope
Also must do this at the root directory of the git repository.Peeling
Might be an interesting note: For use on Windows double quotes are required.Louiselouisette
@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 -- *.mLobel
@Louiselouisette in Windows, you don't need quotes at all - I've tested it in Powershell v.5.1-7.3.1Pearlypearman
T
38

To include files recursively (including current dir) this worked for me:

git diff -- '***.py'
Taut answered 10/8, 2018 at 11:36 Comment(4)
For me it is the best solution. You could also exclude some files/directories. For example: git diff -- '***.py' ':!.Trashes'Moxie
What do the triple asterisks do (compared to single asterisk)?Bradbury
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.Taut
'*.py' also includes py files in nested directories (tested on git version 2.28)Twylatwyman
H
10

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

Hilliard answered 18/12, 2011 at 21:19 Comment(1)
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 :(Diley
R
6

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"
Reine answered 2/11, 2018 at 10:2 Comment(1)
It worked for me when I provided -- before the extension, like this, git diff master@{1} origin/master --name-only -- "*.cs"Beeline
D
4

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
Dilapidation answered 18/12, 2011 at 21:12 Comment(0)
B
3

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 --
Burnie answered 18/12, 2011 at 21:8 Comment(4)
please make the answer a bit more readable. You could have sufficed with git diff *.py and without the shouting headingsHilliard
The 'shout headings' were #-comments in a shell script.Burnie
This is the only solution that worked for me, the quotes (etc.) did not work on Windows command prompt.Tautologize
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).Regurgitate
T
2

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'

Tensity answered 20/11, 2020 at 7:4 Comment(1)
Using **/*.ext doesn't capture foo.ext (not in a folder). So it should be ***.ext etc. instead.Fitzwater
A
1

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`
Anderson answered 8/11, 2014 at 17:25 Comment(1)
This https://mcmap.net/q/116042/-how-to-filter-git-diff-based-on-file-extensions works, but one has to use double quotes instead of single for WindowsLouiselouisette
N
0

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.

Nyaya answered 14/9, 2015 at 10:46 Comment(0)
W
0

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.

Warrin answered 6/4, 2018 at 21:4 Comment(0)
T
0

As some persons have pointed out in comments, on Windows you need to use " or no quotes at all. Single quotes doesn't work.

  • git diff -- ".c" ".h"
  • git diff -- *.c *.h
Tigre answered 22/12, 2023 at 8:54 Comment(0)
A
0

In Windows, notice double quotes in place of single quotes:

git diff --name-only -- "*.java"

or no quotes at all:

git diff --name-only -- *.java *.c
Awe answered 19/4 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.