9

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

Olivvv
  • 1,140
  • 1
  • 13
  • 37

2 Answers2

16

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'
phd
  • 82,685
  • 13
  • 120
  • 165
  • thanks. I will try once I am back at work tomorrow . – Olivvv Oct 14 '19 at 18:39
  • Ok, actually I retested and found something very unintuitive: it has to be : git diff -- . ":(exclude)package-lock.json" .With simple quotes it will not work and will show the diff for every file including package.json. – Olivvv Oct 15 '19 at 12:00
  • What OS? Windows? In Unix/Linux/MacOS there is no such major difference between quote types. – phd Oct 15 '19 at 12:53
  • Well the response is in your question i think... It is windows 10.... – Olivvv Oct 15 '19 at 13:02
  • Yep, I see. I'm not a w32 user so I seldom remember that difference. – phd Oct 15 '19 at 13:06
  • it is like living on a remote island far away – Olivvv Oct 15 '19 at 13:11
5

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 an explanatory comment, and is optional):

#exclude package-lock from git diff
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.

machineghost
  • 33,529
  • 30
  • 159
  • 234