Running echo "*.m diff=objc" >> .gitattributes
in a git repo tells git to use the obj-c diff algorithm when running git diff
. For the most part, this isn't much different from the standard diff algorithm. However, it adds one key benefit.
In a unified git diff, added lines are prefixed with a +
, and removed lines are prefixed with a -
. Unchanged lines are provided for context, and have no prefix. As added context, though, unified diffs have a @@
-prefixed line at the beginning of each hunk. Minimally, the @@
lines specify which lines in the file are being changed. It can also contain a label, if git can determine an appropriate label for that section of code.
By default, the label on a hunk context line is the name of the enclosing C function. That works great for C-ish code, but completely misses Obj-C method signatures. As a result, when diffing Obj-C files, the context label is either empty, or falls back to the last C-ish declaration it could find. This is usually entirely useless.
Adding the diff=objc
attribute to .m
files tells git how to find method headers. As a result, your context lines will now tell you what method the change was in, where previously they were useless.