blob: 4338d023d9a313edae0bae8b2ac8087a3d15a003 [file] [log] [blame]
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001gitattributes(5)
2================
3
4NAME
5----
Nguyễn Thái Ngọc Duy1b81d8c2018-05-20 20:40:02 +02006gitattributes - Defining attributes per path
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07007
8SYNOPSIS
9--------
Gustaf Hendebye5b5c1d2008-08-31 18:00:27 +020010$GIT_DIR/info/attributes, .gitattributes
Junio C Hamano88e7fdf2007-04-19 20:48:03 -070011
12
13DESCRIPTION
14-----------
15
16A `gitattributes` file is a simple text file that gives
17`attributes` to pathnames.
18
19Each line in `gitattributes` file is of form:
20
Martin Ågren8d75a1d2019-03-06 07:30:18 +010021 pattern attr1 attr2 ...
Junio C Hamano88e7fdf2007-04-19 20:48:03 -070022
Johannes Sixt3f74c8e2009-02-25 08:54:42 +010023That is, a pattern followed by an attributes list,
Nguyễn Thái Ngọc Duy860a74d2017-01-27 18:01:50 -080024separated by whitespaces. Leading and trailing whitespaces are
25ignored. Lines that begin with '#' are ignored. Patterns
26that begin with a double quote are quoted in C style.
27When the pattern matches the path in question, the attributes
28listed on the line are given to the path.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -070029
30Each attribute can be in one of these states for a given path:
31
32Set::
33
34 The path has the attribute with special value "true";
35 this is specified by listing only the name of the
36 attribute in the attribute list.
37
38Unset::
39
40 The path has the attribute with special value "false";
41 this is specified by listing the name of the attribute
42 prefixed with a dash `-` in the attribute list.
43
44Set to a value::
45
46 The path has the attribute with specified string value;
47 this is specified by listing the name of the attribute
48 followed by an equal sign `=` and its value in the
49 attribute list.
50
51Unspecified::
52
Johannes Sixt3f74c8e2009-02-25 08:54:42 +010053 No pattern matches the path, and nothing says if
Junio C Hamanob9d14ff2007-04-24 13:46:02 -070054 the path has or does not have the attribute, the
55 attribute for the path is said to be Unspecified.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -070056
Johannes Sixt3f74c8e2009-02-25 08:54:42 +010057When more than one pattern matches the path, a later line
Junio C Hamanob9d14ff2007-04-24 13:46:02 -070058overrides an earlier line. This overriding is done per
Jeff Kingb635ed92018-03-20 00:14:54 -040059attribute.
60
61The rules by which the pattern matches paths are the same as in
62`.gitignore` files (see linkgit:gitignore[5]), with a few exceptions:
63
64 - negative patterns are forbidden
65
66 - patterns that match a directory do not recursively match paths
67 inside that directory (so using the trailing-slash `path/` syntax is
68 pointless in an attributes file; use `path/**` instead)
Junio C Hamano88e7fdf2007-04-19 20:48:03 -070069
Thomas Ackermann2de9b712013-01-21 20:17:53 +010070When deciding what attributes are assigned to a path, Git
Junio C Hamano88e7fdf2007-04-19 20:48:03 -070071consults `$GIT_DIR/info/attributes` file (which has the highest
72precedence), `.gitattributes` file in the same directory as the
Jason Merrill20ff3ec2009-04-06 11:03:36 -040073path in question, and its parent directories up to the toplevel of the
74work tree (the further the directory that contains `.gitattributes`
Petr Onderka6df42ab2010-09-01 00:42:43 +020075is from the path in question, the lower its precedence). Finally
76global and system-wide files are considered (they have the lowest
77precedence).
Junio C Hamano88e7fdf2007-04-19 20:48:03 -070078
Nguyen Thai Ngoc Duy40701ad2012-10-10 20:55:52 +070079When the `.gitattributes` file is missing from the work tree, the
80path in the index is used as a fall-back. During checkout process,
81`.gitattributes` in the index is used and then the file in the
82working tree is used as a fall-back.
83
Jeff King90b22902008-03-27 01:31:00 -040084If you wish to affect only a single repository (i.e., to assign
Petr Onderka6df42ab2010-09-01 00:42:43 +020085attributes to files that are particular to
86one user's workflow for that repository), then
Jeff King90b22902008-03-27 01:31:00 -040087attributes should be placed in the `$GIT_DIR/info/attributes` file.
88Attributes which should be version-controlled and distributed to other
89repositories (i.e., attributes of interest to all users) should go into
Petr Onderka6df42ab2010-09-01 00:42:43 +020090`.gitattributes` files. Attributes that should affect all repositories
91for a single user should be placed in a file specified by the
Nguyễn Thái Ngọc Duyda0005b2015-03-11 16:32:45 -040092`core.attributesFile` configuration option (see linkgit:git-config[1]).
Huynh Khoi Nguyen Nguyen684e40f2012-06-22 11:03:25 +020093Its default value is $XDG_CONFIG_HOME/git/attributes. If $XDG_CONFIG_HOME
94is either not set or empty, $HOME/.config/git/attributes is used instead.
Petr Onderka6df42ab2010-09-01 00:42:43 +020095Attributes for all users on a system should be placed in the
96`$(prefix)/etc/gitattributes` file.
Jeff King90b22902008-03-27 01:31:00 -040097
Stefan Bellerfaa4e8c2017-01-27 18:01:52 -080098Sometimes you would need to override a setting of an attribute
Michael Haggerty09225702011-08-03 15:41:30 +020099for a path to `Unspecified` state. This can be done by listing
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700100the name of the attribute prefixed with an exclamation point `!`.
101
102
Joanna Wang2232a882023-11-16 05:44:37 +0000103RESERVED BUILTIN_* ATTRIBUTES
104-----------------------------
105
106builtin_* is a reserved namespace for builtin attribute values. Any
107user defined attributes under this namespace will be ignored and
108trigger a warning.
109
110`builtin_objectmode`
111~~~~~~~~~~~~~~~~~~~~
112This attribute is for filtering files by their file bit modes (40000,
113120000, 160000, 100755, 100644). e.g. ':(attr:builtin_objectmode=160000)'.
114You may also check these values with `git check-attr builtin_objectmode -- <file>`.
115If the object is not in the index `git check-attr --cached` will return unspecified.
116
117
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700118EFFECTS
119-------
120
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100121Certain operations by Git can be influenced by assigning
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700122particular attributes to a path. Currently, the following
123operations are attributes-aware.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700124
125Checking-out and checking-in
126~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700128These attributes affect how the contents stored in the
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700129repository are copied to the working tree files when commands
Nguyễn Thái Ngọc Duyd787d312019-03-29 17:39:05 +0700130such as 'git switch', 'git checkout' and 'git merge' run.
131They also affect how
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100132Git stores the contents you prepare in the working tree in the
Thomas Rast0b444cd2010-01-10 00:33:00 +0100133repository upon 'git add' and 'git commit'.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700134
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200135`text`
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700136^^^^^^
137
Alex Henrie66960772023-05-02 22:46:56 -0600138This attribute marks the path as a text file, which enables end-of-line
139conversion: When a matching file is added to the index, the file's line
140endings are normalized to LF in the index. Conversely, when the file is
141copied from the index to the working directory, its line endings may be
142converted from LF to CRLF depending on the `eol` attribute, the Git
143config, and the platform (see explanation of `eol` below).
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700144
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700145Set::
146
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200147 Setting the `text` attribute on a path enables end-of-line
Alex Henrie66960772023-05-02 22:46:56 -0600148 conversion on checkin and checkout as described above. Line endings
149 are normalized to LF in the index every time the file is checked in,
150 even if the file was previously added to Git with CRLF line endings.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700151
152Unset::
153
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100154 Unsetting the `text` attribute on a path tells Git not to
Junio C Hamanobbb896d2008-08-30 14:35:15 -0700155 attempt any end-of-line conversion upon checkin or checkout.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700156
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200157Set to string value "auto"::
158
Alex Henrie66960772023-05-02 22:46:56 -0600159 When `text` is set to "auto", Git decides by itself whether the file
160 is text or binary. If it is text and the file was not already in
161 Git with CRLF endings, line endings are converted on checkin and
162 checkout as described above. Otherwise, no conversion is done on
163 checkin or checkout.
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200164
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700165Unspecified::
166
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100167 If the `text` attribute is unspecified, Git uses the
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200168 `core.autocrlf` configuration variable to determine if the
169 file should be converted.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700170
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100171Any other value causes Git to act as if `text` has been left
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200172unspecified.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700173
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200174`eol`
175^^^^^
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700176
Alex Henrie66960772023-05-02 22:46:56 -0600177This attribute marks a path to use a specific line-ending style in the
178working tree when it is checked out. It has effect only if `text` or
179`text=auto` is set (see above), but specifying `eol` automatically sets
180`text` if `text` was left unspecified.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700181
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200182Set to string value "crlf"::
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700183
Alex Henrie66960772023-05-02 22:46:56 -0600184 This setting converts the file's line endings in the working
185 directory to CRLF when the file is checked out.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700186
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200187Set to string value "lf"::
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700188
Alex Henrie66960772023-05-02 22:46:56 -0600189 This setting uses the same line endings in the working directory as
190 in the index when the file is checked out.
191
192Unspecified::
193
194 If the `eol` attribute is unspecified for a file, its line endings
195 in the working directory are determined by the `core.autocrlf` or
196 `core.eol` configuration variable (see the definitions of those
197 options in linkgit:git-config[1]). If `text` is set but neither of
198 those variables is, the default is `eol=crlf` on Windows and
199 `eol=lf` on all other platforms.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700200
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200201Backwards compatibility with `crlf` attribute
202^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203
204For backwards compatibility, the `crlf` attribute is interpreted as
205follows:
206
207------------------------
208crlf text
209-crlf -text
210crlf=input eol=lf
211------------------------
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700212
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200213End-of-line conversion
214^^^^^^^^^^^^^^^^^^^^^^
215
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100216While Git normally leaves file contents alone, it can be configured to
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200217normalize line endings to LF in the repository and, optionally, to
218convert them to CRLF when files are checked out.
219
Torsten Bögershausene28eae32016-08-26 22:18:48 +0200220If you simply want to have CRLF line endings in your working directory
221regardless of the repository you are working with, you can set the
222config variable "core.autocrlf" without using any attributes.
223
224------------------------
225[core]
226 autocrlf = true
227------------------------
228
229This does not force normalization of text files, but does ensure
230that text files that you introduce to the repository have their line
231endings normalized to LF when they are added, and that files that are
232already normalized in the repository stay normalized.
233
234If you want to ensure that text files that any contributor introduces to
235the repository have their line endings normalized, you can set the
236`text` attribute to "auto" for _all_ files.
237
238------------------------
239* text=auto
240------------------------
241
242The attributes allow a fine-grained control, how the line endings
243are converted.
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100244Here is an example that will make Git normalize .txt, .vcproj and .sh
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200245files, ensure that .vcproj files have CRLF and .sh files have LF in
246the working directory, and prevent .jpg files from being normalized
247regardless of their content.
248
249------------------------
Torsten Bögershausen65237282016-06-28 10:01:13 +0200250* text=auto
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200251*.txt text
Torsten Bögershausen65237282016-06-28 10:01:13 +0200252*.vcproj text eol=crlf
253*.sh text eol=lf
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200254*.jpg -text
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200255------------------------
256
Torsten Bögershausene28eae32016-08-26 22:18:48 +0200257NOTE: When `text=auto` conversion is enabled in a cross-platform
258project using push and pull to a central repository the text files
259containing CRLFs should be normalized.
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200260
Torsten Bögershausene28eae32016-08-26 22:18:48 +0200261From a clean working directory:
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200262
263-------------------------------------------------
Torsten Bögershausene28eae32016-08-26 22:18:48 +0200264$ echo "* text=auto" >.gitattributes
Torsten Bögershausen94729352017-11-16 17:38:28 +0100265$ git add --renormalize .
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200266$ git status # Show files that will be normalized
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200267$ git commit -m "Introduce end-of-line normalization"
268-------------------------------------------------
269
270If any files that should not be normalized show up in 'git status',
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200271unset their `text` attribute before running 'git add -u'.
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200272
273------------------------
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200274manual.pdf -text
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200275------------------------
276
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100277Conversely, text files that Git does not detect can have normalization
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200278enabled manually.
279
280------------------------
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200281weirdchars.txt text
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200282------------------------
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700283
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100284If `core.safecrlf` is set to "true" or "warn", Git verifies if
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100285the conversion is reversible for the current setting of
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100286`core.autocrlf`. For "true", Git rejects irreversible
287conversions; for "warn", Git only prints a warning but accepts
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100288an irreversible conversion. The safety triggers to prevent such
289a conversion done to the files in the work tree, but there are a
290few exceptions. Even though...
291
Thomas Rast0b444cd2010-01-10 00:33:00 +0100292- 'git add' itself does not touch the files in the work tree, the
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100293 next checkout would, so the safety triggers;
294
Thomas Rast0b444cd2010-01-10 00:33:00 +0100295- 'git apply' to update a text file with a patch does touch the files
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100296 in the work tree, but the operation is about text files and CRLF
297 conversion is about fixing the line ending inconsistencies, so the
298 safety does not trigger;
299
Thomas Rast0b444cd2010-01-10 00:33:00 +0100300- 'git diff' itself does not touch the files in the work tree, it is
301 often run to inspect the changes you intend to next 'git add'. To
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100302 catch potential problems early, safety triggers.
303
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700304
Lars Schneider107642f2018-04-15 20:16:07 +0200305`working-tree-encoding`
306^^^^^^^^^^^^^^^^^^^^^^^
307
308Git recognizes files encoded in ASCII or one of its supersets (e.g.
309UTF-8, ISO-8859-1, ...) as text files. Files encoded in certain other
310encodings (e.g. UTF-16) are interpreted as binary and consequently
311built-in Git text processing tools (e.g. 'git diff') as well as most Git
312web front ends do not visualize the contents of these files by default.
313
314In these cases you can tell Git the encoding of a file in the working
315directory with the `working-tree-encoding` attribute. If a file with this
Elijah Newren031fd4b2019-11-05 17:07:20 +0000316attribute is added to Git, then Git re-encodes the content from the
Lars Schneider107642f2018-04-15 20:16:07 +0200317specified encoding to UTF-8. Finally, Git stores the UTF-8 encoded
318content in its internal data structure (called "the index"). On checkout
Elijah Newren031fd4b2019-11-05 17:07:20 +0000319the content is re-encoded back to the specified encoding.
Lars Schneider107642f2018-04-15 20:16:07 +0200320
321Please note that using the `working-tree-encoding` attribute may have a
322number of pitfalls:
323
324- Alternative Git implementations (e.g. JGit or libgit2) and older Git
325 versions (as of March 2018) do not support the `working-tree-encoding`
326 attribute. If you decide to use the `working-tree-encoding` attribute
327 in your repository, then it is strongly recommended to ensure that all
328 clients working with the repository support it.
Andreas Heidukad471942018-10-22 22:45:43 +0200329+
330For example, Microsoft Visual Studio resources files (`*.rc`) or
331PowerShell script files (`*.ps1`) are sometimes encoded in UTF-16.
332If you declare `*.ps1` as files as UTF-16 and you add `foo.ps1` with
333a `working-tree-encoding` enabled Git client, then `foo.ps1` will be
334stored as UTF-8 internally. A client without `working-tree-encoding`
335support will checkout `foo.ps1` as UTF-8 encoded file. This will
336typically cause trouble for the users of this file.
337+
Alexander Blesiused318512019-03-16 11:34:39 +0100338If a Git client that does not support the `working-tree-encoding`
339attribute adds a new file `bar.ps1`, then `bar.ps1` will be
Andreas Heidukad471942018-10-22 22:45:43 +0200340stored "as-is" internally (in this example probably as UTF-16).
341A client with `working-tree-encoding` support will interpret the
342internal contents as UTF-8 and try to convert it to UTF-16 on checkout.
343That operation will fail and cause an error.
Lars Schneider107642f2018-04-15 20:16:07 +0200344
Lars Schneidere92d6222018-04-15 20:16:10 +0200345- Reencoding content to non-UTF encodings can cause errors as the
346 conversion might not be UTF-8 round trip safe. If you suspect your
347 encoding to not be round trip safe, then add it to
348 `core.checkRoundtripEncoding` to make Git check the round trip
349 encoding (see linkgit:git-config[1]). SHIFT-JIS (Japanese character
350 set) is known to have round trip issues with UTF-8 and is checked by
351 default.
352
Lars Schneider107642f2018-04-15 20:16:07 +0200353- Reencoding content requires resources that might slow down certain
354 Git operations (e.g 'git checkout' or 'git add').
355
356Use the `working-tree-encoding` attribute only if you cannot store a file
357in UTF-8 encoding and if you want Git to be able to process the content
358as text.
359
360As an example, use the following attributes if your '*.ps1' files are
361UTF-16 encoded with byte order mark (BOM) and you want Git to perform
362automatic line ending conversion based on your platform.
363
364------------------------
365*.ps1 text working-tree-encoding=UTF-16
366------------------------
367
368Use the following attributes if your '*.ps1' files are UTF-16 little
369endian encoded without BOM and you want Git to use Windows line endings
Yash Bhatambaree6e15192019-03-06 05:23:10 +0000370in the working directory (use `UTF-16LE-BOM` instead of `UTF-16LE` if
Torsten Bögershausenaab2a1a2019-01-30 16:01:52 +0100371you want UTF-16 little endian with BOM).
372Please note, it is highly recommended to
Lars Schneider107642f2018-04-15 20:16:07 +0200373explicitly define the line endings with `eol` if the `working-tree-encoding`
374attribute is used to avoid ambiguity.
375
376------------------------
377*.ps1 text working-tree-encoding=UTF-16LE eol=CRLF
378------------------------
379
380You can get a list of all available encodings on your platform with the
381following command:
382
383------------------------
384iconv --list
385------------------------
386
387If you do not know the encoding of a file, then you can use the `file`
388command to guess the encoding:
389
390------------------------
391file foo.ps1
392------------------------
393
394
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700395`ident`
396^^^^^^^
397
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100398When the attribute `ident` is set for a path, Git replaces
Jan Krüger2c850f12008-10-30 19:14:33 +0100399`$Id$` in the blob object with `$Id:`, followed by the
Junio C Hamano3fed15f2007-04-21 19:09:02 -070040040-character hexadecimal blob object name, followed by a dollar
401sign `$` upon checkout. Any byte sequence that begins with
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100402`$Id:` and ends with `$` in the worktree file is replaced
403with `$Id$` upon check-in.
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700404
405
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700406`filter`
407^^^^^^^^
408
Wincent Colaiutac05ef932007-11-14 08:51:41 +0100409A `filter` attribute can be set to a string value that names a
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700410filter driver specified in the configuration.
411
Wincent Colaiutac05ef932007-11-14 08:51:41 +0100412A filter driver consists of a `clean` command and a `smudge`
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700413command, either of which can be left unspecified. Upon
Wincent Colaiutac05ef932007-11-14 08:51:41 +0100414checkout, when the `smudge` command is specified, the command is
415fed the blob object from its standard input, and its standard
416output is used to update the worktree file. Similarly, the
417`clean` command is used to convert the contents of worktree file
Lars Schneideredcc8582016-10-16 16:20:37 -0700418upon checkin. By default these commands process only a single
419blob and terminate. If a long running `process` filter is used
420in place of `clean` and/or `smudge` filters, then Git can process
421all blobs with a single filter command invocation for the entire
422life of a single Git command, for example `git add --all`. If a
423long running `process` filter is configured then it always takes
424precedence over a configured single blob filter. See section
425below for the description of the protocol used to communicate with
426a `process` filter.
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700427
Jehan Bing36daaac2012-02-16 17:19:03 -0800428One use of the content filtering is to massage the content into a shape
429that is more convenient for the platform, filesystem, and the user to use.
430For this mode of operation, the key phrase here is "more convenient" and
431not "turning something unusable into usable". In other words, the intent
432is that if someone unsets the filter driver definition, or does not have
433the appropriate filter program, the project should still be usable.
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700434
Jehan Bing36daaac2012-02-16 17:19:03 -0800435Another use of the content filtering is to store the content that cannot
436be directly used in the repository (e.g. a UUID that refers to the true
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100437content stored outside Git, or an encrypted content) and turn it into a
Jehan Bing36daaac2012-02-16 17:19:03 -0800438usable form upon checkout (e.g. download the external content, or decrypt
439the encrypted content).
440
441These two filters behave differently, and by default, a filter is taken as
442the former, massaging the contents into more convenient shape. A missing
443filter driver definition in the config, or a filter driver that exits with
444a non-zero status, is not an error but makes the filter a no-op passthru.
445
446You can declare that a filter turns a content that by itself is unusable
447into a usable content by setting the filter.<driver>.required configuration
448variable to `true`.
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700449
Torsten Bögershausen94729352017-11-16 17:38:28 +0100450Note: Whenever the clean filter is changed, the repo should be renormalized:
451$ git add --renormalize .
452
Nanako Shiraishid79f5d12009-12-15 12:11:10 +0900453For example, in .gitattributes, you would assign the `filter`
454attribute for paths.
455
456------------------------
457*.c filter=indent
458------------------------
459
460Then you would define a "filter.indent.clean" and "filter.indent.smudge"
461configuration in your .git/config to specify a pair of commands to
462modify the contents of C programs when the source files are checked
463in ("clean" is run) and checked out (no change is made because the
464command is "cat").
465
466------------------------
467[filter "indent"]
468 clean = indent
469 smudge = cat
470------------------------
471
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200472For best results, `clean` should not alter its output further if it is
473run twice ("clean->clean" should be equivalent to "clean"), and
474multiple `smudge` commands should not alter `clean`'s output
475("smudge->smudge->clean" should be equivalent to "clean"). See the
476section on merging below.
477
478The "indent" filter is well-behaved in this regard: it will not modify
479input that is already correctly indented. In this case, the lack of a
480smudge filter means that the clean filter _must_ accept its own output
481without modifying it.
482
Jehan Bing36daaac2012-02-16 17:19:03 -0800483If a filter _must_ succeed in order to make the stored contents usable,
484you can declare that the filter is `required`, in the configuration:
485
486------------------------
487[filter "crypt"]
488 clean = openssl enc ...
489 smudge = openssl enc -d ...
490 required
491------------------------
492
Pete Wyckoffa2b665d2010-12-22 06:40:13 -0800493Sequence "%f" on the filter command line is replaced with the name of
494the file the filter is working on. A filter might use this in keyword
495substitution. For example:
496
497------------------------
498[filter "p4"]
499 clean = git-p4-filter --clean %f
500 smudge = git-p4-filter --smudge %f
501------------------------
502
Joey Hess52db4b02016-07-11 18:45:05 -0400503Note that "%f" is the name of the path that is being worked on. Depending
504on the version that is being filtered, the corresponding file on disk may
505not exist, or may have different contents. So, smudge and clean commands
506should not try to access the file on disk, but only act as filters on the
507content provided to them on standard input.
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700508
Lars Schneideredcc8582016-10-16 16:20:37 -0700509Long Running Filter Process
510^^^^^^^^^^^^^^^^^^^^^^^^^^^
511
512If the filter command (a string value) is defined via
513`filter.<driver>.process` then Git can process all blobs with a
514single filter invocation for the entire life of a single Git
Jonathan Tanaddad102018-01-25 10:53:24 -0800515command. This is achieved by using the long-running process protocol
516(described in technical/long-running-process-protocol.txt).
Lars Schneideredcc8582016-10-16 16:20:37 -0700517
Jonathan Tanaddad102018-01-25 10:53:24 -0800518When Git encounters the first file that needs to be cleaned or smudged,
519it starts the filter and performs the handshake. In the handshake, the
520welcome message sent by Git is "git-filter-client", only version 2 is
Elijah Newren031fd4b2019-11-05 17:07:20 +0000521supported, and the supported capabilities are "clean", "smudge", and
Jonathan Tanaddad102018-01-25 10:53:24 -0800522"delay".
Lars Schneideredcc8582016-10-16 16:20:37 -0700523
524Afterwards Git sends a list of "key=value" pairs terminated with
525a flush packet. The list will contain at least the filter command
526(based on the supported capabilities) and the pathname of the file
527to filter relative to the repository root. Right after the flush packet
528Git sends the content split in zero or more pkt-line packets and a
529flush packet to terminate content. Please note, that the filter
530must not send any response before it received the content and the
Lars Schneiderc6b08312016-12-03 20:45:16 +0100531final flush packet. Also note that the "value" of a "key=value" pair
532can contain the "=" character whereas the key would never contain
533that character.
Lars Schneideredcc8582016-10-16 16:20:37 -0700534------------------------
535packet: git> command=smudge
536packet: git> pathname=path/testfile.dat
537packet: git> 0000
538packet: git> CONTENT
539packet: git> 0000
540------------------------
541
542The filter is expected to respond with a list of "key=value" pairs
543terminated with a flush packet. If the filter does not experience
544problems then the list must contain a "success" status. Right after
545these packets the filter is expected to send the content in zero
546or more pkt-line packets and a flush packet at the end. Finally, a
547second list of "key=value" pairs terminated with a flush packet
548is expected. The filter can change the status in the second list
549or keep the status as is with an empty list. Please note that the
550empty list must be terminated with a flush packet regardless.
551
552------------------------
553packet: git< status=success
554packet: git< 0000
555packet: git< SMUDGED_CONTENT
556packet: git< 0000
557packet: git< 0000 # empty list, keep "status=success" unchanged!
558------------------------
559
560If the result content is empty then the filter is expected to respond
561with a "success" status and a flush packet to signal the empty content.
562------------------------
563packet: git< status=success
564packet: git< 0000
565packet: git< 0000 # empty content!
566packet: git< 0000 # empty list, keep "status=success" unchanged!
567------------------------
568
569In case the filter cannot or does not want to process the content,
570it is expected to respond with an "error" status.
571------------------------
572packet: git< status=error
573packet: git< 0000
574------------------------
575
576If the filter experiences an error during processing, then it can
577send the status "error" after the content was (partially or
578completely) sent.
579------------------------
580packet: git< status=success
581packet: git< 0000
582packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT
583packet: git< 0000
584packet: git< status=error
585packet: git< 0000
586------------------------
587
588In case the filter cannot or does not want to process the content
589as well as any future content for the lifetime of the Git process,
590then it is expected to respond with an "abort" status at any point
591in the protocol.
592------------------------
593packet: git< status=abort
594packet: git< 0000
595------------------------
596
597Git neither stops nor restarts the filter process in case the
598"error"/"abort" status is set. However, Git sets its exit code
599according to the `filter.<driver>.required` flag, mimicking the
600behavior of the `filter.<driver>.clean` / `filter.<driver>.smudge`
601mechanism.
602
603If the filter dies during the communication or does not adhere to
604the protocol then Git will stop the filter process and restart it
605with the next file that needs to be processed. Depending on the
606`filter.<driver>.required` flag Git will interpret that as error.
607
Lars Schneider2841e8f2017-06-30 22:41:28 +0200608Delay
609^^^^^
610
611If the filter supports the "delay" capability, then Git can send the
612flag "can-delay" after the filter command and pathname. This flag
613denotes that the filter can delay filtering the current blob (e.g. to
614compensate network latencies) by responding with no content but with
615the status "delayed" and a flush packet.
616------------------------
617packet: git> command=smudge
618packet: git> pathname=path/testfile.dat
619packet: git> can-delay=1
620packet: git> 0000
621packet: git> CONTENT
622packet: git> 0000
623packet: git< status=delayed
624packet: git< 0000
625------------------------
626
627If the filter supports the "delay" capability then it must support the
628"list_available_blobs" command. If Git sends this command, then the
629filter is expected to return a list of pathnames representing blobs
630that have been delayed earlier and are now available.
631The list must be terminated with a flush packet followed
632by a "success" status that is also terminated with a flush packet. If
633no blobs for the delayed paths are available, yet, then the filter is
634expected to block the response until at least one blob becomes
635available. The filter can tell Git that it has no more delayed blobs
636by sending an empty list. As soon as the filter responds with an empty
637list, Git stops asking. All blobs that Git has not received at this
638point are considered missing and will result in an error.
639
640------------------------
641packet: git> command=list_available_blobs
642packet: git> 0000
643packet: git< pathname=path/testfile.dat
644packet: git< pathname=path/otherfile.dat
645packet: git< 0000
646packet: git< status=success
647packet: git< 0000
648------------------------
649
650After Git received the pathnames, it will request the corresponding
651blobs again. These requests contain a pathname and an empty content
652section. The filter is expected to respond with the smudged content
653in the usual way as explained above.
654------------------------
655packet: git> command=smudge
656packet: git> pathname=path/testfile.dat
657packet: git> 0000
658packet: git> 0000 # empty content!
659packet: git< status=success
660packet: git< 0000
661packet: git< SMUDGED_CONTENT
662packet: git< 0000
663packet: git< 0000 # empty list, keep "status=success" unchanged!
664------------------------
665
666Example
667^^^^^^^
668
Lars Schneider0f71fa22016-10-16 16:20:38 -0700669A long running filter demo implementation can be found in
670`contrib/long-running-filter/example.pl` located in the Git
671core repository. If you develop your own long running filter
Lars Schneideredcc8582016-10-16 16:20:37 -0700672process then the `GIT_TRACE_PACKET` environment variables can be
673very helpful for debugging (see linkgit:git[1]).
674
675Please note that you cannot use an existing `filter.<driver>.clean`
676or `filter.<driver>.smudge` command with `filter.<driver>.process`
677because the former two use a different inter process communication
678protocol than the latter one.
679
680
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700681Interaction between checkin/checkout attributes
682^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
683
684In the check-in codepath, the worktree file is first converted
685with `filter` driver (if specified and corresponding driver
686defined), then the result is processed with `ident` (if
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200687specified), and then finally with `text` (again, if specified
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700688and applicable).
689
690In the check-out codepath, the blob content is first converted
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200691with `text`, and then `ident` and fed to `filter`.
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700692
693
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200694Merging branches with differing checkin/checkout attributes
695^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
696
697If you have added attributes to a file that cause the canonical
698repository format for that file to change, such as adding a
699clean/smudge filter or text/eol/ident attributes, merging anything
700where the attribute is not in place would normally cause merge
701conflicts.
702
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100703To prevent these unnecessary merge conflicts, Git can be told to run a
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200704virtual check-out and check-in of all three stages of a file when
705resolving a three-way merge by setting the `merge.renormalize`
706configuration variable. This prevents changes caused by check-in
707conversion from causing spurious merge conflicts when a converted file
708is merged with an unconverted file.
709
710As long as a "smudge->clean" results in the same output as a "clean"
711even on files that are already smudged, this strategy will
712automatically resolve all filter-related conflicts. Filters that do
713not act in this way may cause additional merge conflicts that must be
714resolved manually.
715
716
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700717Generating diff text
718~~~~~~~~~~~~~~~~~~~~
719
Jakub Narebski4f73e242008-11-01 06:24:55 +0000720`diff`
721^^^^^^
722
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100723The attribute `diff` affects how Git generates diffs for particular
724files. It can tell Git whether to generate a textual patch for the path
Jeff King678852d2008-10-26 00:41:52 -0400725or to treat the path as a binary file. It can also affect what line is
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100726shown on the hunk header `@@ -k,l +n,m @@` line, tell Git to use an
727external command to generate the diff, or ask Git to convert binary
Jeff King678852d2008-10-26 00:41:52 -0400728files to a text format before generating the diff.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700729
730Set::
731
732 A path to which the `diff` attribute is set is treated
733 as text, even when they contain byte values that
734 normally never appear in text files, such as NUL.
735
736Unset::
737
738 A path to which the `diff` attribute is unset will
Jeff King678852d2008-10-26 00:41:52 -0400739 generate `Binary files differ` (or a binary patch, if
740 binary patches are enabled).
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700741
742Unspecified::
743
744 A path to which the `diff` attribute is unspecified
745 first gets its contents inspected, and if it looks like
Nguyễn Thái Ngọc Duy6bf3b812014-08-16 10:08:05 +0700746 text and is smaller than core.bigFileThreshold, it is treated
747 as text. Otherwise it would generate `Binary files differ`.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700748
Junio C Hamano2cc31672007-04-23 00:21:02 -0700749String::
750
Jeff King678852d2008-10-26 00:41:52 -0400751 Diff is shown using the specified diff driver. Each driver may
752 specify one or more options, as described in the following
753 section. The options for the diff driver "foo" are defined
754 by the configuration variables in the "diff.foo" section of the
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100755 Git config file.
Junio C Hamano2cc31672007-04-23 00:21:02 -0700756
757
Jeff King678852d2008-10-26 00:41:52 -0400758Defining an external diff driver
759^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Junio C Hamano2cc31672007-04-23 00:21:02 -0700760
761The definition of a diff driver is done in `gitconfig`, not
762`gitattributes` file, so strictly speaking this manual page is a
763wrong place to talk about it. However...
764
Jeff King678852d2008-10-26 00:41:52 -0400765To define an external diff driver `jcdiff`, add a section to your
Junio C Hamano2cc31672007-04-23 00:21:02 -0700766`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:
767
768----------------------------------------------------------------
769[diff "jcdiff"]
770 command = j-c-diff
771----------------------------------------------------------------
772
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100773When Git needs to show you a diff for the path with `diff`
Junio C Hamano2cc31672007-04-23 00:21:02 -0700774attribute set to `jcdiff`, it calls the command you specified
775with the above configuration, i.e. `j-c-diff`, with 7
776parameters, just like `GIT_EXTERNAL_DIFF` program is called.
Christian Couder9e1f0a82008-06-06 09:07:32 +0200777See linkgit:git[1] for details.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700778
John Caia4cf9002023-02-20 21:04:42 +0000779Setting the internal diff algorithm
780^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
781
782The diff algorithm can be set through the `diff.algorithm` config key, but
783sometimes it may be helpful to set the diff algorithm per path. For example,
784one may want to use the `minimal` diff algorithm for .json files, and the
785`histogram` for .c files, and so on without having to pass in the algorithm
786through the command line each time.
787
788First, in `.gitattributes`, assign the `diff` attribute for paths.
789
790------------------------
791*.json diff=<name>
792------------------------
793
794Then, define a "diff.<name>.algorithm" configuration to specify the diff
795algorithm, choosing from `myers`, `patience`, `minimal`, or `histogram`.
796
797----------------------------------------------------------------
798[diff "<name>"]
799 algorithm = histogram
800----------------------------------------------------------------
801
802This diff algorithm applies to user facing diff output like git-diff(1),
803git-show(1) and is used for the `--stat` output as well. The merge machinery
804will not use the diff algorithm set through this method.
805
806NOTE: If `diff.<name>.command` is defined for path with the
807`diff=<name>` attribute, it is executed as an external diff driver
808(see above), and adding `diff.<name>.algorithm` has no effect, as the
809algorithm is not passed to the external diff driver.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -0700810
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700811Defining a custom hunk-header
812^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
813
Garry Dolleyc882c012008-09-16 23:20:39 -0700814Each group of changes (called a "hunk") in the textual diff output
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700815is prefixed with a line of the form:
816
817 @@ -k,l +n,m @@ TEXT
818
Garry Dolleyc882c012008-09-16 23:20:39 -0700819This is called a 'hunk header'. The "TEXT" portion is by default a line
820that begins with an alphabet, an underscore or a dollar sign; this
821matches what GNU 'diff -p' output uses. This default selection however
822is not suited for some contents, and you can use a customized pattern
823to make a selection.
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700824
Garry Dolleyc882c012008-09-16 23:20:39 -0700825First, in .gitattributes, you would assign the `diff` attribute
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700826for paths.
827
828------------------------
829*.tex diff=tex
830------------------------
831
Shawn O. Pearceedb7e822008-09-29 10:23:19 -0700832Then, you would define a "diff.tex.xfuncname" configuration to
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700833specify a regular expression that matches a line that you would
Jeff Kingc4c86d22009-04-16 03:17:12 -0400834want to appear as the hunk header "TEXT". Add a section to your
835`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700836
837------------------------
838[diff "tex"]
Brandon Casey45d94142008-09-18 17:44:33 -0500839 xfuncname = "^(\\\\(sub)*section\\{.*)$"
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700840------------------------
841
842Note. A single level of backslashes are eaten by the
843configuration file parser, so you would need to double the
844backslashes; the pattern above picks a line that begins with a
Brian Hetro02783072007-08-23 20:44:13 -0400845backslash, and zero or more occurrences of `sub` followed by
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700846`section` followed by open brace, to the end of line.
847
848There are a few built-in patterns to make this easier, and `tex`
849is one of them, so you do not have to write the above in your
850configuration file (you still need to enable this with the
Gustaf Hendebyd08ed6d2008-08-12 16:24:25 +0200851attribute mechanism, via `.gitattributes`). The following built in
852patterns are available:
853
Adrian Johnsone90d0652012-09-16 13:24:15 +0930854- `ada` suitable for source code in the Ada language.
855
Victor Engmark2ff6c342020-10-22 12:45:08 +1300856- `bash` suitable for source code in the Bourne-Again SHell language.
857 Covers a superset of POSIX shell function definitions.
858
Gustaf Hendeby23b5beb2008-08-12 16:24:26 +0200859- `bibtex` suitable for files with BibTeX coded references.
860
Thomas Rast80c49c32009-01-17 17:29:48 +0100861- `cpp` suitable for source code in the C and C++ languages.
862
Petr Onderkab2212072010-08-16 17:01:02 +0000863- `csharp` suitable for source code in the C# language.
864
William Duclot0719f3e2016-06-03 14:32:26 +0200865- `css` suitable for cascading style sheets.
866
Stephen Boyd3c817602019-08-19 14:22:43 -0700867- `dts` suitable for devicetree (DTS) files.
868
Łukasz Niemiera8072002019-11-08 22:38:24 +0100869- `elixir` suitable for source code in the Elixir language.
870
Brandon Casey909a5492010-09-10 11:18:14 -0500871- `fortran` suitable for source code in the Fortran language.
872
Zoë Blade69f9c872015-07-21 14:22:46 +0100873- `fountain` suitable for Fountain documents.
874
Alban Gruin1dbf0c02018-03-01 12:19:07 +0100875- `golang` suitable for source code in the Go language.
876
Andreas Ericssonaf9ce1f2008-09-07 22:15:29 +0200877- `html` suitable for HTML/XHTML documents.
878
Ralf Wildenhuesb66e00f2008-09-12 21:10:26 +0200879- `java` suitable for source code in the Java language.
Gustaf Hendebyd08ed6d2008-08-12 16:24:25 +0200880
Jaydeep P Das09188ed2022-03-12 10:18:32 +0530881- `kotlin` suitable for source code in the Kotlin language.
882
Ash Holland09dad922020-05-02 14:15:43 +0100883- `markdown` suitable for Markdown documents.
884
Boxuan Li2731a782019-05-30 00:15:39 +0800885- `matlab` suitable for source code in the MATLAB and Octave languages.
Gustaf Hendeby53b10a12011-11-15 21:15:03 +0100886
Jonathan del Strother5d1e9582008-10-01 00:46:34 +0100887- `objc` suitable for source code in the Objective-C language.
888
Gustaf Hendebyd08ed6d2008-08-12 16:24:25 +0200889- `pascal` suitable for source code in the Pascal/Delphi language.
890
Jonathan Nieder71a5d4b2010-12-26 03:07:31 -0600891- `perl` suitable for source code in the Perl language.
892
Andreas Ericssonaf9ce1f2008-09-07 22:15:29 +0200893- `php` suitable for source code in the PHP language.
894
Kirill Smelkov7c172052008-08-20 19:57:07 +0400895- `python` suitable for source code in the Python language.
896
Gustaf Hendebyd08ed6d2008-08-12 16:24:25 +0200897- `ruby` suitable for source code in the Ruby language.
898
Marc-André Lureaud74e7862019-05-17 01:58:15 +0200899- `rust` suitable for source code in the Rust language.
900
Atharva Raykara4373902021-04-08 14:44:43 +0530901- `scheme` suitable for source code in the Scheme language.
902
Gustaf Hendebyd08ed6d2008-08-12 16:24:25 +0200903- `tex` suitable for source code for LaTeX documents.
Junio C Hamanoae7aa492007-07-08 17:15:09 -0700904
905
Thomas Rast80c49c32009-01-17 17:29:48 +0100906Customizing word diff
907^^^^^^^^^^^^^^^^^^^^^
908
Thomas Rast882749a2010-04-14 17:59:06 +0200909You can customize the rules that `git diff --word-diff` uses to
Thomas Rast80c49c32009-01-17 17:29:48 +0100910split words in a line, by specifying an appropriate regular expression
Boyd Stephen Smith Jrae3b9702009-01-20 22:59:54 -0600911in the "diff.*.wordRegex" configuration variable. For example, in TeX
Thomas Rast80c49c32009-01-17 17:29:48 +0100912a backslash followed by a sequence of letters forms a command, but
913several such commands can be run together without intervening
Jeff Kingc4c86d22009-04-16 03:17:12 -0400914whitespace. To separate them, use a regular expression in your
915`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:
Thomas Rast80c49c32009-01-17 17:29:48 +0100916
917------------------------
918[diff "tex"]
Boyd Stephen Smith Jrae3b9702009-01-20 22:59:54 -0600919 wordRegex = "\\\\[a-zA-Z]+|[{}]|\\\\.|[^\\{}[:space:]]+"
Thomas Rast80c49c32009-01-17 17:29:48 +0100920------------------------
921
922A built-in pattern is provided for all languages listed in the
923previous section.
924
925
Jeff King678852d2008-10-26 00:41:52 -0400926Performing text diffs of binary files
927^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
928
929Sometimes it is desirable to see the diff of a text-converted
930version of some binary files. For example, a word processor
931document can be converted to an ASCII text representation, and
932the diff of the text shown. Even though this conversion loses
933some information, the resulting diff is useful for human
934viewing (but cannot be applied directly).
935
936The `textconv` config option is used to define a program for
937performing such a conversion. The program should take a single
938argument, the name of a file to convert, and produce the
939resulting text on stdout.
940
941For example, to show the diff of the exif information of a
942file instead of the binary information (assuming you have the
Jeff Kingc4c86d22009-04-16 03:17:12 -0400943exif tool installed), add the following section to your
944`$GIT_DIR/config` file (or `$HOME/.gitconfig` file):
Jeff King678852d2008-10-26 00:41:52 -0400945
946------------------------
947[diff "jpg"]
948 textconv = exif
949------------------------
950
951NOTE: The text conversion is generally a one-way conversion;
952in this example, we lose the actual image contents and focus
953just on the text data. This means that diffs generated by
954textconv are _not_ suitable for applying. For this reason,
955only `git diff` and the `git log` family of commands (i.e.,
956log, whatchanged, show) will perform text conversion. `git
957format-patch` will never generate this output. If you want to
958send somebody a text-converted diff of a binary file (e.g.,
959because it quickly conveys the changes you have made), you
960should generate it separately and send it as a comment _in
961addition to_ the usual binary diff that you might send.
962
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400963Because text conversion can be slow, especially when doing a
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100964large number of them with `git log -p`, Git provides a mechanism
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400965to cache the output and use it in future diffs. To enable
966caching, set the "cachetextconv" variable in your diff driver's
967config. For example:
968
969------------------------
970[diff "jpg"]
971 textconv = exif
972 cachetextconv = true
973------------------------
974
975This will cache the result of running "exif" on each blob
976indefinitely. If you change the textconv config variable for a
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100977diff driver, Git will automatically invalidate the cache entries
Jeff Kingd9bae1a2010-04-01 20:12:15 -0400978and re-run the textconv filter. If you want to invalidate the
979cache manually (e.g., because your version of "exif" was updated
980and now produces better output), you can remove the cache
981manually with `git update-ref -d refs/notes/textconv/jpg` (where
982"jpg" is the name of the diff driver, as in the example above).
Jeff King678852d2008-10-26 00:41:52 -0400983
Jeff King55601c62011-05-24 15:45:27 -0400984Choosing textconv versus external diff
985^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
986
987If you want to show differences between binary or specially-formatted
988blobs in your repository, you can choose to use either an external diff
989command, or to use textconv to convert them to a diff-able text format.
990Which method you choose depends on your exact situation.
991
992The advantage of using an external diff command is flexibility. You are
993not bound to find line-oriented changes, nor is it necessary for the
994output to resemble unified diff. You are free to locate and report
995changes in the most appropriate way for your data format.
996
997A textconv, by comparison, is much more limiting. You provide a
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100998transformation of the data into a line-oriented text format, and Git
Jeff King55601c62011-05-24 15:45:27 -0400999uses its regular diff tools to generate the output. There are several
1000advantages to choosing this method:
1001
10021. Ease of use. It is often much simpler to write a binary to text
1003 transformation than it is to perform your own diff. In many cases,
1004 existing programs can be used as textconv filters (e.g., exif,
1005 odt2txt).
1006
10072. Git diff features. By performing only the transformation step
Thomas Ackermann2de9b712013-01-21 20:17:53 +01001008 yourself, you can still utilize many of Git's diff features,
Jeff King55601c62011-05-24 15:45:27 -04001009 including colorization, word-diff, and combined diffs for merges.
1010
10113. Caching. Textconv caching can speed up repeated diffs, such as those
1012 you might trigger by running `git log -p`.
1013
1014
Jeff Kingab435612011-01-09 15:10:04 -05001015Marking files as binary
1016^^^^^^^^^^^^^^^^^^^^^^^
1017
1018Git usually guesses correctly whether a blob contains text or binary
1019data by examining the beginning of the contents. However, sometimes you
1020may want to override its decision, either because a blob contains binary
1021data later in the file, or because the content, while technically
1022composed of text characters, is opaque to a human reader. For example,
Thomas Ackermannf745acb2014-11-03 21:37:07 +01001023many postscript files contain only ASCII characters, but produce noisy
Jeff Kingab435612011-01-09 15:10:04 -05001024and meaningless diffs.
1025
1026The simplest way to mark a file as binary is to unset the diff
1027attribute in the `.gitattributes` file:
1028
1029------------------------
1030*.ps -diff
1031------------------------
1032
Thomas Ackermann2de9b712013-01-21 20:17:53 +01001033This will cause Git to generate `Binary files differ` (or a binary
Jeff Kingab435612011-01-09 15:10:04 -05001034patch, if binary patches are enabled) instead of a regular diff.
1035
1036However, one may also want to specify other diff driver attributes. For
1037example, you might want to use `textconv` to convert postscript files to
Thomas Ackermannf745acb2014-11-03 21:37:07 +01001038an ASCII representation for human viewing, but otherwise treat them as
Jeff Kingab435612011-01-09 15:10:04 -05001039binary files. You cannot specify both `-diff` and `diff=ps` attributes.
1040The solution is to use the `diff.*.binary` config option:
1041
1042------------------------
1043[diff "ps"]
1044 textconv = ps2ascii
1045 binary = true
1046------------------------
1047
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001048Performing a three-way merge
1049~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1050
Jakub Narebski4f73e242008-11-01 06:24:55 +00001051`merge`
1052^^^^^^^
1053
Alexei Sholikb547ce02011-03-18 15:14:27 +02001054The attribute `merge` affects how three versions of a file are
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001055merged when a file-level merge is necessary during `git merge`,
Ori Avtalion57f6ec02009-08-07 17:24:21 +03001056and other commands such as `git revert` and `git cherry-pick`.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001057
1058Set::
1059
1060 Built-in 3-way merge driver is used to merge the
Jonathan Nieder2fd02c92008-07-03 00:55:07 -05001061 contents in a way similar to 'merge' command of `RCS`
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001062 suite. This is suitable for ordinary text files.
1063
1064Unset::
1065
1066 Take the version from the current branch as the
1067 tentative merge result, and declare that the merge has
Alexei Sholikb547ce02011-03-18 15:14:27 +02001068 conflicts. This is suitable for binary files that do
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001069 not have a well-defined merge semantics.
1070
1071Unspecified::
1072
1073 By default, this uses the same built-in 3-way merge
Alexei Sholikb547ce02011-03-18 15:14:27 +02001074 driver as is the case when the `merge` attribute is set.
1075 However, the `merge.default` configuration variable can name
1076 different merge driver to be used with paths for which the
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001077 `merge` attribute is unspecified.
1078
Junio C Hamano2cc31672007-04-23 00:21:02 -07001079String::
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001080
1081 3-way merge is performed using the specified custom
1082 merge driver. The built-in 3-way merge driver can be
1083 explicitly specified by asking for "text" driver; the
1084 built-in "take the current branch" driver can be
Junio C Hamanob9d14ff2007-04-24 13:46:02 -07001085 requested with "binary".
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001086
1087
Junio C Hamano0e545f72007-12-22 23:14:59 -08001088Built-in merge drivers
1089^^^^^^^^^^^^^^^^^^^^^^
1090
1091There are a few built-in low-level merge drivers defined that
1092can be asked for via the `merge` attribute.
1093
1094text::
1095
1096 Usual 3-way file level merge for text files. Conflicted
1097 regions are marked with conflict markers `<<<<<<<`,
1098 `=======` and `>>>>>>>`. The version from your branch
1099 appears before the `=======` marker, and the version
1100 from the merged branch appears after the `=======`
1101 marker.
1102
1103binary::
1104
1105 Keep the version from your branch in the work tree, but
1106 leave the path in the conflicted state for the user to
1107 sort out.
1108
1109union::
1110
1111 Run 3-way file level merge for text files, but take
1112 lines from both versions, instead of leaving conflict
1113 markers. This tends to leave the added lines in the
1114 resulting file in random order and the user should
1115 verify the result. Do not use this if you do not
1116 understand the implications.
1117
1118
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001119Defining a custom merge driver
1120^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1121
Junio C Hamano0e545f72007-12-22 23:14:59 -08001122The definition of a merge driver is done in the `.git/config`
1123file, not in the `gitattributes` file, so strictly speaking this
1124manual page is a wrong place to talk about it. However...
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001125
1126To define a custom merge driver `filfre`, add a section to your
1127`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:
1128
1129----------------------------------------------------------------
1130[merge "filfre"]
1131 name = feel-free merge driver
Junio C Hamanoef45bb12015-06-04 15:10:29 -07001132 driver = filfre %O %A %B %L %P
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001133 recursive = binary
1134----------------------------------------------------------------
1135
1136The `merge.*.name` variable gives the driver a human-readable
1137name.
1138
1139The `merge.*.driver` variable's value is used to construct a
Antonin Delpeuch81effe92024-01-24 20:09:11 +00001140command to run to common ancestor's version (`%O`), current
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001141version (`%A`) and the other branches' version (`%B`). These
1142three tokens are replaced with the names of temporary files that
1143hold the contents of these versions when the command line is
Antonin Delpeuch81effe92024-01-24 20:09:11 +00001144built. Additionally, `%L` will be replaced with the conflict marker
Bert Wesarg16758622010-02-23 21:11:12 +01001145size (see below).
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001146
1147The merge driver is expected to leave the result of the merge in
1148the file named with `%A` by overwriting it, and exit with zero
1149status if it managed to merge them cleanly, or non-zero if there
Junio C Hamano2b7b7882023-06-22 17:33:01 -07001150were conflicts. When the driver crashes (e.g. killed by SEGV),
1151it is expected to exit with non-zero status that are higher than
1152128, and in such a case, the merge results in a failure (which is
1153different from producing a conflict).
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001154
1155The `merge.*.recursive` variable specifies what other merge
1156driver to use when the merge driver is called for an internal
1157merge between common ancestors, when there are more than one.
1158When left unspecified, the driver itself is used for both
1159internal merge and the final merge.
1160
Junio C Hamanoef45bb12015-06-04 15:10:29 -07001161The merge driver can learn the pathname in which the merged result
Antonin Delpeuch81effe92024-01-24 20:09:11 +00001162will be stored via placeholder `%P`. The conflict labels to be used
1163for the common ancestor, local head and other head can be passed by
1164using '%S', '%X' and '%Y` respectively.
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001165
Junio C Hamano4c734802010-01-20 23:49:27 -08001166`conflict-marker-size`
1167^^^^^^^^^^^^^^^^^^^^^^
1168
1169This attribute controls the length of conflict markers left in
Štěpán Němec97509a32023-10-05 11:00:51 +02001170the work tree file during a conflicted merge. Only a positive
1171integer has a meaningful effect.
Junio C Hamano4c734802010-01-20 23:49:27 -08001172
1173For example, this line in `.gitattributes` can be used to tell the merge
1174machinery to leave much longer (instead of the usual 7-character-long)
1175conflict markers when merging the file `Documentation/git-merge.txt`
1176results in a conflict.
1177
1178------------------------
1179Documentation/git-merge.txt conflict-marker-size=32
1180------------------------
1181
1182
Junio C Hamanocf1b7862007-12-06 00:14:14 -08001183Checking whitespace errors
1184~~~~~~~~~~~~~~~~~~~~~~~~~~
1185
1186`whitespace`
1187^^^^^^^^^^^^
1188
1189The `core.whitespace` configuration variable allows you to define what
Jonathan Nieder2fd02c92008-07-03 00:55:07 -05001190'diff' and 'apply' should consider whitespace errors for all paths in
Dan McGee5162e692007-12-29 00:20:38 -06001191the project (See linkgit:git-config[1]). This attribute gives you finer
Junio C Hamanocf1b7862007-12-06 00:14:14 -08001192control per path.
1193
1194Set::
1195
Thomas Ackermann2de9b712013-01-21 20:17:53 +01001196 Notice all types of potential whitespace errors known to Git.
Johannes Sixtf4b05a42010-11-30 09:29:11 +01001197 The tab width is taken from the value of the `core.whitespace`
1198 configuration variable.
Junio C Hamanocf1b7862007-12-06 00:14:14 -08001199
1200Unset::
1201
1202 Do not notice anything as error.
1203
1204Unspecified::
1205
Johannes Sixtf4b05a42010-11-30 09:29:11 +01001206 Use the value of the `core.whitespace` configuration variable to
Junio C Hamanocf1b7862007-12-06 00:14:14 -08001207 decide what to notice as error.
1208
1209String::
1210
Andrei Rybakf9552642022-12-27 22:01:56 +01001211 Specify a comma separated list of common whitespace problems to
Johannes Sixtf4b05a42010-11-30 09:29:11 +01001212 notice in the same format as the `core.whitespace` configuration
Junio C Hamanocf1b7862007-12-06 00:14:14 -08001213 variable.
1214
1215
Junio C Hamano8a33dd82008-07-05 18:14:27 -07001216Creating an archive
1217~~~~~~~~~~~~~~~~~~~
1218
Junio C Hamano08b51f52008-07-05 18:33:16 -07001219`export-ignore`
1220^^^^^^^^^^^^^^^
1221
1222Files and directories with the attribute `export-ignore` won't be added to
1223archive files.
1224
Junio C Hamano8a33dd82008-07-05 18:14:27 -07001225`export-subst`
1226^^^^^^^^^^^^^^
1227
Thomas Ackermann2de9b712013-01-21 20:17:53 +01001228If the attribute `export-subst` is set for a file then Git will expand
Junio C Hamano8a33dd82008-07-05 18:14:27 -07001229several placeholders when adding this file to an archive. The
Junio C Hamano08b51f52008-07-05 18:33:16 -07001230expansion depends on the availability of a commit ID, i.e., if
Junio C Hamano8a33dd82008-07-05 18:14:27 -07001231linkgit:git-archive[1] has been given a tree instead of a commit or a
1232tag then no replacement will be done. The placeholders are the same
1233as those for the option `--pretty=format:` of linkgit:git-log[1],
1234except that they need to be wrapped like this: `$Format:PLACEHOLDERS$`
1235in the file. E.g. the string `$Format:%H$` will be replaced by the
René Scharfe96099722021-02-28 12:22:47 +01001236commit hash. However, only one `%(describe)` placeholder is expanded
1237per archive to avoid denial-of-service attacks.
Junio C Hamano8a33dd82008-07-05 18:14:27 -07001238
1239
Nasser Grainawi975457f2009-10-21 14:06:21 -07001240Packing objects
1241~~~~~~~~~~~~~~~
1242
1243`delta`
1244^^^^^^^
1245
1246Delta compression will not be attempted for blobs for paths with the
1247attribute `delta` set to false.
1248
1249
Alexander Gavrilova2df1fb2008-11-13 20:28:49 +03001250Viewing files in GUI tools
1251~~~~~~~~~~~~~~~~~~~~~~~~~~
1252
1253`encoding`
1254^^^^^^^^^^
1255
1256The value of this attribute specifies the character encoding that should
1257be used by GUI tools (e.g. linkgit:gitk[1] and linkgit:git-gui[1]) to
1258display the contents of the relevant file. Note that due to performance
1259considerations linkgit:gitk[1] does not use this attribute unless you
1260manually enable per-file encodings in its options.
1261
1262If this attribute is not set or has an invalid value, the value of the
1263`gui.encoding` configuration variable is used instead
1264(See linkgit:git-config[1]).
1265
1266
Michael Haggerty09225702011-08-03 15:41:30 +02001267USING MACRO ATTRIBUTES
Junio C Hamanobbb896d2008-08-30 14:35:15 -07001268----------------------
1269
1270You do not want any end-of-line conversions applied to, nor textual diffs
1271produced for, any binary file you track. You would need to specify e.g.
1272
1273------------
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +02001274*.jpg -text -diff
Junio C Hamanobbb896d2008-08-30 14:35:15 -07001275------------
1276
1277but that may become cumbersome, when you have many attributes. Using
Michael Haggerty09225702011-08-03 15:41:30 +02001278macro attributes, you can define an attribute that, when set, also
Michael Haggerty98e84062011-08-03 15:41:29 +02001279sets or unsets a number of other attributes at the same time. The
Michael Haggerty09225702011-08-03 15:41:30 +02001280system knows a built-in macro attribute, `binary`:
Junio C Hamanobbb896d2008-08-30 14:35:15 -07001281
1282------------
1283*.jpg binary
1284------------
1285
Michael Haggerty98e84062011-08-03 15:41:29 +02001286Setting the "binary" attribute also unsets the "text" and "diff"
Michael Haggerty09225702011-08-03 15:41:30 +02001287attributes as above. Note that macro attributes can only be "Set",
Michael Haggerty98e84062011-08-03 15:41:29 +02001288though setting one might have the effect of setting or unsetting other
1289attributes or even returning other attributes to the "Unspecified"
1290state.
Junio C Hamanobbb896d2008-08-30 14:35:15 -07001291
1292
Michael Haggerty09225702011-08-03 15:41:30 +02001293DEFINING MACRO ATTRIBUTES
Junio C Hamanobbb896d2008-08-30 14:35:15 -07001294-------------------------
1295
Michael Haggertye78e6962014-01-14 03:58:49 +01001296Custom macro attributes can be defined only in top-level gitattributes
1297files (`$GIT_DIR/info/attributes`, the `.gitattributes` file at the
1298top level of the working tree, or the global or system-wide
1299gitattributes files), not in `.gitattributes` files in working tree
1300subdirectories. The built-in macro attribute "binary" is equivalent
1301to:
Junio C Hamanobbb896d2008-08-30 14:35:15 -07001302
1303------------
Junio C Hamano155a4b72012-09-08 21:28:55 -07001304[attr]binary -diff -merge -text
Junio C Hamanobbb896d2008-08-30 14:35:15 -07001305------------
1306
Jeff King8ff06de2021-05-03 16:43:28 -04001307NOTES
1308-----
1309
1310Git does not follow symbolic links when accessing a `.gitattributes`
1311file in the working tree. This keeps behavior consistent when the file
1312is accessed from the index or a tree versus from the filesystem.
Junio C Hamanobbb896d2008-08-30 14:35:15 -07001313
Nguyễn Thái Ngọc Duy76a87882018-04-30 17:35:33 +02001314EXAMPLES
1315--------
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001316
1317If you have these three `gitattributes` file:
1318
1319----------------------------------------------------------------
1320(in $GIT_DIR/info/attributes)
1321
1322a* foo !bar -baz
1323
1324(in .gitattributes)
1325abc foo bar baz
1326
1327(in t/.gitattributes)
1328ab* merge=filfre
1329abc -foo -bar
1330*.c frotz
1331----------------------------------------------------------------
1332
1333the attributes given to path `t/abc` are computed as follows:
1334
13351. By examining `t/.gitattributes` (which is in the same
Thomas Ackermann2de9b712013-01-21 20:17:53 +01001336 directory as the path in question), Git finds that the first
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001337 line matches. `merge` attribute is set. It also finds that
1338 the second line matches, and attributes `foo` and `bar`
1339 are unset.
1340
13412. Then it examines `.gitattributes` (which is in the parent
1342 directory), and finds that the first line matches, but
1343 `t/.gitattributes` file already decided how `merge`, `foo`
1344 and `bar` attributes should be given to this path, so it
1345 leaves `foo` and `bar` unset. Attribute `baz` is set.
1346
David Soria Parra5c759f92007-07-29 22:50:24 -030013473. Finally it examines `$GIT_DIR/info/attributes`. This file
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001348 is used to override the in-tree settings. The first line is
1349 a match, and `foo` is set, `bar` is reverted to unspecified
1350 state, and `baz` is unset.
1351
Brian Hetro02783072007-08-23 20:44:13 -04001352As the result, the attributes assignment to `t/abc` becomes:
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001353
1354----------------------------------------------------------------
1355foo set to true
1356bar unspecified
1357baz set to false
1358merge set to string value "filfre"
1359frotz unspecified
1360----------------------------------------------------------------
1361
1362
Michael Haggertycde15182011-08-04 06:36:11 +02001363SEE ALSO
1364--------
1365linkgit:git-check-attr[1].
René Scharfe8460b2f2007-09-03 20:07:01 +02001366
Junio C Hamano88e7fdf2007-04-19 20:48:03 -07001367GIT
1368---
Christian Couder9e1f0a82008-06-06 09:07:32 +02001369Part of the linkgit:git[1] suite