Home Explore Blog CI



git

14th chunk of `Documentation/gitattributes.adoc`
e883d0e75b45a010ec8b2b6cb0a41122bdbacaeee0396e240000000100000fb5
 Textconv caching can speed up repeated diffs, such as those
   you might trigger by running `git log -p`.


Marking files as binary
^^^^^^^^^^^^^^^^^^^^^^^

Git usually guesses correctly whether a blob contains text or binary
data by examining the beginning of the contents. However, sometimes you
may want to override its decision, either because a blob contains binary
data later in the file, or because the content, while technically
composed of text characters, is opaque to a human reader. For example,
many postscript files contain only ASCII characters, but produce noisy
and meaningless diffs.

The simplest way to mark a file as binary is to unset the diff
attribute in the `.gitattributes` file:

------------------------
*.ps -diff
------------------------

This will cause Git to generate `Binary files differ` (or a binary
patch, if binary patches are enabled) instead of a regular diff.

However, one may also want to specify other diff driver attributes. For
example, you might want to use `textconv` to convert postscript files to
an ASCII representation for human viewing, but otherwise treat them as
binary files. You cannot specify both `-diff` and `diff=ps` attributes.
The solution is to use the `diff.*.binary` config option:

------------------------
[diff "ps"]
  textconv = ps2ascii
  binary = true
------------------------

Performing a three-way merge
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

`merge`
^^^^^^^

The attribute `merge` affects how three versions of a file are
merged when a file-level merge is necessary during `git merge`,
and other commands such as `git revert` and `git cherry-pick`.

Set::

	Built-in 3-way merge driver is used to merge the
	contents in a way similar to 'merge' command of `RCS`
	suite.  This is suitable for ordinary text files.

Unset::

	Take the version from the current branch as the
	tentative merge result, and declare that the merge has
	conflicts.  This is suitable for binary files that do
	not have a well-defined merge semantics.

Unspecified::

	By default, this uses the same built-in 3-way merge
	driver as is the case when the `merge` attribute is set.
	However, the `merge.default` configuration variable can name
	different merge driver to be used with paths for which the
	`merge` attribute is unspecified.

String::

	3-way merge is performed using the specified custom
	merge driver.  The built-in 3-way merge driver can be
	explicitly specified by asking for "text" driver; the
	built-in "take the current branch" driver can be
	requested with "binary".


Built-in merge drivers
^^^^^^^^^^^^^^^^^^^^^^

There are a few built-in low-level merge drivers defined that
can be asked for via the `merge` attribute.

text::

	Usual 3-way file level merge for text files.  Conflicted
	regions are marked with conflict markers `<<<<<<<`,
	`=======` and `>>>>>>>`.  The version from your branch
	appears before the `=======` marker, and the version
	from the merged branch appears after the `=======`
	marker.

binary::

	Keep the version from your branch in the work tree, but
	leave the path in the conflicted state for the user to
	sort out.

union::

	Run 3-way file level merge for text files, but take
	lines from both versions, instead of leaving conflict
	markers.  This tends to leave the added lines in the
	resulting file in random order and the user should
	verify the result. Do not use this if you do not
	understand the implications.


Defining a custom merge driver
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The definition of a merge driver is done in the `.git/config`
file, not in the `gitattributes` file, so strictly speaking this
manual page is a wrong place to talk about it.  However...

To define a custom merge driver `filfre`, add a section to your
`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:

----------------------------------------------------------------
[merge "filfre"]
	name = feel-free merge driver
	driver = filfre %O %A %B %L %P
	recursive = binary
----------------------------------------------------------------

Title: Customizing Git Merge and Diff Behavior
Summary
Git allows customization of merge and diff behavior through various attributes and configuration options, including marking files as binary, specifying custom merge drivers, and defining custom merge strategies to handle different types of files and merge scenarios.