Is it possible to filter the files in diff for their extensions?

Is it possible to filter the files in diff for their extensions?
Say for example

I have test1.json, test1.txt in a folder , I want to filter only test1.json using diff, I saw some answer in internet git diff – ‘*.json’ , but that didn’t work for me.

You can pass a list of files for git diff to compare against the current HEAD like this:

git diff -- FILE1 FILE2 ...

The file list can be created by a shell glob, that’s what

git diff -- *.json

does. However, *.json matches only files in the current directory. If your files are in subdirectories you have two options:

  1. Use find to build the file list, e.g.
git diff -- $(find . -name *.json)
  1. If you’re using bash you can set the globstar shell option and use ** to match any number of directories:
shopt -s globstar
git diff -- **/*.json
2 Likes