change, such as adding a
clean/smudge filter or text/eol/ident attributes, merging anything
where the attribute is not in place would normally cause merge
conflicts.
To prevent these unnecessary merge conflicts, Git can be told to run a
virtual check-out and check-in of all three stages of each file that
needs a three-way content merge, by setting the `merge.renormalize`
configuration variable. This prevents changes caused by check-in
conversion from causing spurious merge conflicts when a converted file
is merged with an unconverted file.
As long as a "smudge->clean" results in the same output as a "clean"
even on files that are already smudged, this strategy will
automatically resolve all filter-related conflicts. Filters that do
not act in this way may cause additional merge conflicts that must be
resolved manually.
Generating diff text
~~~~~~~~~~~~~~~~~~~~
`diff`
^^^^^^
The attribute `diff` affects how Git generates diffs for particular
files. It can tell Git whether to generate a textual patch for the path
or to treat the path as a binary file. It can also affect what line is
shown on the hunk header `@@ -k,l +n,m @@` line, tell Git to use an
external command to generate the diff, or ask Git to convert binary
files to a text format before generating the diff.
Set::
A path to which the `diff` attribute is set is treated
as text, even when they contain byte values that
normally never appear in text files, such as NUL.
Unset::
A path to which the `diff` attribute is unset will
generate `Binary files differ` (or a binary patch, if
binary patches are enabled).
Unspecified::
A path to which the `diff` attribute is unspecified
first gets its contents inspected, and if it looks like
text and is smaller than core.bigFileThreshold, it is treated
as text. Otherwise it would generate `Binary files differ`.
String::
Diff is shown using the specified diff driver. Each driver may
specify one or more options, as described in the following
section. The options for the diff driver "foo" are defined
by the configuration variables in the "diff.foo" section of the
Git config file.
Defining an external diff driver
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The definition of a diff driver is done in `gitconfig`, not
`gitattributes` file, so strictly speaking this manual page is a
wrong place to talk about it. However...
To define an external diff driver `jcdiff`, add a section to your
`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:
----------------------------------------------------------------
[diff "jcdiff"]
command = j-c-diff
----------------------------------------------------------------
When Git needs to show you a diff for the path with `diff`
attribute set to `jcdiff`, it calls the command you specified
with the above configuration, i.e. `j-c-diff`, with 7
parameters, just like `GIT_EXTERNAL_DIFF` program is called.
See linkgit:git[1] for details.
If the program is able to ignore certain changes (similar to
`git diff --ignore-space-change`), then also set the option
`trustExitCode` to true. It is then expected to return exit code 1 if
it finds significant changes and 0 if it doesn't.
Setting the internal diff algorithm
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The diff algorithm can be set through the `diff.algorithm` config key, but
sometimes it may be helpful to set the diff algorithm per path. For example,
one may want to use the `minimal` diff algorithm for .json files, and the
`histogram` for .c files, and so on without having to pass in the algorithm
through the command line each time.
First, in `.gitattributes`, assign the `diff` attribute for paths.
------------------------
*.json diff=<name>
------------------------
Then, define a "diff.<name>.algorithm" configuration to specify the diff
algorithm, choosing from `myers`, `patience`, `minimal`, or `histogram`.
----------------------------------------------------------------
[diff "<name>"]
algorithm = histogram
----------------------------------------------------------------