Git Exclude Files: With git diff command

Advertisement

Git provides the git diff command to check the difference between old and new changes from our code.

Sometimes, We intentionally want to ignore some files or folders from git diff.

In this article, I am showing you, How we can ignore some files or folders from the git diff command.

You are going to see:

Exclude Files from git diff Exclude Files from git diff

To exclude the files we need to follow the below syntax:

git diff -- . ':(exclude)FILENAME'

Or

Use the shorthand method.

git diff -- . ':!FILENAME'

Read more about git diff.

Let’s check some below examples for reference.

Top ↑

Exclude Single File from git diff Exclude Single File from git diff

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 the shorthand method as below:

git diff -- . ':(exclude)single.php'

Top ↑

Exclude Multiple Files from git diff Exclude Multiple Files from git diff

Same as excluding a single file, we can exclude multiple files by adding the same syntax.

We just need to add one space between both files.

E.g.

git diff -- . ':(exclude)single.php' ':(exclude)another.php

Or

Use the shorthand method as below:

git diff -- . ':!single.php' ':!another.php

Here, I have excluded the single.php and another.php files.

Top ↑

Exclude Files by File Extension from git diff Exclude Files by File Extension from git diff

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 deploy the project then we don’t need to watch the minified files. If the minified files are large then we can exclude them from the git diff command as below:

git diff -- . ':(exclude)*.min.js' ':(exclude)*.min.css'

Or

Use the shorthand method as below:

git diff -- . ':!*.min.css' ':!*.min.js'

Leave a Reply