Selectively ignore lines in git diff

I have a things-as-code project that outputs mostly text-based formats, but a lot of them. To keep an eye on consistency, I rebuild all the outputs and dump them into a local git repository so I can very easily diff to spot any changes – which was fine until we added a build timestamp, so every file looks changed on every run! This post is about ignoring the matching line with 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!

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)