Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No effect when doing git diff -- ':(exclude)package-lock.json'

I want to a do a diff but without package-lock.json which is huge and is a generated file so nothing interesting for me there. I tried both :

git diff -- ':(exclude)package-lock.json'

git diff -- ':!package-lock.json'

but it has just zero output. What am I doing wrong ? I am running git 2.21.0

like image 310
Olivvv Avatar asked Mar 04 '26 01:03

Olivvv


2 Answers

Before using exclude pathspec you need to list non-exclude paths. Dot, for example, or a magic root :/:

git diff -- . ':(exclude)package-lock.json'
git diff -- . ':!package-lock.json'
like image 200
phd Avatar answered Mar 05 '26 17:03

phd


If you don't want to have to add arguments every time you run git diff, you can add a .gitattributes file to the root of your repository, and then add the following line to it (well, actually two lines, but the first line is completely optional; it's an explanatory comment):

# exclude package-lock from git diff (see https://stackoverflow.com/q/58378724/5921)
package-lock.json -diff

After you add that file you will be able to do a regular old git diff, with no additional arguments, and still not have to look at all the package-lock.json diff.

This marks package-lock.json file as binary, so git will not display the line-by-line diff.

See https://git-scm.com/docs/gitattributes#_marking_files_as_binary

like image 30
machineghost Avatar answered Mar 05 '26 17:03

machineghost



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!