Is there an option to restrict git diff
to a given set of file extensions?
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'
git diff -- *.{c,h,etc}
–
Celestinacelestine git diff master HEAD -- "*filename.txt"
also useful is the git diff master HEAD --name-only
–
Humble ''
) are also important! git diff -- src/*.js
should be git diff -- 'src/*.js'
–
Spinthariscope git diff --stat my-branch..master -- *.m
–
Lobel To include files recursively (including current dir) this worked for me:
git diff -- '***.py'
git diff -- '***.py' ':!.Trashes'
–
Moxie '*.py'
also includes py files in nested directories (tested on git version 2.28) –
Twylatwyman 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.'
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 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"
git diff master@{1} origin/master --name-only -- "*.cs"
–
Beeline 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
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 --
git diff *.py
and without the shouting headings –
Hilliard 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'
**/*.ext
doesn't capture foo.ext
(not in a folder). So it should be ***.ext
etc. instead. –
Fitzwater 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`
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.
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.
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
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
© 2022 - 2024 — McMap. All rights reserved.