Basically we use the Git diff command to check the old and new difference from our code.
Sometimes we intentionally want to ignore some files to watch them from the git diff.
So, In this article i’m showing you how we can ignore some files or folders from the git diff command.
Syntax Syntax
To exclude the files we need to follow below syntax:
git diff -- . ':(exclude)FILENAME'
Or
Use shorthand method.
git diff -- . ':!FILENAME'
Let’s check some below examples for reference.
Exclude single file Exclude single file
We can exclude the specific file with the git diff -- . ':(exclude)FILENAME
.
See the below example for reference in which I have excluded the single.php file from git diff.
E.g.
git diff -- . ':(exclude)single.php'
Or
Use shorthand method as below:
git diff -- . ':(exclude)single.php'
Exclude multiple files Exclude multiple files
Same as excluding single file, we can exclude multiple files by adding the same syntax.
We just need to add one space in between both files.
E.g.
git diff -- . ':(exclude)single.php' ':(exclude)another.php
Or
Use shorthand method as below:
git diff -- . ':!single.php' ':!another.php
Here, I have excluded the single.php and another.php files.
Exclude files by file extension Exclude files by file extension
Additionally we can exclude the files by file extension.
The most common example for this case is the minified JS and CSS files.
When we deply the project then we don’t need to watch the minified fiels. If the minified files are large then we can excude them from git diff command as below:
git diff -- . ':(exclude)*.min.js' ':(exclude)*.min.css'
Or
Use shorthand method as below:
git diff -- . ':!*.min.css' ':!*.min.js'