Selectively ignore lines in git diff
git diff -I.
With a task like this where I want a quick check on a LOT of output (a few million lines I think) which probably hasn’t changed much, the text-based output and source control tools are a great fit. Also since I’m a confident git user, I can very easily re-run the script in the directory (already set up with the paths for my system) and when it finishes, check there’s nothing in git diff.
It worked great – until it didn’t! We added a nice piece of metadata showing when the file was last regenerated, which seemed like a good feature and I approved the change myself. But of course it means all the output files look changed each time. I needed a way to ignore only that change and still see anything except the timestamp updates.
Ignore patterns in git diff
The -I (it’s a capital “i” and is short for --ignore-matching-lines) accepts a regex and can be used with diff, log, show and probably other git commands. I’m using it with diff, like this:
git diff -I 'x-generated-date'
I only see diff entries that do not match this regex (okay, in this example it’s a string, but you can do cleverer things if you need them! I could put the datetime pattern in here to be super certain what I’m matching), which is exactly what I need. I know that this line will always be changed so we can ignore it and only inspect any other changes that are present.
To see just the list of files so I can get an idea of the blast radius on what I just changed, I can add --stat to my diff command.
Working with larger filesets and particularly with generated output where it’s useful to quickly sanity check any side effects, tricks like this are so useful – and I thought I would share. If you have additional strategies to offer me in return then I would love to hear about them in the comments, please!

