How to make git-diff ignore specific lines

Recently I use git for versioning Cisco router/switch configurations. One problem here is the ‘ntp clock-period’ line. Cisco equipments have a clock adjustment value for NTP in their configuration and its value changes without any manual configuration changes.

So even though I have made no changes in their configuration, git-diff shows lines like following.

-ntp clock-period 36027555
+ntp clock-period 36027579

Because it will be adjusted automatically, usually no need to save an updated value. So I want them disappear when I do git-diff. The git-diff has ‘-G’ option to show changes matched with regex. But it has no option to avoid matched lines. Generally in such a situation, we use ‘negative lookahead’ expression to filter specific words like below.

^(?!ntp clock-period)

Unfortunately git-diff seems not to support negative lookahead. So I have to write like below:

^([^n]|n[^t]|nt[^p]|ntp[^ ]|ntp [^c])

Though it is an ugly expression, it works well. The ‘ntp clock-period’ is a only command related NTP having sub command beginning with ‘c’ letter. So it’s enough to see if ‘ntp c’ is in the begininng of lines. Using a expression with ‘-G’ option like the above is one option.

Another option is using normal diff as an external-diff tool. It has ‘-I’ option to ignore matched lines with regex. For this Cisco specific purpose, I made following lines in my git config file.

        tool = diffcisco
[difftool "diffcisco"]
        cmd = \"diff\" -u -I \"^ntp clock-period\" \"$LOCAL\" \"$REMOTE\"

Now git-difftool (not git-diff) works fine. With ‘-y’ option we can stop annoying prompts.

$ git difftool -y

If you are a git-gui user, I recommend you the latter option. It’s comfortable with registered ‘git difftool -y’ in ‘Tools’ menu to check really changed lines. In git-gui with the former solution, unchanged configurations still remain in ‘Unstaged Changes’ area and will make annoying dialog box when clicked.

One more thing. If you have modified lines just before or after a ntp clock-period line, ‘diff -I’ will show you the modified lines with the ntp clock-period line. This is normal behavior of the -I option.