blob: 6deb89a2967708dfc2c560ac72c29907a5b6334a [file] [log] [blame]
Junio C Hamanoe90436a2007-04-22 07:22:131gitattributes(5)
2================
3
4NAME
5----
Junio C Hamanoc9cb5172018-06-01 07:13:446gitattributes - Defining attributes per path
Junio C Hamanoe90436a2007-04-22 07:22:137
8SYNOPSIS
9--------
Junio C Hamano17bd2272008-09-01 06:11:1710$GIT_DIR/info/attributes, .gitattributes
Junio C Hamanoe90436a2007-04-22 07:22:1311
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
Junio C Hamano6b7d2152019-04-16 12:51:1521pattern attr1 attr2 ...
Junio C Hamanoe90436a2007-04-22 07:22:1322
Junio C Hamanod533bdb2009-02-25 09:56:5823That is, a pattern followed by an attributes list,
Junio C Hamanocbe0eb22017-02-27 23:37:4924separated 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 Hamanoe90436a2007-04-22 07:22:1329
30Each attribute can be in one of these states for a given path:
31
32Set::
33
34The path has the attribute with special value "true";
35this is specified by listing only the name of the
36attribute in the attribute list.
37
38Unset::
39
40The path has the attribute with special value "false";
41this is specified by listing the name of the attribute
42prefixed with a dash `-` in the attribute list.
43
44Set to a value::
45
46The path has the attribute with specified string value;
47this is specified by listing the name of the attribute
48followed by an equal sign `=` and its value in the
49attribute list.
50
51Unspecified::
52
Junio C Hamanod533bdb2009-02-25 09:56:5853No pattern matches the path, and nothing says if
Junio C Hamano22572e72007-04-25 05:59:2554the path has or does not have the attribute, the
55attribute for the path is said to be Unspecified.
Junio C Hamanoe90436a2007-04-22 07:22:1356
Junio C Hamanod533bdb2009-02-25 09:56:5857When more than one pattern matches the path, a later line
Junio C Hamano22572e72007-04-25 05:59:2558overrides an earlier line. This overriding is done per
Junio C Hamano81000162018-03-21 19:54:5059attribute.
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 Hamanoe90436a2007-04-22 07:22:1369
Junio C Hamano076ffcc2013-02-06 05:13:2170When deciding what attributes are assigned to a path, Git
Junio C Hamanoe90436a2007-04-22 07:22:1371consults `$GIT_DIR/info/attributes` file (which has the highest
72precedence), `.gitattributes` file in the same directory as the
Junio C Hamanob4a9ede2009-04-08 07:36:3273path in question, and its parent directories up to the toplevel of the
74work tree (the further the directory that contains `.gitattributes`
Junio C Hamano3b4609d2010-09-30 00:04:3475is from the path in question, the lower its precedence). Finally
76global and system-wide files are considered (they have the lowest
77precedence).
Junio C Hamanoe90436a2007-04-22 07:22:1378
Junio C Hamano6631a092012-10-10 22:50:5779When 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
Junio C Hamano99dc24a2008-03-27 23:49:5084If you wish to affect only a single repository (i.e., to assign
Junio C Hamano3b4609d2010-09-30 00:04:3485attributes to files that are particular to
86one user's workflow for that repository), then
Junio C Hamano99dc24a2008-03-27 23:49:5087attributes 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
Junio C Hamano3b4609d2010-09-30 00:04:3490`.gitattributes` files. Attributes that should affect all repositories
91for a single user should be placed in a file specified by the
Junio C Hamano322c6242015-03-23 21:32:4692`core.attributesFile` configuration option (see linkgit:git-config[1]).
Junio C Hamano8edb4c72012-07-09 20:33:5593Its 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.
Junio C Hamano3b4609d2010-09-30 00:04:3495Attributes for all users on a system should be placed in the
96`$(prefix)/etc/gitattributes` file.
Junio C Hamano99dc24a2008-03-27 23:49:5097
Junio C Hamanocbe0eb22017-02-27 23:37:4998Sometimes you would need to override a setting of an attribute
Junio C Hamano86d84362011-08-04 00:21:5199for a path to `Unspecified` state. This can be done by listing
Junio C Hamanoe90436a2007-04-22 07:22:13100the name of the attribute prefixed with an exclamation point `!`.
101
102
103EFFECTS
104-------
105
Junio C Hamano076ffcc2013-02-06 05:13:21106Certain operations by Git can be influenced by assigning
Junio C Hamano2a8f6dc2007-07-09 08:48:38107particular attributes to a path. Currently, the following
108operations are attributes-aware.
Junio C Hamanoe90436a2007-04-22 07:22:13109
110Checking-out and checking-in
111~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112
Junio C Hamano469d60e2007-04-29 18:30:34113These attributes affect how the contents stored in the
Junio C Hamanoe90436a2007-04-22 07:22:13114repository are copied to the working tree files when commands
Junio C Hamanoc9f11c22019-07-10 02:54:04115such as 'git switch', 'git checkout' and 'git merge' run.
116They also affect how
Junio C Hamano076ffcc2013-02-06 05:13:21117Git stores the contents you prepare in the working tree in the
Junio C Hamano1aa40d22010-01-21 17:46:43118repository upon 'git add' and 'git commit'.
Junio C Hamanoe90436a2007-04-22 07:22:13119
Junio C Hamanobb88cf42010-06-21 15:23:55120`text`
Junio C Hamano469d60e2007-04-29 18:30:34121^^^^^^
122
Junio C Hamano7ab2d202023-05-10 18:59:24123This attribute marks the path as a text file, which enables end-of-line
124conversion: When a matching file is added to the index, the file's line
125endings are normalized to LF in the index. Conversely, when the file is
126copied from the index to the working directory, its line endings may be
127converted from LF to CRLF depending on the `eol` attribute, the Git
128config, and the platform (see explanation of `eol` below).
Junio C Hamano469d60e2007-04-29 18:30:34129
Junio C Hamanoe90436a2007-04-22 07:22:13130Set::
131
Junio C Hamanobb88cf42010-06-21 15:23:55132Setting the `text` attribute on a path enables end-of-line
Junio C Hamano7ab2d202023-05-10 18:59:24133conversion on checkin and checkout as described above. Line endings
134are normalized to LF in the index every time the file is checked in,
135even if the file was previously added to Git with CRLF line endings.
Junio C Hamanoe90436a2007-04-22 07:22:13136
137Unset::
138
Junio C Hamano076ffcc2013-02-06 05:13:21139Unsetting the `text` attribute on a path tells Git not to
Junio C Hamanofcb740a2008-08-31 06:32:05140attempt any end-of-line conversion upon checkin or checkout.
Junio C Hamanoe90436a2007-04-22 07:22:13141
Junio C Hamanobb88cf42010-06-21 15:23:55142Set to string value "auto"::
143
Junio C Hamano7ab2d202023-05-10 18:59:24144When `text` is set to "auto", Git decides by itself whether the file
145is text or binary. If it is text and the file was not already in
146Git with CRLF endings, line endings are converted on checkin and
147checkout as described above. Otherwise, no conversion is done on
148checkin or checkout.
Junio C Hamanobb88cf42010-06-21 15:23:55149
Junio C Hamanoe90436a2007-04-22 07:22:13150Unspecified::
151
Junio C Hamano076ffcc2013-02-06 05:13:21152If the `text` attribute is unspecified, Git uses the
Junio C Hamanobb88cf42010-06-21 15:23:55153`core.autocrlf` configuration variable to determine if the
154file should be converted.
Junio C Hamanoe90436a2007-04-22 07:22:13155
Junio C Hamano076ffcc2013-02-06 05:13:21156Any other value causes Git to act as if `text` has been left
Junio C Hamanobb88cf42010-06-21 15:23:55157unspecified.
Junio C Hamanoe90436a2007-04-22 07:22:13158
Junio C Hamanobb88cf42010-06-21 15:23:55159`eol`
160^^^^^
Junio C Hamanoe90436a2007-04-22 07:22:13161
Junio C Hamano7ab2d202023-05-10 18:59:24162This attribute marks a path to use a specific line-ending style in the
163working tree when it is checked out. It has effect only if `text` or
164`text=auto` is set (see above), but specifying `eol` automatically sets
165`text` if `text` was left unspecified.
Junio C Hamanoe90436a2007-04-22 07:22:13166
Junio C Hamanobb88cf42010-06-21 15:23:55167Set to string value "crlf"::
Junio C Hamanoe90436a2007-04-22 07:22:13168
Junio C Hamano7ab2d202023-05-10 18:59:24169This setting converts the file's line endings in the working
170directory to CRLF when the file is checked out.
Junio C Hamanoe90436a2007-04-22 07:22:13171
Junio C Hamanobb88cf42010-06-21 15:23:55172Set to string value "lf"::
Junio C Hamanoe90436a2007-04-22 07:22:13173
Junio C Hamano7ab2d202023-05-10 18:59:24174This setting uses the same line endings in the working directory as
175in the index when the file is checked out.
176
177Unspecified::
178
179If the `eol` attribute is unspecified for a file, its line endings
180in the working directory are determined by the `core.autocrlf` or
181`core.eol` configuration variable (see the definitions of those
182options in linkgit:git-config[1]). If `text` is set but neither of
183those variables is, the default is `eol=crlf` on Windows and
184`eol=lf` on all other platforms.
Junio C Hamanoe90436a2007-04-22 07:22:13185
Junio C Hamanobb88cf42010-06-21 15:23:55186Backwards compatibility with `crlf` attribute
187^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
188
189For backwards compatibility, the `crlf` attribute is interpreted as
190follows:
191
192------------------------
193crlf text
194-crlf -text
195crlf=input eol=lf
196------------------------
197
198End-of-line conversion
199^^^^^^^^^^^^^^^^^^^^^^
200
Junio C Hamano076ffcc2013-02-06 05:13:21201While Git normally leaves file contents alone, it can be configured to
Junio C Hamanobb88cf42010-06-21 15:23:55202normalize line endings to LF in the repository and, optionally, to
203convert them to CRLF when files are checked out.
204
Junio C Hamanoe7808d72016-08-26 21:48:01205If you simply want to have CRLF line endings in your working directory
206regardless of the repository you are working with, you can set the
207config variable "core.autocrlf" without using any attributes.
208
209------------------------
210[core]
211autocrlf = true
212------------------------
213
214This does not force normalization of text files, but does ensure
215that text files that you introduce to the repository have their line
216endings normalized to LF when they are added, and that files that are
217already normalized in the repository stay normalized.
218
219If you want to ensure that text files that any contributor introduces to
220the repository have their line endings normalized, you can set the
221`text` attribute to "auto" for _all_ files.
222
223------------------------
224* text=auto
225------------------------
226
227The attributes allow a fine-grained control, how the line endings
228are converted.
Junio C Hamano076ffcc2013-02-06 05:13:21229Here is an example that will make Git normalize .txt, .vcproj and .sh
Junio C Hamanobb88cf42010-06-21 15:23:55230files, ensure that .vcproj files have CRLF and .sh files have LF in
231the working directory, and prevent .jpg files from being normalized
232regardless of their content.
233
234------------------------
Junio C Hamanoeed15af2016-07-25 22:49:42235* text=auto
Junio C Hamanobb88cf42010-06-21 15:23:55236*.txt text
Junio C Hamanoeed15af2016-07-25 22:49:42237*.vcproj text eol=crlf
238*.sh text eol=lf
Junio C Hamanobb88cf42010-06-21 15:23:55239*.jpg -text
240------------------------
241
Junio C Hamanoe7808d72016-08-26 21:48:01242NOTE: When `text=auto` conversion is enabled in a cross-platform
243project using push and pull to a central repository the text files
244containing CRLFs should be normalized.
Junio C Hamanobb88cf42010-06-21 15:23:55245
Junio C Hamanoe7808d72016-08-26 21:48:01246From a clean working directory:
Junio C Hamanobb88cf42010-06-21 15:23:55247
248-------------------------------------------------
Junio C Hamanoe7808d72016-08-26 21:48:01249$ echo "* text=auto" >.gitattributes
Junio C Hamano4886c772017-11-27 03:33:28250$ git add --renormalize .
Junio C Hamanobb88cf42010-06-21 15:23:55251$ git status # Show files that will be normalized
Junio C Hamanobb88cf42010-06-21 15:23:55252$ git commit -m "Introduce end-of-line normalization"
253-------------------------------------------------
254
255If any files that should not be normalized show up in 'git status',
256unset their `text` attribute before running 'git add -u'.
257
258------------------------
259manual.pdf -text
260------------------------
261
Junio C Hamano076ffcc2013-02-06 05:13:21262Conversely, text files that Git does not detect can have normalization
Junio C Hamanobb88cf42010-06-21 15:23:55263enabled manually.
264
265------------------------
266weirdchars.txt text
267------------------------
Junio C Hamanoe90436a2007-04-22 07:22:13268
Junio C Hamano076ffcc2013-02-06 05:13:21269If `core.safecrlf` is set to "true" or "warn", Git verifies if
Junio C Hamano26e590a2008-02-17 03:53:51270the conversion is reversible for the current setting of
Junio C Hamano076ffcc2013-02-06 05:13:21271`core.autocrlf`. For "true", Git rejects irreversible
272conversions; for "warn", Git only prints a warning but accepts
Junio C Hamano26e590a2008-02-17 03:53:51273an irreversible conversion. The safety triggers to prevent such
274a conversion done to the files in the work tree, but there are a
275few exceptions. Even though...
276
Junio C Hamano1aa40d22010-01-21 17:46:43277- 'git add' itself does not touch the files in the work tree, the
Junio C Hamano26e590a2008-02-17 03:53:51278 next checkout would, so the safety triggers;
279
Junio C Hamano1aa40d22010-01-21 17:46:43280- 'git apply' to update a text file with a patch does touch the files
Junio C Hamano26e590a2008-02-17 03:53:51281 in the work tree, but the operation is about text files and CRLF
282 conversion is about fixing the line ending inconsistencies, so the
283 safety does not trigger;
284
Junio C Hamano1aa40d22010-01-21 17:46:43285- 'git diff' itself does not touch the files in the work tree, it is
286 often run to inspect the changes you intend to next 'git add'. To
Junio C Hamano26e590a2008-02-17 03:53:51287 catch potential problems early, safety triggers.
288
Junio C Hamanoe90436a2007-04-22 07:22:13289
Junio C Hamanoccb82522018-05-08 07:52:09290`working-tree-encoding`
291^^^^^^^^^^^^^^^^^^^^^^^
292
293Git recognizes files encoded in ASCII or one of its supersets (e.g.
294UTF-8, ISO-8859-1, ...) as text files. Files encoded in certain other
295encodings (e.g. UTF-16) are interpreted as binary and consequently
296built-in Git text processing tools (e.g. 'git diff') as well as most Git
297web front ends do not visualize the contents of these files by default.
298
299In these cases you can tell Git the encoding of a file in the working
300directory with the `working-tree-encoding` attribute. If a file with this
Junio C Hamano8ef91f32019-12-01 22:58:27301attribute is added to Git, then Git re-encodes the content from the
Junio C Hamanoccb82522018-05-08 07:52:09302specified encoding to UTF-8. Finally, Git stores the UTF-8 encoded
303content in its internal data structure (called "the index"). On checkout
Junio C Hamano8ef91f32019-12-01 22:58:27304the content is re-encoded back to the specified encoding.
Junio C Hamanoccb82522018-05-08 07:52:09305
306Please note that using the `working-tree-encoding` attribute may have a
307number of pitfalls:
308
309- Alternative Git implementations (e.g. JGit or libgit2) and older Git
310 versions (as of March 2018) do not support the `working-tree-encoding`
311 attribute. If you decide to use the `working-tree-encoding` attribute
312 in your repository, then it is strongly recommended to ensure that all
313 clients working with the repository support it.
Junio C Hamano94d00a52018-10-30 07:34:40314+
315For example, Microsoft Visual Studio resources files (`*.rc`) or
316PowerShell script files (`*.ps1`) are sometimes encoded in UTF-16.
317If you declare `*.ps1` as files as UTF-16 and you add `foo.ps1` with
318a `working-tree-encoding` enabled Git client, then `foo.ps1` will be
319stored as UTF-8 internally. A client without `working-tree-encoding`
320support will checkout `foo.ps1` as UTF-8 encoded file. This will
321typically cause trouble for the users of this file.
322+
Junio C Hamano6b7d2152019-04-16 12:51:15323If a Git client that does not support the `working-tree-encoding`
324attribute adds a new file `bar.ps1`, then `bar.ps1` will be
Junio C Hamano94d00a52018-10-30 07:34:40325stored "as-is" internally (in this example probably as UTF-16).
326A client with `working-tree-encoding` support will interpret the
327internal contents as UTF-8 and try to convert it to UTF-16 on checkout.
328That operation will fail and cause an error.
Junio C Hamanoccb82522018-05-08 07:52:09329
330- Reencoding content to non-UTF encodings can cause errors as the
331 conversion might not be UTF-8 round trip safe. If you suspect your
332 encoding to not be round trip safe, then add it to
333 `core.checkRoundtripEncoding` to make Git check the round trip
334 encoding (see linkgit:git-config[1]). SHIFT-JIS (Japanese character
335 set) is known to have round trip issues with UTF-8 and is checked by
336 default.
337
338- Reencoding content requires resources that might slow down certain
339 Git operations (e.g 'git checkout' or 'git add').
340
341Use the `working-tree-encoding` attribute only if you cannot store a file
342in UTF-8 encoding and if you want Git to be able to process the content
343as text.
344
345As an example, use the following attributes if your '*.ps1' files are
346UTF-16 encoded with byte order mark (BOM) and you want Git to perform
347automatic line ending conversion based on your platform.
348
349------------------------
350*.ps1 text working-tree-encoding=UTF-16
351------------------------
352
353Use the following attributes if your '*.ps1' files are UTF-16 little
354endian encoded without BOM and you want Git to use Windows line endings
Junio C Hamanodecc7312019-03-11 09:02:54355in the working directory (use `UTF-16LE-BOM` instead of `UTF-16LE` if
Junio C Hamano23168ef2019-02-07 07:20:47356you want UTF-16 little endian with BOM).
357Please note, it is highly recommended to
Junio C Hamanoccb82522018-05-08 07:52:09358explicitly define the line endings with `eol` if the `working-tree-encoding`
359attribute is used to avoid ambiguity.
360
361------------------------
362*.ps1 text working-tree-encoding=UTF-16LE eol=CRLF
363------------------------
364
365You can get a list of all available encodings on your platform with the
366following command:
367
368------------------------
369iconv --list
370------------------------
371
372If you do not know the encoding of a file, then you can use the `file`
373command to guess the encoding:
374
375------------------------
376file foo.ps1
377------------------------
378
379
Junio C Hamano469d60e2007-04-29 18:30:34380`ident`
381^^^^^^^
382
Junio C Hamano076ffcc2013-02-06 05:13:21383When the attribute `ident` is set for a path, Git replaces
Junio C Hamano6f1bbb72008-10-31 11:32:55384`$Id$` in the blob object with `$Id:`, followed by the
Junio C Hamano469d60e2007-04-29 18:30:3438540-character hexadecimal blob object name, followed by a dollar
386sign `$` upon checkout. Any byte sequence that begins with
Junio C Hamano0430e3a2007-05-15 03:13:17387`$Id:` and ends with `$` in the worktree file is replaced
388with `$Id$` upon check-in.
Junio C Hamano469d60e2007-04-29 18:30:34389
390
Junio C Hamano469d60e2007-04-29 18:30:34391`filter`
392^^^^^^^^
393
Junio C Hamano361c1332007-11-14 12:17:22394A `filter` attribute can be set to a string value that names a
Junio C Hamano469d60e2007-04-29 18:30:34395filter driver specified in the configuration.
396
Junio C Hamano361c1332007-11-14 12:17:22397A filter driver consists of a `clean` command and a `smudge`
Junio C Hamano469d60e2007-04-29 18:30:34398command, either of which can be left unspecified. Upon
Junio C Hamano361c1332007-11-14 12:17:22399checkout, when the `smudge` command is specified, the command is
400fed the blob object from its standard input, and its standard
401output is used to update the worktree file. Similarly, the
402`clean` command is used to convert the contents of worktree file
Junio C Hamanof2f28b12016-10-31 21:41:58403upon checkin. By default these commands process only a single
404blob and terminate. If a long running `process` filter is used
405in place of `clean` and/or `smudge` filters, then Git can process
406all blobs with a single filter command invocation for the entire
407life of a single Git command, for example `git add --all`. If a
408long running `process` filter is configured then it always takes
409precedence over a configured single blob filter. See section
410below for the description of the protocol used to communicate with
411a `process` filter.
Junio C Hamano469d60e2007-04-29 18:30:34412
Junio C Hamano90a5b7b2012-02-28 23:40:31413One use of the content filtering is to massage the content into a shape
414that is more convenient for the platform, filesystem, and the user to use.
415For this mode of operation, the key phrase here is "more convenient" and
416not "turning something unusable into usable". In other words, the intent
417is that if someone unsets the filter driver definition, or does not have
418the appropriate filter program, the project should still be usable.
Junio C Hamano469d60e2007-04-29 18:30:34419
Junio C Hamano90a5b7b2012-02-28 23:40:31420Another use of the content filtering is to store the content that cannot
421be directly used in the repository (e.g. a UUID that refers to the true
Junio C Hamano076ffcc2013-02-06 05:13:21422content stored outside Git, or an encrypted content) and turn it into a
Junio C Hamano90a5b7b2012-02-28 23:40:31423usable form upon checkout (e.g. download the external content, or decrypt
424the encrypted content).
425
426These two filters behave differently, and by default, a filter is taken as
427the former, massaging the contents into more convenient shape. A missing
428filter driver definition in the config, or a filter driver that exits with
429a non-zero status, is not an error but makes the filter a no-op passthru.
430
431You can declare that a filter turns a content that by itself is unusable
432into a usable content by setting the filter.<driver>.required configuration
433variable to `true`.
Junio C Hamano469d60e2007-04-29 18:30:34434
Junio C Hamano4886c772017-11-27 03:33:28435Note: Whenever the clean filter is changed, the repo should be renormalized:
436$ git add --renormalize .
437
Junio C Hamano6a548ef2009-12-17 00:16:20438For example, in .gitattributes, you would assign the `filter`
439attribute for paths.
440
441------------------------
442*.c filter=indent
443------------------------
444
445Then you would define a "filter.indent.clean" and "filter.indent.smudge"
446configuration in your .git/config to specify a pair of commands to
447modify the contents of C programs when the source files are checked
448in ("clean" is run) and checked out (no change is made because the
449command is "cat").
450
451------------------------
452[filter "indent"]
453clean = indent
454smudge = cat
455------------------------
456
Junio C Hamano2db3e752010-09-03 21:33:06457For best results, `clean` should not alter its output further if it is
458run twice ("clean->clean" should be equivalent to "clean"), and
459multiple `smudge` commands should not alter `clean`'s output
460("smudge->smudge->clean" should be equivalent to "clean"). See the
461section on merging below.
462
463The "indent" filter is well-behaved in this regard: it will not modify
464input that is already correctly indented. In this case, the lack of a
465smudge filter means that the clean filter _must_ accept its own output
466without modifying it.
467
Junio C Hamano90a5b7b2012-02-28 23:40:31468If a filter _must_ succeed in order to make the stored contents usable,
469you can declare that the filter is `required`, in the configuration:
470
471------------------------
472[filter "crypt"]
473clean = openssl enc ...
474smudge = openssl enc -d ...
475required
476------------------------
477
Junio C Hamanoe0552e42011-01-06 00:34:47478Sequence "%f" on the filter command line is replaced with the name of
479the file the filter is working on. A filter might use this in keyword
480substitution. For example:
481
482------------------------
483[filter "p4"]
484clean = git-p4-filter --clean %f
485smudge = git-p4-filter --smudge %f
486------------------------
487
Junio C Hamanoe548d1b2016-08-08 22:26:49488Note that "%f" is the name of the path that is being worked on. Depending
489on the version that is being filtered, the corresponding file on disk may
490not exist, or may have different contents. So, smudge and clean commands
491should not try to access the file on disk, but only act as filters on the
492content provided to them on standard input.
Junio C Hamano469d60e2007-04-29 18:30:34493
Junio C Hamanof2f28b12016-10-31 21:41:58494Long Running Filter Process
495^^^^^^^^^^^^^^^^^^^^^^^^^^^
496
497If the filter command (a string value) is defined via
498`filter.<driver>.process` then Git can process all blobs with a
499single filter invocation for the entire life of a single Git
Junio C Hamano640779d2018-02-14 01:29:14500command. This is achieved by using the long-running process protocol
501(described in technical/long-running-process-protocol.txt).
Junio C Hamanof2f28b12016-10-31 21:41:58502
Junio C Hamano640779d2018-02-14 01:29:14503When Git encounters the first file that needs to be cleaned or smudged,
504it starts the filter and performs the handshake. In the handshake, the
505welcome message sent by Git is "git-filter-client", only version 2 is
Junio C Hamano8ef91f32019-12-01 22:58:27506supported, and the supported capabilities are "clean", "smudge", and
Junio C Hamano640779d2018-02-14 01:29:14507"delay".
Junio C Hamanof2f28b12016-10-31 21:41:58508
509Afterwards Git sends a list of "key=value" pairs terminated with
510a flush packet. The list will contain at least the filter command
511(based on the supported capabilities) and the pathname of the file
512to filter relative to the repository root. Right after the flush packet
513Git sends the content split in zero or more pkt-line packets and a
514flush packet to terminate content. Please note, that the filter
515must not send any response before it received the content and the
Junio C Hamano9e35abf2016-12-27 22:37:27516final flush packet. Also note that the "value" of a "key=value" pair
517can contain the "=" character whereas the key would never contain
518that character.
Junio C Hamanof2f28b12016-10-31 21:41:58519------------------------
520packet: git> command=smudge
521packet: git> pathname=path/testfile.dat
522packet: git> 0000
523packet: git> CONTENT
524packet: git> 0000
525------------------------
526
527The filter is expected to respond with a list of "key=value" pairs
528terminated with a flush packet. If the filter does not experience
529problems then the list must contain a "success" status. Right after
530these packets the filter is expected to send the content in zero
531or more pkt-line packets and a flush packet at the end. Finally, a
532second list of "key=value" pairs terminated with a flush packet
533is expected. The filter can change the status in the second list
534or keep the status as is with an empty list. Please note that the
535empty list must be terminated with a flush packet regardless.
536
537------------------------
538packet: git< status=success
539packet: git< 0000
540packet: git< SMUDGED_CONTENT
541packet: git< 0000
542packet: git< 0000 # empty list, keep "status=success" unchanged!
543------------------------
544
545If the result content is empty then the filter is expected to respond
546with a "success" status and a flush packet to signal the empty content.
547------------------------
548packet: git< status=success
549packet: git< 0000
550packet: git< 0000 # empty content!
551packet: git< 0000 # empty list, keep "status=success" unchanged!
552------------------------
553
554In case the filter cannot or does not want to process the content,
555it is expected to respond with an "error" status.
556------------------------
557packet: git< status=error
558packet: git< 0000
559------------------------
560
561If the filter experiences an error during processing, then it can
562send the status "error" after the content was (partially or
563completely) sent.
564------------------------
565packet: git< status=success
566packet: git< 0000
567packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT
568packet: git< 0000
569packet: git< status=error
570packet: git< 0000
571------------------------
572
573In case the filter cannot or does not want to process the content
574as well as any future content for the lifetime of the Git process,
575then it is expected to respond with an "abort" status at any point
576in the protocol.
577------------------------
578packet: git< status=abort
579packet: git< 0000
580------------------------
581
582Git neither stops nor restarts the filter process in case the
583"error"/"abort" status is set. However, Git sets its exit code
584according to the `filter.<driver>.required` flag, mimicking the
585behavior of the `filter.<driver>.clean` / `filter.<driver>.smudge`
586mechanism.
587
588If the filter dies during the communication or does not adhere to
589the protocol then Git will stop the filter process and restart it
590with the next file that needs to be processed. Depending on the
591`filter.<driver>.required` flag Git will interpret that as error.
592
Junio C Hamano4b2a6c12017-08-22 18:32:37593Delay
594^^^^^
595
596If the filter supports the "delay" capability, then Git can send the
597flag "can-delay" after the filter command and pathname. This flag
598denotes that the filter can delay filtering the current blob (e.g. to
599compensate network latencies) by responding with no content but with
600the status "delayed" and a flush packet.
601------------------------
602packet: git> command=smudge
603packet: git> pathname=path/testfile.dat
604packet: git> can-delay=1
605packet: git> 0000
606packet: git> CONTENT
607packet: git> 0000
608packet: git< status=delayed
609packet: git< 0000
610------------------------
611
612If the filter supports the "delay" capability then it must support the
613"list_available_blobs" command. If Git sends this command, then the
614filter is expected to return a list of pathnames representing blobs
615that have been delayed earlier and are now available.
616The list must be terminated with a flush packet followed
617by a "success" status that is also terminated with a flush packet. If
618no blobs for the delayed paths are available, yet, then the filter is
619expected to block the response until at least one blob becomes
620available. The filter can tell Git that it has no more delayed blobs
621by sending an empty list. As soon as the filter responds with an empty
622list, Git stops asking. All blobs that Git has not received at this
623point are considered missing and will result in an error.
624
625------------------------
626packet: git> command=list_available_blobs
627packet: git> 0000
628packet: git< pathname=path/testfile.dat
629packet: git< pathname=path/otherfile.dat
630packet: git< 0000
631packet: git< status=success
632packet: git< 0000
633------------------------
634
635After Git received the pathnames, it will request the corresponding
636blobs again. These requests contain a pathname and an empty content
637section. The filter is expected to respond with the smudged content
638in the usual way as explained above.
639------------------------
640packet: git> command=smudge
641packet: git> pathname=path/testfile.dat
642packet: git> 0000
643packet: git> 0000 # empty content!
644packet: git< status=success
645packet: git< 0000
646packet: git< SMUDGED_CONTENT
647packet: git< 0000
648packet: git< 0000 # empty list, keep "status=success" unchanged!
649------------------------
650
651Example
652^^^^^^^
653
Junio C Hamanof2f28b12016-10-31 21:41:58654A long running filter demo implementation can be found in
655`contrib/long-running-filter/example.pl` located in the Git
656core repository. If you develop your own long running filter
657process then the `GIT_TRACE_PACKET` environment variables can be
658very helpful for debugging (see linkgit:git[1]).
659
660Please note that you cannot use an existing `filter.<driver>.clean`
661or `filter.<driver>.smudge` command with `filter.<driver>.process`
662because the former two use a different inter process communication
663protocol than the latter one.
664
665
Junio C Hamano469d60e2007-04-29 18:30:34666Interaction between checkin/checkout attributes
667^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
668
669In the check-in codepath, the worktree file is first converted
670with `filter` driver (if specified and corresponding driver
671defined), then the result is processed with `ident` (if
Junio C Hamanobb88cf42010-06-21 15:23:55672specified), and then finally with `text` (again, if specified
Junio C Hamano469d60e2007-04-29 18:30:34673and applicable).
674
675In the check-out codepath, the blob content is first converted
Junio C Hamanobb88cf42010-06-21 15:23:55676with `text`, and then `ident` and fed to `filter`.
Junio C Hamano469d60e2007-04-29 18:30:34677
678
Junio C Hamano2db3e752010-09-03 21:33:06679Merging branches with differing checkin/checkout attributes
680^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
681
682If you have added attributes to a file that cause the canonical
683repository format for that file to change, such as adding a
684clean/smudge filter or text/eol/ident attributes, merging anything
685where the attribute is not in place would normally cause merge
686conflicts.
687
Junio C Hamano076ffcc2013-02-06 05:13:21688To prevent these unnecessary merge conflicts, Git can be told to run a
Junio C Hamano2db3e752010-09-03 21:33:06689virtual check-out and check-in of all three stages of a file when
690resolving a three-way merge by setting the `merge.renormalize`
691configuration variable. This prevents changes caused by check-in
692conversion from causing spurious merge conflicts when a converted file
693is merged with an unconverted file.
694
695As long as a "smudge->clean" results in the same output as a "clean"
696even on files that are already smudged, this strategy will
697automatically resolve all filter-related conflicts. Filters that do
698not act in this way may cause additional merge conflicts that must be
699resolved manually.
700
701
Junio C Hamanoe90436a2007-04-22 07:22:13702Generating diff text
703~~~~~~~~~~~~~~~~~~~~
704
Junio C Hamanof7e47ee2008-11-02 18:16:45705`diff`
706^^^^^^
707
Junio C Hamano076ffcc2013-02-06 05:13:21708The attribute `diff` affects how Git generates diffs for particular
709files. It can tell Git whether to generate a textual patch for the path
Junio C Hamano28363492008-11-14 08:26:31710or to treat the path as a binary file. It can also affect what line is
Junio C Hamano076ffcc2013-02-06 05:13:21711shown on the hunk header `@@ -k,l +n,m @@` line, tell Git to use an
712external command to generate the diff, or ask Git to convert binary
Junio C Hamano28363492008-11-14 08:26:31713files to a text format before generating the diff.
Junio C Hamanoe90436a2007-04-22 07:22:13714
715Set::
716
717A path to which the `diff` attribute is set is treated
718as text, even when they contain byte values that
719normally never appear in text files, such as NUL.
720
721Unset::
722
723A path to which the `diff` attribute is unset will
Junio C Hamano28363492008-11-14 08:26:31724generate `Binary files differ` (or a binary patch, if
725binary patches are enabled).
Junio C Hamanoe90436a2007-04-22 07:22:13726
727Unspecified::
728
729A path to which the `diff` attribute is unspecified
730first gets its contents inspected, and if it looks like
Junio C Hamano8e61f1f2014-09-11 21:53:45731text and is smaller than core.bigFileThreshold, it is treated
732as text. Otherwise it would generate `Binary files differ`.
Junio C Hamanoe90436a2007-04-22 07:22:13733
Junio C Hamano9db95ca2007-04-23 07:27:05734String::
735
Junio C Hamano28363492008-11-14 08:26:31736Diff is shown using the specified diff driver. Each driver may
737specify one or more options, as described in the following
738section. The options for the diff driver "foo" are defined
739by the configuration variables in the "diff.foo" section of the
Junio C Hamano076ffcc2013-02-06 05:13:21740Git config file.
Junio C Hamano9db95ca2007-04-23 07:27:05741
742
Junio C Hamano28363492008-11-14 08:26:31743Defining an external diff driver
744^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Junio C Hamano9db95ca2007-04-23 07:27:05745
746The definition of a diff driver is done in `gitconfig`, not
747`gitattributes` file, so strictly speaking this manual page is a
748wrong place to talk about it. However...
749
Junio C Hamano28363492008-11-14 08:26:31750To define an external diff driver `jcdiff`, add a section to your
Junio C Hamano9db95ca2007-04-23 07:27:05751`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:
752
753----------------------------------------------------------------
754[diff "jcdiff"]
755command = j-c-diff
756----------------------------------------------------------------
757
Junio C Hamano076ffcc2013-02-06 05:13:21758When Git needs to show you a diff for the path with `diff`
Junio C Hamano9db95ca2007-04-23 07:27:05759attribute set to `jcdiff`, it calls the command you specified
760with the above configuration, i.e. `j-c-diff`, with 7
761parameters, just like `GIT_EXTERNAL_DIFF` program is called.
Junio C Hamanof7c042d2008-06-06 22:50:53762See linkgit:git[1] for details.
Junio C Hamanoe90436a2007-04-22 07:22:13763
Junio C Hamano98137252023-02-27 18:49:30764Setting the internal diff algorithm
765^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
766
767The diff algorithm can be set through the `diff.algorithm` config key, but
768sometimes it may be helpful to set the diff algorithm per path. For example,
769one may want to use the `minimal` diff algorithm for .json files, and the
770`histogram` for .c files, and so on without having to pass in the algorithm
771through the command line each time.
772
773First, in `.gitattributes`, assign the `diff` attribute for paths.
774
775------------------------
776*.json diff=<name>
777------------------------
778
779Then, define a "diff.<name>.algorithm" configuration to specify the diff
780algorithm, choosing from `myers`, `patience`, `minimal`, or `histogram`.
781
782----------------------------------------------------------------
783[diff "<name>"]
784 algorithm = histogram
785----------------------------------------------------------------
786
787This diff algorithm applies to user facing diff output like git-diff(1),
788git-show(1) and is used for the `--stat` output as well. The merge machinery
789will not use the diff algorithm set through this method.
790
791NOTE: If `diff.<name>.command` is defined for path with the
792`diff=<name>` attribute, it is executed as an external diff driver
793(see above), and adding `diff.<name>.algorithm` has no effect, as the
794algorithm is not passed to the external diff driver.
Junio C Hamanoe90436a2007-04-22 07:22:13795
Junio C Hamano2a8f6dc2007-07-09 08:48:38796Defining a custom hunk-header
797^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
798
Junio C Hamano5e4d8de2008-09-19 06:27:02799Each group of changes (called a "hunk") in the textual diff output
Junio C Hamano2a8f6dc2007-07-09 08:48:38800is prefixed with a line of the form:
801
802@@ -k,l +n,m @@ TEXT
803
Junio C Hamano5e4d8de2008-09-19 06:27:02804This is called a 'hunk header'. The "TEXT" portion is by default a line
805that begins with an alphabet, an underscore or a dollar sign; this
806matches what GNU 'diff -p' output uses. This default selection however
807is not suited for some contents, and you can use a customized pattern
808to make a selection.
Junio C Hamano2a8f6dc2007-07-09 08:48:38809
Junio C Hamano5e4d8de2008-09-19 06:27:02810First, in .gitattributes, you would assign the `diff` attribute
Junio C Hamano2a8f6dc2007-07-09 08:48:38811for paths.
812
813------------------------
814*.tex diff=tex
815------------------------
816
Junio C Hamanoa476efa2008-10-10 15:31:42817Then, you would define a "diff.tex.xfuncname" configuration to
Junio C Hamano2a8f6dc2007-07-09 08:48:38818specify a regular expression that matches a line that you would
Junio C Hamano3bd66e92009-04-18 06:17:56819want to appear as the hunk header "TEXT". Add a section to your
820`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:
Junio C Hamano2a8f6dc2007-07-09 08:48:38821
822------------------------
823[diff "tex"]
Junio C Hamanoa476efa2008-10-10 15:31:42824xfuncname = "^(\\\\(sub)*section\\{.*)$"
Junio C Hamano2a8f6dc2007-07-09 08:48:38825------------------------
826
827Note. A single level of backslashes are eaten by the
828configuration file parser, so you would need to double the
829backslashes; the pattern above picks a line that begins with a
Junio C Hamanoa6387422007-08-25 03:54:27830backslash, and zero or more occurrences of `sub` followed by
Junio C Hamano2a8f6dc2007-07-09 08:48:38831`section` followed by open brace, to the end of line.
832
833There are a few built-in patterns to make this easier, and `tex`
834is one of them, so you do not have to write the above in your
835configuration file (you still need to enable this with the
Junio C Hamanoa76cb0d2008-08-13 20:04:10836attribute mechanism, via `.gitattributes`). The following built in
837patterns are available:
838
Junio C Hamano4f07aec2012-09-25 19:08:44839- `ada` suitable for source code in the Ada language.
840
Junio C Hamanodf3d3cd2020-11-02 22:05:05841- `bash` suitable for source code in the Bourne-Again SHell language.
842 Covers a superset of POSIX shell function definitions.
843
Junio C Hamanoa76cb0d2008-08-13 20:04:10844- `bibtex` suitable for files with BibTeX coded references.
845
Junio C Hamano20d47e32009-01-26 06:36:02846- `cpp` suitable for source code in the C and C++ languages.
847
Junio C Hamano53ba6d02010-08-22 07:25:12848- `csharp` suitable for source code in the C# language.
849
Junio C Hamanofedb1b42016-06-20 21:23:40850- `css` suitable for cascading style sheets.
851
Junio C Hamanocb705392019-09-18 19:30:01852- `dts` suitable for devicetree (DTS) files.
853
Junio C Hamano8ef91f32019-12-01 22:58:27854- `elixir` suitable for source code in the Elixir language.
855
Junio C Hamano3b4609d2010-09-30 00:04:34856- `fortran` suitable for source code in the Fortran language.
857
Junio C Hamanod7ed4042015-08-03 19:43:00858- `fountain` suitable for Fountain documents.
859
Junio C Hamanoa25ccc42018-03-08 21:36:14860- `golang` suitable for source code in the Go language.
861
Junio C Hamanof24b8062008-09-09 01:34:39862- `html` suitable for HTML/XHTML documents.
863
Junio C Hamano675570c2008-09-13 06:07:28864- `java` suitable for source code in the Java language.
Junio C Hamanoa76cb0d2008-08-13 20:04:10865
Junio C Hamano67fef492022-03-27 17:31:23866- `kotlin` suitable for source code in the Kotlin language.
867
Junio C Hamano2b43cff2020-05-08 22:27:04868- `markdown` suitable for Markdown documents.
869
Junio C Hamano73c64862019-06-18 03:24:20870- `matlab` suitable for source code in the MATLAB and Octave languages.
Junio C Hamano92887a32011-12-07 00:26:29871
Junio C Hamanoa476efa2008-10-10 15:31:42872- `objc` suitable for source code in the Objective-C language.
873
Junio C Hamanoa76cb0d2008-08-13 20:04:10874- `pascal` suitable for source code in the Pascal/Delphi language.
875
Junio C Hamano9b6fafe2011-01-14 00:43:22876- `perl` suitable for source code in the Perl language.
877
Junio C Hamanof24b8062008-09-09 01:34:39878- `php` suitable for source code in the PHP language.
879
Junio C Hamano3f2f6e52008-08-21 10:34:22880- `python` suitable for source code in the Python language.
881
Junio C Hamanoa76cb0d2008-08-13 20:04:10882- `ruby` suitable for source code in the Ruby language.
883
Junio C Hamanod0a99402019-06-21 18:58:41884- `rust` suitable for source code in the Rust language.
885
Junio C Hamano4078a552021-04-30 06:08:10886- `scheme` suitable for source code in the Scheme language.
887
Junio C Hamanoa76cb0d2008-08-13 20:04:10888- `tex` suitable for source code for LaTeX documents.
Junio C Hamano2a8f6dc2007-07-09 08:48:38889
890
Junio C Hamano20d47e32009-01-26 06:36:02891Customizing word diff
892^^^^^^^^^^^^^^^^^^^^^
893
Junio C Hamanocc7636a2010-05-21 14:57:53894You can customize the rules that `git diff --word-diff` uses to
Junio C Hamano20d47e32009-01-26 06:36:02895split words in a line, by specifying an appropriate regular expression
896in the "diff.*.wordRegex" configuration variable. For example, in TeX
897a backslash followed by a sequence of letters forms a command, but
898several such commands can be run together without intervening
Junio C Hamano3bd66e92009-04-18 06:17:56899whitespace. To separate them, use a regular expression in your
900`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:
Junio C Hamano20d47e32009-01-26 06:36:02901
902------------------------
903[diff "tex"]
904wordRegex = "\\\\[a-zA-Z]+|[{}]|\\\\.|[^\\{}[:space:]]+"
905------------------------
906
907A built-in pattern is provided for all languages listed in the
908previous section.
909
910
Junio C Hamano28363492008-11-14 08:26:31911Performing text diffs of binary files
912^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
913
914Sometimes it is desirable to see the diff of a text-converted
915version of some binary files. For example, a word processor
916document can be converted to an ASCII text representation, and
917the diff of the text shown. Even though this conversion loses
918some information, the resulting diff is useful for human
919viewing (but cannot be applied directly).
920
921The `textconv` config option is used to define a program for
922performing such a conversion. The program should take a single
923argument, the name of a file to convert, and produce the
924resulting text on stdout.
925
926For example, to show the diff of the exif information of a
927file instead of the binary information (assuming you have the
Junio C Hamano3bd66e92009-04-18 06:17:56928exif tool installed), add the following section to your
929`$GIT_DIR/config` file (or `$HOME/.gitconfig` file):
Junio C Hamano28363492008-11-14 08:26:31930
931------------------------
932[diff "jpg"]
933textconv = exif
934------------------------
935
936NOTE: The text conversion is generally a one-way conversion;
937in this example, we lose the actual image contents and focus
938just on the text data. This means that diffs generated by
939textconv are _not_ suitable for applying. For this reason,
940only `git diff` and the `git log` family of commands (i.e.,
941log, whatchanged, show) will perform text conversion. `git
942format-patch` will never generate this output. If you want to
943send somebody a text-converted diff of a binary file (e.g.,
944because it quickly conveys the changes you have made), you
945should generate it separately and send it as a comment _in
946addition to_ the usual binary diff that you might send.
947
Junio C Hamanocb39aea2010-05-09 07:24:48948Because text conversion can be slow, especially when doing a
Junio C Hamano076ffcc2013-02-06 05:13:21949large number of them with `git log -p`, Git provides a mechanism
Junio C Hamanocb39aea2010-05-09 07:24:48950to cache the output and use it in future diffs. To enable
951caching, set the "cachetextconv" variable in your diff driver's
952config. For example:
953
954------------------------
955[diff "jpg"]
956textconv = exif
957cachetextconv = true
958------------------------
959
960This will cache the result of running "exif" on each blob
961indefinitely. If you change the textconv config variable for a
Junio C Hamano076ffcc2013-02-06 05:13:21962diff driver, Git will automatically invalidate the cache entries
Junio C Hamanocb39aea2010-05-09 07:24:48963and re-run the textconv filter. If you want to invalidate the
964cache manually (e.g., because your version of "exif" was updated
965and now produces better output), you can remove the cache
966manually with `git update-ref -d refs/notes/textconv/jpg` (where
967"jpg" is the name of the diff driver, as in the example above).
Junio C Hamano28363492008-11-14 08:26:31968
Junio C Hamanof93530e2011-05-25 23:59:59969Choosing textconv versus external diff
970^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
971
972If you want to show differences between binary or specially-formatted
973blobs in your repository, you can choose to use either an external diff
974command, or to use textconv to convert them to a diff-able text format.
975Which method you choose depends on your exact situation.
976
977The advantage of using an external diff command is flexibility. You are
978not bound to find line-oriented changes, nor is it necessary for the
979output to resemble unified diff. You are free to locate and report
980changes in the most appropriate way for your data format.
981
982A textconv, by comparison, is much more limiting. You provide a
Junio C Hamano076ffcc2013-02-06 05:13:21983transformation of the data into a line-oriented text format, and Git
Junio C Hamanof93530e2011-05-25 23:59:59984uses its regular diff tools to generate the output. There are several
985advantages to choosing this method:
986
9871. Ease of use. It is often much simpler to write a binary to text
988 transformation than it is to perform your own diff. In many cases,
989 existing programs can be used as textconv filters (e.g., exif,
990 odt2txt).
991
9922. Git diff features. By performing only the transformation step
Junio C Hamano076ffcc2013-02-06 05:13:21993 yourself, you can still utilize many of Git's diff features,
Junio C Hamanof93530e2011-05-25 23:59:59994 including colorization, word-diff, and combined diffs for merges.
995
9963. Caching. Textconv caching can speed up repeated diffs, such as those
997 you might trigger by running `git log -p`.
998
999
Junio C Hamano9b6fafe2011-01-14 00:43:221000Marking files as binary
1001^^^^^^^^^^^^^^^^^^^^^^^
1002
1003Git usually guesses correctly whether a blob contains text or binary
1004data by examining the beginning of the contents. However, sometimes you
1005may want to override its decision, either because a blob contains binary
1006data later in the file, or because the content, while technically
1007composed of text characters, is opaque to a human reader. For example,
Junio C Hamanofb5ffde2014-11-04 22:38:561008many postscript files contain only ASCII characters, but produce noisy
Junio C Hamano9b6fafe2011-01-14 00:43:221009and meaningless diffs.
1010
1011The simplest way to mark a file as binary is to unset the diff
1012attribute in the `.gitattributes` file:
1013
1014------------------------
1015*.ps -diff
1016------------------------
1017
Junio C Hamano076ffcc2013-02-06 05:13:211018This will cause Git to generate `Binary files differ` (or a binary
Junio C Hamano9b6fafe2011-01-14 00:43:221019patch, if binary patches are enabled) instead of a regular diff.
1020
1021However, one may also want to specify other diff driver attributes. For
1022example, you might want to use `textconv` to convert postscript files to
Junio C Hamanofb5ffde2014-11-04 22:38:561023an ASCII representation for human viewing, but otherwise treat them as
Junio C Hamano9b6fafe2011-01-14 00:43:221024binary files. You cannot specify both `-diff` and `diff=ps` attributes.
1025The solution is to use the `diff.*.binary` config option:
1026
1027------------------------
1028[diff "ps"]
1029 textconv = ps2ascii
1030 binary = true
1031------------------------
1032
Junio C Hamanoe90436a2007-04-22 07:22:131033Performing a three-way merge
1034~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1035
Junio C Hamanof7e47ee2008-11-02 18:16:451036`merge`
1037^^^^^^^
1038
Junio C Hamanofa28cb12011-03-19 01:35:351039The attribute `merge` affects how three versions of a file are
Junio C Hamanoe90436a2007-04-22 07:22:131040merged when a file-level merge is necessary during `git merge`,
Junio C Hamano27a128b2009-08-13 01:23:001041and other commands such as `git revert` and `git cherry-pick`.
Junio C Hamanoe90436a2007-04-22 07:22:131042
1043Set::
1044
1045Built-in 3-way merge driver is used to merge the
Junio C Hamanoba4b9282008-07-06 05:20:311046contents in a way similar to 'merge' command of `RCS`
Junio C Hamanoe90436a2007-04-22 07:22:131047suite. This is suitable for ordinary text files.
1048
1049Unset::
1050
1051Take the version from the current branch as the
1052tentative merge result, and declare that the merge has
Junio C Hamanofa28cb12011-03-19 01:35:351053conflicts. This is suitable for binary files that do
Junio C Hamanoe90436a2007-04-22 07:22:131054not have a well-defined merge semantics.
1055
1056Unspecified::
1057
1058By default, this uses the same built-in 3-way merge
Junio C Hamanofa28cb12011-03-19 01:35:351059driver as is the case when the `merge` attribute is set.
1060However, the `merge.default` configuration variable can name
1061different merge driver to be used with paths for which the
Junio C Hamanoe90436a2007-04-22 07:22:131062`merge` attribute is unspecified.
1063
Junio C Hamano9db95ca2007-04-23 07:27:051064String::
Junio C Hamanoe90436a2007-04-22 07:22:131065
10663-way merge is performed using the specified custom
1067merge driver. The built-in 3-way merge driver can be
1068explicitly specified by asking for "text" driver; the
1069built-in "take the current branch" driver can be
Junio C Hamano22572e72007-04-25 05:59:251070requested with "binary".
Junio C Hamanoe90436a2007-04-22 07:22:131071
1072
Junio C Hamanof556fc22007-12-23 08:16:381073Built-in merge drivers
1074^^^^^^^^^^^^^^^^^^^^^^
1075
1076There are a few built-in low-level merge drivers defined that
1077can be asked for via the `merge` attribute.
1078
1079text::
1080
1081Usual 3-way file level merge for text files. Conflicted
1082regions are marked with conflict markers `<<<<<<<`,
1083`=======` and `>>>>>>>`. The version from your branch
1084appears before the `=======` marker, and the version
1085from the merged branch appears after the `=======`
1086marker.
1087
1088binary::
1089
1090Keep the version from your branch in the work tree, but
1091leave the path in the conflicted state for the user to
1092sort out.
1093
1094union::
1095
1096Run 3-way file level merge for text files, but take
1097lines from both versions, instead of leaving conflict
1098markers. This tends to leave the added lines in the
1099resulting file in random order and the user should
1100verify the result. Do not use this if you do not
1101understand the implications.
1102
1103
Junio C Hamanoe90436a2007-04-22 07:22:131104Defining a custom merge driver
1105^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1106
Junio C Hamanof556fc22007-12-23 08:16:381107The definition of a merge driver is done in the `.git/config`
1108file, not in the `gitattributes` file, so strictly speaking this
1109manual page is a wrong place to talk about it. However...
Junio C Hamanoe90436a2007-04-22 07:22:131110
1111To define a custom merge driver `filfre`, add a section to your
1112`$GIT_DIR/config` file (or `$HOME/.gitconfig` file) like this:
1113
1114----------------------------------------------------------------
1115[merge "filfre"]
1116name = feel-free merge driver
Junio C Hamanodf5384b2015-06-24 21:12:121117driver = filfre %O %A %B %L %P
Junio C Hamanoe90436a2007-04-22 07:22:131118recursive = binary
1119----------------------------------------------------------------
1120
1121The `merge.*.name` variable gives the driver a human-readable
1122name.
1123
1124The `merge.*.driver` variable's value is used to construct a
1125command to run to merge ancestor's version (`%O`), current
1126version (`%A`) and the other branches' version (`%B`). These
1127three tokens are replaced with the names of temporary files that
1128hold the contents of these versions when the command line is
Junio C Hamano86660db2010-02-24 01:12:541129built. Additionally, %L will be replaced with the conflict marker
1130size (see below).
Junio C Hamanoe90436a2007-04-22 07:22:131131
1132The merge driver is expected to leave the result of the merge in
1133the file named with `%A` by overwriting it, and exit with zero
1134status if it managed to merge them cleanly, or non-zero if there
Junio C Hamano2e885d82023-06-30 19:50:461135were conflicts. When the driver crashes (e.g. killed by SEGV),
1136it is expected to exit with non-zero status that are higher than
1137128, and in such a case, the merge results in a failure (which is
1138different from producing a conflict).
Junio C Hamanoe90436a2007-04-22 07:22:131139
1140The `merge.*.recursive` variable specifies what other merge
1141driver to use when the merge driver is called for an internal
1142merge between common ancestors, when there are more than one.
1143When left unspecified, the driver itself is used for both
1144internal merge and the final merge.
1145
Junio C Hamanodf5384b2015-06-24 21:12:121146The merge driver can learn the pathname in which the merged result
1147will be stored via placeholder `%P`.
1148
Junio C Hamanoe90436a2007-04-22 07:22:131149
Junio C Hamano1aa40d22010-01-21 17:46:431150`conflict-marker-size`
1151^^^^^^^^^^^^^^^^^^^^^^
1152
1153This attribute controls the length of conflict markers left in
1154the work tree file during a conflicted merge. Only setting to
1155the value to a positive integer has any meaningful effect.
1156
1157For example, this line in `.gitattributes` can be used to tell the merge
1158machinery to leave much longer (instead of the usual 7-character-long)
1159conflict markers when merging the file `Documentation/git-merge.txt`
1160results in a conflict.
1161
1162------------------------
1163Documentation/git-merge.txt conflict-marker-size=32
1164------------------------
1165
1166
Junio C Hamano942b35e2007-12-09 10:19:331167Checking whitespace errors
1168~~~~~~~~~~~~~~~~~~~~~~~~~~
1169
1170`whitespace`
1171^^^^^^^^^^^^
1172
1173The `core.whitespace` configuration variable allows you to define what
Junio C Hamanoba4b9282008-07-06 05:20:311174'diff' and 'apply' should consider whitespace errors for all paths in
Junio C Hamano35738e82008-01-07 07:55:461175the project (See linkgit:git-config[1]). This attribute gives you finer
Junio C Hamano942b35e2007-12-09 10:19:331176control per path.
1177
1178Set::
1179
Junio C Hamano076ffcc2013-02-06 05:13:211180Notice all types of potential whitespace errors known to Git.
Junio C Hamanoeef01fe2010-12-13 08:31:581181The tab width is taken from the value of the `core.whitespace`
1182configuration variable.
Junio C Hamano942b35e2007-12-09 10:19:331183
1184Unset::
1185
1186Do not notice anything as error.
1187
1188Unspecified::
1189
Junio C Hamanoeef01fe2010-12-13 08:31:581190Use the value of the `core.whitespace` configuration variable to
Junio C Hamano942b35e2007-12-09 10:19:331191decide what to notice as error.
1192
1193String::
1194
Junio C Hamanoac204f42023-01-05 06:34:541195Specify a comma separated list of common whitespace problems to
Junio C Hamanoeef01fe2010-12-13 08:31:581196notice in the same format as the `core.whitespace` configuration
Junio C Hamano942b35e2007-12-09 10:19:331197variable.
1198
1199
Junio C Hamanoba4b9282008-07-06 05:20:311200Creating an archive
1201~~~~~~~~~~~~~~~~~~~
1202
1203`export-ignore`
1204^^^^^^^^^^^^^^^
1205
1206Files and directories with the attribute `export-ignore` won't be added to
1207archive files.
1208
1209`export-subst`
1210^^^^^^^^^^^^^^
1211
Junio C Hamano076ffcc2013-02-06 05:13:211212If the attribute `export-subst` is set for a file then Git will expand
Junio C Hamanoba4b9282008-07-06 05:20:311213several placeholders when adding this file to an archive. The
1214expansion depends on the availability of a commit ID, i.e., if
1215linkgit:git-archive[1] has been given a tree instead of a commit or a
1216tag then no replacement will be done. The placeholders are the same
1217as those for the option `--pretty=format:` of linkgit:git-log[1],
1218except that they need to be wrapped like this: `$Format:PLACEHOLDERS$`
1219in the file. E.g. the string `$Format:%H$` will be replaced by the
Junio C Hamano458281a2021-03-22 21:20:261220commit hash. However, only one `%(describe)` placeholder is expanded
1221per archive to avoid denial-of-service attacks.
Junio C Hamanoba4b9282008-07-06 05:20:311222
1223
Junio C Hamanoa10d8d22009-10-22 03:09:051224Packing objects
1225~~~~~~~~~~~~~~~
1226
1227`delta`
1228^^^^^^^
1229
1230Delta compression will not be attempted for blobs for paths with the
1231attribute `delta` set to false.
1232
1233
Junio C Hamanod3452a02008-11-15 08:07:551234Viewing files in GUI tools
1235~~~~~~~~~~~~~~~~~~~~~~~~~~
1236
1237`encoding`
1238^^^^^^^^^^
1239
1240The value of this attribute specifies the character encoding that should
1241be used by GUI tools (e.g. linkgit:gitk[1] and linkgit:git-gui[1]) to
1242display the contents of the relevant file. Note that due to performance
1243considerations linkgit:gitk[1] does not use this attribute unless you
1244manually enable per-file encodings in its options.
1245
1246If this attribute is not set or has an invalid value, the value of the
1247`gui.encoding` configuration variable is used instead
1248(See linkgit:git-config[1]).
1249
1250
Junio C Hamano86d84362011-08-04 00:21:511251USING MACRO ATTRIBUTES
Junio C Hamanofcb740a2008-08-31 06:32:051252----------------------
1253
1254You do not want any end-of-line conversions applied to, nor textual diffs
1255produced for, any binary file you track. You would need to specify e.g.
1256
1257------------
Junio C Hamanobb88cf42010-06-21 15:23:551258*.jpg -text -diff
Junio C Hamanofcb740a2008-08-31 06:32:051259------------
1260
1261but that may become cumbersome, when you have many attributes. Using
Junio C Hamano86d84362011-08-04 00:21:511262macro attributes, you can define an attribute that, when set, also
1263sets or unsets a number of other attributes at the same time. The
1264system knows a built-in macro attribute, `binary`:
Junio C Hamanofcb740a2008-08-31 06:32:051265
1266------------
1267*.jpg binary
1268------------
1269
Junio C Hamano86d84362011-08-04 00:21:511270Setting the "binary" attribute also unsets the "text" and "diff"
1271attributes as above. Note that macro attributes can only be "Set",
1272though setting one might have the effect of setting or unsetting other
1273attributes or even returning other attributes to the "Unspecified"
1274state.
Junio C Hamanofcb740a2008-08-31 06:32:051275
1276
Junio C Hamano86d84362011-08-04 00:21:511277DEFINING MACRO ATTRIBUTES
Junio C Hamanofcb740a2008-08-31 06:32:051278-------------------------
1279
Junio C Hamanoc5bd79e2014-01-27 21:31:261280Custom macro attributes can be defined only in top-level gitattributes
1281files (`$GIT_DIR/info/attributes`, the `.gitattributes` file at the
1282top level of the working tree, or the global or system-wide
1283gitattributes files), not in `.gitattributes` files in working tree
1284subdirectories. The built-in macro attribute "binary" is equivalent
1285to:
Junio C Hamanofcb740a2008-08-31 06:32:051286
1287------------
Junio C Hamanoa3cfb762012-09-15 05:32:141288[attr]binary -diff -merge -text
Junio C Hamanofcb740a2008-08-31 06:32:051289------------
1290
Junio C Hamano51a55bc2021-05-12 05:22:191291NOTES
1292-----
1293
1294Git does not follow symbolic links when accessing a `.gitattributes`
1295file in the working tree. This keeps behavior consistent when the file
1296is accessed from the index or a tree versus from the filesystem.
Junio C Hamanofcb740a2008-08-31 06:32:051297
Junio C Hamanob9d9d902018-05-23 07:07:421298EXAMPLES
1299--------
Junio C Hamanoe90436a2007-04-22 07:22:131300
1301If you have these three `gitattributes` file:
1302
1303----------------------------------------------------------------
1304(in $GIT_DIR/info/attributes)
1305
1306a* foo !bar -baz
1307
1308(in .gitattributes)
1309abc foo bar baz
1310
1311(in t/.gitattributes)
1312ab* merge=filfre
1313abc -foo -bar
1314*.c frotz
1315----------------------------------------------------------------
1316
1317the attributes given to path `t/abc` are computed as follows:
1318
13191. By examining `t/.gitattributes` (which is in the same
Junio C Hamano076ffcc2013-02-06 05:13:211320 directory as the path in question), Git finds that the first
Junio C Hamanoe90436a2007-04-22 07:22:131321 line matches. `merge` attribute is set. It also finds that
1322 the second line matches, and attributes `foo` and `bar`
1323 are unset.
1324
13252. Then it examines `.gitattributes` (which is in the parent
1326 directory), and finds that the first line matches, but
1327 `t/.gitattributes` file already decided how `merge`, `foo`
1328 and `bar` attributes should be given to this path, so it
1329 leaves `foo` and `bar` unset. Attribute `baz` is set.
1330
Junio C Hamanobdeff822007-07-30 09:07:1713313. Finally it examines `$GIT_DIR/info/attributes`. This file
Junio C Hamanoe90436a2007-04-22 07:22:131332 is used to override the in-tree settings. The first line is
1333 a match, and `foo` is set, `bar` is reverted to unspecified
1334 state, and `baz` is unset.
1335
Junio C Hamanoa6387422007-08-25 03:54:271336As the result, the attributes assignment to `t/abc` becomes:
Junio C Hamanoe90436a2007-04-22 07:22:131337
1338----------------------------------------------------------------
1339foo set to true
1340bar unspecified
1341baz set to false
1342merge set to string value "filfre"
1343frotz unspecified
1344----------------------------------------------------------------
1345
1346
Junio C Hamanof7279012011-08-18 06:13:131347SEE ALSO
1348--------
1349linkgit:git-check-attr[1].
Junio C Hamanoef4c2d12007-09-10 08:11:271350
Junio C Hamanoe90436a2007-04-22 07:22:131351GIT
1352---
Junio C Hamanof7c042d2008-06-06 22:50:531353Part of the linkgit:git[1] suite