1GITATTRIBUTES(5) Git Manual GITATTRIBUTES(5)
2
3
4
6 gitattributes - Defining attributes per path
7
9 $GIT_DIR/info/attributes, .gitattributes
10
12 A gitattributes file is a simple text file that gives attributes to
13 pathnames.
14
15 Each line in gitattributes file is of form:
16
17 pattern attr1 attr2 ...
18
19 That is, a pattern followed by an attributes list, separated by
20 whitespaces. Leading and trailing whitespaces are ignored. Lines that
21 begin with # are ignored. Patterns that begin with a double quote are
22 quoted in C style. When the pattern matches the path in question, the
23 attributes listed on the line are given to the path.
24
25 Each attribute can be in one of these states for a given path:
26
27 Set
28 The path has the attribute with special value "true"; this is
29 specified by listing only the name of the attribute in the
30 attribute list.
31
32 Unset
33 The path has the attribute with special value "false"; this is
34 specified by listing the name of the attribute prefixed with a dash
35 - in the attribute list.
36
37 Set to a value
38 The path has the attribute with specified string value; this is
39 specified by listing the name of the attribute followed by an equal
40 sign = and its value in the attribute list.
41
42 Unspecified
43 No pattern matches the path, and nothing says if the path has or
44 does not have the attribute, the attribute for the path is said to
45 be Unspecified.
46
47 When more than one pattern matches the path, a later line overrides an
48 earlier line. This overriding is done per attribute.
49
50 The rules by which the pattern matches paths are the same as in
51 .gitignore files (see gitignore(5)), with a few exceptions:
52
53 · negative patterns are forbidden
54
55 · patterns that match a directory do not recursively match paths
56 inside that directory (so using the trailing-slash path/ syntax is
57 pointless in an attributes file; use path/** instead)
58
59 When deciding what attributes are assigned to a path, Git consults
60 $GIT_DIR/info/attributes file (which has the highest precedence),
61 .gitattributes file in the same directory as the path in question, and
62 its parent directories up to the toplevel of the work tree (the further
63 the directory that contains .gitattributes is from the path in
64 question, the lower its precedence). Finally global and system-wide
65 files are considered (they have the lowest precedence).
66
67 When the .gitattributes file is missing from the work tree, the path in
68 the index is used as a fall-back. During checkout process,
69 .gitattributes in the index is used and then the file in the working
70 tree is used as a fall-back.
71
72 If you wish to affect only a single repository (i.e., to assign
73 attributes to files that are particular to one user’s workflow for that
74 repository), then attributes should be placed in the
75 $GIT_DIR/info/attributes file. Attributes which should be
76 version-controlled and distributed to other repositories (i.e.,
77 attributes of interest to all users) should go into .gitattributes
78 files. Attributes that should affect all repositories for a single user
79 should be placed in a file specified by the core.attributesFile
80 configuration option (see git-config(1)). Its default value is
81 $XDG_CONFIG_HOME/git/attributes. If $XDG_CONFIG_HOME is either not set
82 or empty, $HOME/.config/git/attributes is used instead. Attributes for
83 all users on a system should be placed in the
84 $(prefix)/etc/gitattributes file.
85
86 Sometimes you would need to override a setting of an attribute for a
87 path to Unspecified state. This can be done by listing the name of the
88 attribute prefixed with an exclamation point !.
89
91 Certain operations by Git can be influenced by assigning particular
92 attributes to a path. Currently, the following operations are
93 attributes-aware.
94
95 Checking-out and checking-in
96 These attributes affect how the contents stored in the repository are
97 copied to the working tree files when commands such as git checkout and
98 git merge run. They also affect how Git stores the contents you prepare
99 in the working tree in the repository upon git add and git commit.
100
101 text
102 This attribute enables and controls end-of-line normalization. When
103 a text file is normalized, its line endings are converted to LF in
104 the repository. To control what line ending style is used in the
105 working directory, use the eol attribute for a single file and the
106 core.eol configuration variable for all text files. Note that
107 setting core.autocrlf to true or input overrides core.eol (see the
108 definitions of those options in git-config(1)).
109
110 Set
111 Setting the text attribute on a path enables end-of-line
112 normalization and marks the path as a text file. End-of-line
113 conversion takes place without guessing the content type.
114
115 Unset
116 Unsetting the text attribute on a path tells Git not to attempt
117 any end-of-line conversion upon checkin or checkout.
118
119 Set to string value "auto"
120 When text is set to "auto", the path is marked for automatic
121 end-of-line conversion. If Git decides that the content is
122 text, its line endings are converted to LF on checkin. When the
123 file has been committed with CRLF, no conversion is done.
124
125 Unspecified
126 If the text attribute is unspecified, Git uses the
127 core.autocrlf configuration variable to determine if the file
128 should be converted.
129
130 Any other value causes Git to act as if text has been left
131 unspecified.
132
133 eol
134 This attribute sets a specific line-ending style to be used in the
135 working directory. It enables end-of-line conversion without any
136 content checks, effectively setting the text attribute. Note that
137 setting this attribute on paths which are in the index with CRLF
138 line endings may make the paths to be considered dirty. Adding the
139 path to the index again will normalize the line endings in the
140 index.
141
142 Set to string value "crlf"
143 This setting forces Git to normalize line endings for this file
144 on checkin and convert them to CRLF when the file is checked
145 out.
146
147 Set to string value "lf"
148 This setting forces Git to normalize line endings to LF on
149 checkin and prevents conversion to CRLF when the file is
150 checked out.
151
152 Backwards compatibility with crlf attribute
153 For backwards compatibility, the crlf attribute is interpreted as
154 follows:
155
156 crlf text
157 -crlf -text
158 crlf=input eol=lf
159
160
161 End-of-line conversion
162 While Git normally leaves file contents alone, it can be configured
163 to normalize line endings to LF in the repository and, optionally,
164 to convert them to CRLF when files are checked out.
165
166 If you simply want to have CRLF line endings in your working
167 directory regardless of the repository you are working with, you
168 can set the config variable "core.autocrlf" without using any
169 attributes.
170
171 [core]
172 autocrlf = true
173
174
175 This does not force normalization of text files, but does ensure
176 that text files that you introduce to the repository have their
177 line endings normalized to LF when they are added, and that files
178 that are already normalized in the repository stay normalized.
179
180 If you want to ensure that text files that any contributor
181 introduces to the repository have their line endings normalized,
182 you can set the text attribute to "auto" for all files.
183
184 * text=auto
185
186
187 The attributes allow a fine-grained control, how the line endings
188 are converted. Here is an example that will make Git normalize
189 .txt, .vcproj and .sh files, ensure that .vcproj files have CRLF
190 and .sh files have LF in the working directory, and prevent .jpg
191 files from being normalized regardless of their content.
192
193 * text=auto
194 *.txt text
195 *.vcproj text eol=crlf
196 *.sh text eol=lf
197 *.jpg -text
198
199
200 Note
201 When text=auto conversion is enabled in a cross-platform
202 project using push and pull to a central repository the text
203 files containing CRLFs should be normalized.
204
205 From a clean working directory:
206
207 $ echo "* text=auto" >.gitattributes
208 $ git add --renormalize .
209 $ git status # Show files that will be normalized
210 $ git commit -m "Introduce end-of-line normalization"
211
212
213 If any files that should not be normalized show up in git status,
214 unset their text attribute before running git add -u.
215
216 manual.pdf -text
217
218
219 Conversely, text files that Git does not detect can have
220 normalization enabled manually.
221
222 weirdchars.txt text
223
224
225 If core.safecrlf is set to "true" or "warn", Git verifies if the
226 conversion is reversible for the current setting of core.autocrlf.
227 For "true", Git rejects irreversible conversions; for "warn", Git
228 only prints a warning but accepts an irreversible conversion. The
229 safety triggers to prevent such a conversion done to the files in
230 the work tree, but there are a few exceptions. Even though...
231
232 · git add itself does not touch the files in the work tree, the
233 next checkout would, so the safety triggers;
234
235 · git apply to update a text file with a patch does touch the
236 files in the work tree, but the operation is about text files
237 and CRLF conversion is about fixing the line ending
238 inconsistencies, so the safety does not trigger;
239
240 · git diff itself does not touch the files in the work tree, it
241 is often run to inspect the changes you intend to next git add.
242 To catch potential problems early, safety triggers.
243
244 working-tree-encoding
245 Git recognizes files encoded in ASCII or one of its supersets (e.g.
246 UTF-8, ISO-8859-1, ...) as text files. Files encoded in certain
247 other encodings (e.g. UTF-16) are interpreted as binary and
248 consequently built-in Git text processing tools (e.g. git diff) as
249 well as most Git web front ends do not visualize the contents of
250 these files by default.
251
252 In these cases you can tell Git the encoding of a file in the
253 working directory with the working-tree-encoding attribute. If a
254 file with this attribute is added to Git, then Git reencodes the
255 content from the specified encoding to UTF-8. Finally, Git stores
256 the UTF-8 encoded content in its internal data structure (called
257 "the index"). On checkout the content is reencoded back to the
258 specified encoding.
259
260 Please note that using the working-tree-encoding attribute may have
261 a number of pitfalls:
262
263 · Alternative Git implementations (e.g. JGit or libgit2) and
264 older Git versions (as of March 2018) do not support the
265 working-tree-encoding attribute. If you decide to use the
266 working-tree-encoding attribute in your repository, then it is
267 strongly recommended to ensure that all clients working with
268 the repository support it.
269
270 For example, Microsoft Visual Studio resources files (*.rc) or
271 PowerShell script files (*.ps1) are sometimes encoded in
272 UTF-16. If you declare *.ps1 as files as UTF-16 and you add
273 foo.ps1 with a working-tree-encoding enabled Git client, then
274 foo.ps1 will be stored as UTF-8 internally. A client without
275 working-tree-encoding support will checkout foo.ps1 as UTF-8
276 encoded file. This will typically cause trouble for the users
277 of this file.
278
279 If a Git client, that does not support the
280 working-tree-encoding attribute, adds a new file bar.ps1, then
281 bar.ps1 will be stored "as-is" internally (in this example
282 probably as UTF-16). A client with working-tree-encoding
283 support will interpret the internal contents as UTF-8 and try
284 to convert it to UTF-16 on checkout. That operation will fail
285 and cause an error.
286
287 · Reencoding content to non-UTF encodings can cause errors as the
288 conversion might not be UTF-8 round trip safe. If you suspect
289 your encoding to not be round trip safe, then add it to
290 core.checkRoundtripEncoding to make Git check the round trip
291 encoding (see git-config(1)). SHIFT-JIS (Japanese character
292 set) is known to have round trip issues with UTF-8 and is
293 checked by default.
294
295 · Reencoding content requires resources that might slow down
296 certain Git operations (e.g git checkout or git add).
297
298 Use the working-tree-encoding attribute only if you cannot store a
299 file in UTF-8 encoding and if you want Git to be able to process
300 the content as text.
301
302 As an example, use the following attributes if your *.ps1 files are
303 UTF-16 encoded with byte order mark (BOM) and you want Git to
304 perform automatic line ending conversion based on your platform.
305
306 *.ps1 text working-tree-encoding=UTF-16
307
308
309 Use the following attributes if your *.ps1 files are UTF-16 little
310 endian encoded without BOM and you want Git to use Windows line
311 endings in the working directory (use UTF-16-LE-BOM instead of
312 UTF-16LE if you want UTF-16 little endian with BOM). Please note,
313 it is highly recommended to explicitly define the line endings with
314 eol if the working-tree-encoding attribute is used to avoid
315 ambiguity.
316
317 *.ps1 text working-tree-encoding=UTF-16LE eol=CRLF
318
319
320 You can get a list of all available encodings on your platform with
321 the following command:
322
323 iconv --list
324
325
326 If you do not know the encoding of a file, then you can use the
327 file command to guess the encoding:
328
329 file foo.ps1
330
331
332 ident
333 When the attribute ident is set for a path, Git replaces $Id$ in
334 the blob object with $Id:, followed by the 40-character hexadecimal
335 blob object name, followed by a dollar sign $ upon checkout. Any
336 byte sequence that begins with $Id: and ends with $ in the worktree
337 file is replaced with $Id$ upon check-in.
338
339 filter
340 A filter attribute can be set to a string value that names a filter
341 driver specified in the configuration.
342
343 A filter driver consists of a clean command and a smudge command,
344 either of which can be left unspecified. Upon checkout, when the
345 smudge command is specified, the command is fed the blob object
346 from its standard input, and its standard output is used to update
347 the worktree file. Similarly, the clean command is used to convert
348 the contents of worktree file upon checkin. By default these
349 commands process only a single blob and terminate. If a long
350 running process filter is used in place of clean and/or smudge
351 filters, then Git can process all blobs with a single filter
352 command invocation for the entire life of a single Git command, for
353 example git add --all. If a long running process filter is
354 configured then it always takes precedence over a configured single
355 blob filter. See section below for the description of the protocol
356 used to communicate with a process filter.
357
358 One use of the content filtering is to massage the content into a
359 shape that is more convenient for the platform, filesystem, and the
360 user to use. For this mode of operation, the key phrase here is
361 "more convenient" and not "turning something unusable into usable".
362 In other words, the intent is that if someone unsets the filter
363 driver definition, or does not have the appropriate filter program,
364 the project should still be usable.
365
366 Another use of the content filtering is to store the content that
367 cannot be directly used in the repository (e.g. a UUID that refers
368 to the true content stored outside Git, or an encrypted content)
369 and turn it into a usable form upon checkout (e.g. download the
370 external content, or decrypt the encrypted content).
371
372 These two filters behave differently, and by default, a filter is
373 taken as the former, massaging the contents into more convenient
374 shape. A missing filter driver definition in the config, or a
375 filter driver that exits with a non-zero status, is not an error
376 but makes the filter a no-op passthru.
377
378 You can declare that a filter turns a content that by itself is
379 unusable into a usable content by setting the
380 filter.<driver>.required configuration variable to true.
381
382 Note: Whenever the clean filter is changed, the repo should be
383 renormalized: $ git add --renormalize .
384
385 For example, in .gitattributes, you would assign the filter
386 attribute for paths.
387
388 *.c filter=indent
389
390
391 Then you would define a "filter.indent.clean" and
392 "filter.indent.smudge" configuration in your .git/config to specify
393 a pair of commands to modify the contents of C programs when the
394 source files are checked in ("clean" is run) and checked out (no
395 change is made because the command is "cat").
396
397 [filter "indent"]
398 clean = indent
399 smudge = cat
400
401
402 For best results, clean should not alter its output further if it
403 is run twice ("clean→clean" should be equivalent to "clean"), and
404 multiple smudge commands should not alter clean's output
405 ("smudge→smudge→clean" should be equivalent to "clean"). See the
406 section on merging below.
407
408 The "indent" filter is well-behaved in this regard: it will not
409 modify input that is already correctly indented. In this case, the
410 lack of a smudge filter means that the clean filter must accept its
411 own output without modifying it.
412
413 If a filter must succeed in order to make the stored contents
414 usable, you can declare that the filter is required, in the
415 configuration:
416
417 [filter "crypt"]
418 clean = openssl enc ...
419 smudge = openssl enc -d ...
420 required
421
422
423 Sequence "%f" on the filter command line is replaced with the name
424 of the file the filter is working on. A filter might use this in
425 keyword substitution. For example:
426
427 [filter "p4"]
428 clean = git-p4-filter --clean %f
429 smudge = git-p4-filter --smudge %f
430
431
432 Note that "%f" is the name of the path that is being worked on.
433 Depending on the version that is being filtered, the corresponding
434 file on disk may not exist, or may have different contents. So,
435 smudge and clean commands should not try to access the file on
436 disk, but only act as filters on the content provided to them on
437 standard input.
438
439 Long Running Filter Process
440 If the filter command (a string value) is defined via
441 filter.<driver>.process then Git can process all blobs with a
442 single filter invocation for the entire life of a single Git
443 command. This is achieved by using the long-running process
444 protocol (described in
445 technical/long-running-process-protocol.txt).
446
447 When Git encounters the first file that needs to be cleaned or
448 smudged, it starts the filter and performs the handshake. In the
449 handshake, the welcome message sent by Git is "git-filter-client",
450 only version 2 is suppported, and the supported capabilities are
451 "clean", "smudge", and "delay".
452
453 Afterwards Git sends a list of "key=value" pairs terminated with a
454 flush packet. The list will contain at least the filter command
455 (based on the supported capabilities) and the pathname of the file
456 to filter relative to the repository root. Right after the flush
457 packet Git sends the content split in zero or more pkt-line packets
458 and a flush packet to terminate content. Please note, that the
459 filter must not send any response before it received the content
460 and the final flush packet. Also note that the "value" of a
461 "key=value" pair can contain the "=" character whereas the key
462 would never contain that character.
463
464 packet: git> command=smudge
465 packet: git> pathname=path/testfile.dat
466 packet: git> 0000
467 packet: git> CONTENT
468 packet: git> 0000
469
470
471 The filter is expected to respond with a list of "key=value" pairs
472 terminated with a flush packet. If the filter does not experience
473 problems then the list must contain a "success" status. Right after
474 these packets the filter is expected to send the content in zero or
475 more pkt-line packets and a flush packet at the end. Finally, a
476 second list of "key=value" pairs terminated with a flush packet is
477 expected. The filter can change the status in the second list or
478 keep the status as is with an empty list. Please note that the
479 empty list must be terminated with a flush packet regardless.
480
481 packet: git< status=success
482 packet: git< 0000
483 packet: git< SMUDGED_CONTENT
484 packet: git< 0000
485 packet: git< 0000 # empty list, keep "status=success" unchanged!
486
487
488 If the result content is empty then the filter is expected to
489 respond with a "success" status and a flush packet to signal the
490 empty content.
491
492 packet: git< status=success
493 packet: git< 0000
494 packet: git< 0000 # empty content!
495 packet: git< 0000 # empty list, keep "status=success" unchanged!
496
497
498 In case the filter cannot or does not want to process the content,
499 it is expected to respond with an "error" status.
500
501 packet: git< status=error
502 packet: git< 0000
503
504
505 If the filter experiences an error during processing, then it can
506 send the status "error" after the content was (partially or
507 completely) sent.
508
509 packet: git< status=success
510 packet: git< 0000
511 packet: git< HALF_WRITTEN_ERRONEOUS_CONTENT
512 packet: git< 0000
513 packet: git< status=error
514 packet: git< 0000
515
516
517 In case the filter cannot or does not want to process the content
518 as well as any future content for the lifetime of the Git process,
519 then it is expected to respond with an "abort" status at any point
520 in the protocol.
521
522 packet: git< status=abort
523 packet: git< 0000
524
525
526 Git neither stops nor restarts the filter process in case the
527 "error"/"abort" status is set. However, Git sets its exit code
528 according to the filter.<driver>.required flag, mimicking the
529 behavior of the filter.<driver>.clean / filter.<driver>.smudge
530 mechanism.
531
532 If the filter dies during the communication or does not adhere to
533 the protocol then Git will stop the filter process and restart it
534 with the next file that needs to be processed. Depending on the
535 filter.<driver>.required flag Git will interpret that as error.
536
537 Delay
538 If the filter supports the "delay" capability, then Git can send
539 the flag "can-delay" after the filter command and pathname. This
540 flag denotes that the filter can delay filtering the current blob
541 (e.g. to compensate network latencies) by responding with no
542 content but with the status "delayed" and a flush packet.
543
544 packet: git> command=smudge
545 packet: git> pathname=path/testfile.dat
546 packet: git> can-delay=1
547 packet: git> 0000
548 packet: git> CONTENT
549 packet: git> 0000
550 packet: git< status=delayed
551 packet: git< 0000
552
553
554 If the filter supports the "delay" capability then it must support
555 the "list_available_blobs" command. If Git sends this command, then
556 the filter is expected to return a list of pathnames representing
557 blobs that have been delayed earlier and are now available. The
558 list must be terminated with a flush packet followed by a "success"
559 status that is also terminated with a flush packet. If no blobs for
560 the delayed paths are available, yet, then the filter is expected
561 to block the response until at least one blob becomes available.
562 The filter can tell Git that it has no more delayed blobs by
563 sending an empty list. As soon as the filter responds with an empty
564 list, Git stops asking. All blobs that Git has not received at this
565 point are considered missing and will result in an error.
566
567 packet: git> command=list_available_blobs
568 packet: git> 0000
569 packet: git< pathname=path/testfile.dat
570 packet: git< pathname=path/otherfile.dat
571 packet: git< 0000
572 packet: git< status=success
573 packet: git< 0000
574
575
576 After Git received the pathnames, it will request the corresponding
577 blobs again. These requests contain a pathname and an empty content
578 section. The filter is expected to respond with the smudged content
579 in the usual way as explained above.
580
581 packet: git> command=smudge
582 packet: git> pathname=path/testfile.dat
583 packet: git> 0000
584 packet: git> 0000 # empty content!
585 packet: git< status=success
586 packet: git< 0000
587 packet: git< SMUDGED_CONTENT
588 packet: git< 0000
589 packet: git< 0000 # empty list, keep "status=success" unchanged!
590
591
592 Example
593 A long running filter demo implementation can be found in
594 contrib/long-running-filter/example.pl located in the Git core
595 repository. If you develop your own long running filter process
596 then the GIT_TRACE_PACKET environment variables can be very helpful
597 for debugging (see git(1)).
598
599 Please note that you cannot use an existing filter.<driver>.clean
600 or filter.<driver>.smudge command with filter.<driver>.process
601 because the former two use a different inter process communication
602 protocol than the latter one.
603
604 Interaction between checkin/checkout attributes
605 In the check-in codepath, the worktree file is first converted with
606 filter driver (if specified and corresponding driver defined), then
607 the result is processed with ident (if specified), and then finally
608 with text (again, if specified and applicable).
609
610 In the check-out codepath, the blob content is first converted with
611 text, and then ident and fed to filter.
612
613 Merging branches with differing checkin/checkout attributes
614 If you have added attributes to a file that cause the canonical
615 repository format for that file to change, such as adding a
616 clean/smudge filter or text/eol/ident attributes, merging anything
617 where the attribute is not in place would normally cause merge
618 conflicts.
619
620 To prevent these unnecessary merge conflicts, Git can be told to
621 run a virtual check-out and check-in of all three stages of a file
622 when resolving a three-way merge by setting the merge.renormalize
623 configuration variable. This prevents changes caused by check-in
624 conversion from causing spurious merge conflicts when a converted
625 file is merged with an unconverted file.
626
627 As long as a "smudge→clean" results in the same output as a "clean"
628 even on files that are already smudged, this strategy will
629 automatically resolve all filter-related conflicts. Filters that do
630 not act in this way may cause additional merge conflicts that must
631 be resolved manually.
632
633 Generating diff text
634 diff
635 The attribute diff affects how Git generates diffs for particular
636 files. It can tell Git whether to generate a textual patch for the
637 path or to treat the path as a binary file. It can also affect what
638 line is shown on the hunk header @@ -k,l +n,m @@ line, tell Git to
639 use an external command to generate the diff, or ask Git to convert
640 binary files to a text format before generating the diff.
641
642 Set
643 A path to which the diff attribute is set is treated as text,
644 even when they contain byte values that normally never appear
645 in text files, such as NUL.
646
647 Unset
648 A path to which the diff attribute is unset will generate
649 Binary files differ (or a binary patch, if binary patches are
650 enabled).
651
652 Unspecified
653 A path to which the diff attribute is unspecified first gets
654 its contents inspected, and if it looks like text and is
655 smaller than core.bigFileThreshold, it is treated as text.
656 Otherwise it would generate Binary files differ.
657
658 String
659 Diff is shown using the specified diff driver. Each driver may
660 specify one or more options, as described in the following
661 section. The options for the diff driver "foo" are defined by
662 the configuration variables in the "diff.foo" section of the
663 Git config file.
664
665 Defining an external diff driver
666 The definition of a diff driver is done in gitconfig, not
667 gitattributes file, so strictly speaking this manual page is a
668 wrong place to talk about it. However...
669
670 To define an external diff driver jcdiff, add a section to your
671 $GIT_DIR/config file (or $HOME/.gitconfig file) like this:
672
673 [diff "jcdiff"]
674 command = j-c-diff
675
676
677 When Git needs to show you a diff for the path with diff attribute
678 set to jcdiff, it calls the command you specified with the above
679 configuration, i.e. j-c-diff, with 7 parameters, just like
680 GIT_EXTERNAL_DIFF program is called. See git(1) for details.
681
682 Defining a custom hunk-header
683 Each group of changes (called a "hunk") in the textual diff output
684 is prefixed with a line of the form:
685
686 @@ -k,l +n,m @@ TEXT
687
688 This is called a hunk header. The "TEXT" portion is by default a
689 line that begins with an alphabet, an underscore or a dollar sign;
690 this matches what GNU diff -p output uses. This default selection
691 however is not suited for some contents, and you can use a
692 customized pattern to make a selection.
693
694 First, in .gitattributes, you would assign the diff attribute for
695 paths.
696
697 *.tex diff=tex
698
699
700 Then, you would define a "diff.tex.xfuncname" configuration to
701 specify a regular expression that matches a line that you would
702 want to appear as the hunk header "TEXT". Add a section to your
703 $GIT_DIR/config file (or $HOME/.gitconfig file) like this:
704
705 [diff "tex"]
706 xfuncname = "^(\\\\(sub)*section\\{.*)$"
707
708
709 Note. A single level of backslashes are eaten by the configuration
710 file parser, so you would need to double the backslashes; the
711 pattern above picks a line that begins with a backslash, and zero
712 or more occurrences of sub followed by section followed by open
713 brace, to the end of line.
714
715 There are a few built-in patterns to make this easier, and tex is
716 one of them, so you do not have to write the above in your
717 configuration file (you still need to enable this with the
718 attribute mechanism, via .gitattributes). The following built in
719 patterns are available:
720
721 · ada suitable for source code in the Ada language.
722
723 · bibtex suitable for files with BibTeX coded references.
724
725 · cpp suitable for source code in the C and C++ languages.
726
727 · csharp suitable for source code in the C# language.
728
729 · css suitable for cascading style sheets.
730
731 · fortran suitable for source code in the Fortran language.
732
733 · fountain suitable for Fountain documents.
734
735 · golang suitable for source code in the Go language.
736
737 · html suitable for HTML/XHTML documents.
738
739 · java suitable for source code in the Java language.
740
741 · matlab suitable for source code in the MATLAB language.
742
743 · objc suitable for source code in the Objective-C language.
744
745 · pascal suitable for source code in the Pascal/Delphi language.
746
747 · perl suitable for source code in the Perl language.
748
749 · php suitable for source code in the PHP language.
750
751 · python suitable for source code in the Python language.
752
753 · ruby suitable for source code in the Ruby language.
754
755 · tex suitable for source code for LaTeX documents.
756
757 Customizing word diff
758 You can customize the rules that git diff --word-diff uses to split
759 words in a line, by specifying an appropriate regular expression in
760 the "diff.*.wordRegex" configuration variable. For example, in TeX
761 a backslash followed by a sequence of letters forms a command, but
762 several such commands can be run together without intervening
763 whitespace. To separate them, use a regular expression in your
764 $GIT_DIR/config file (or $HOME/.gitconfig file) like this:
765
766 [diff "tex"]
767 wordRegex = "\\\\[a-zA-Z]+|[{}]|\\\\.|[^\\{}[:space:]]+"
768
769
770 A built-in pattern is provided for all languages listed in the
771 previous section.
772
773 Performing text diffs of binary files
774 Sometimes it is desirable to see the diff of a text-converted
775 version of some binary files. For example, a word processor
776 document can be converted to an ASCII text representation, and the
777 diff of the text shown. Even though this conversion loses some
778 information, the resulting diff is useful for human viewing (but
779 cannot be applied directly).
780
781 The textconv config option is used to define a program for
782 performing such a conversion. The program should take a single
783 argument, the name of a file to convert, and produce the resulting
784 text on stdout.
785
786 For example, to show the diff of the exif information of a file
787 instead of the binary information (assuming you have the exif tool
788 installed), add the following section to your $GIT_DIR/config file
789 (or $HOME/.gitconfig file):
790
791 [diff "jpg"]
792 textconv = exif
793
794
795 Note
796 The text conversion is generally a one-way conversion; in this
797 example, we lose the actual image contents and focus just on
798 the text data. This means that diffs generated by textconv are
799 not suitable for applying. For this reason, only git diff and
800 the git log family of commands (i.e., log, whatchanged, show)
801 will perform text conversion. git format-patch will never
802 generate this output. If you want to send somebody a
803 text-converted diff of a binary file (e.g., because it quickly
804 conveys the changes you have made), you should generate it
805 separately and send it as a comment in addition to the usual
806 binary diff that you might send.
807
808 Because text conversion can be slow, especially when doing a large
809 number of them with git log -p, Git provides a mechanism to cache
810 the output and use it in future diffs. To enable caching, set the
811 "cachetextconv" variable in your diff driver’s config. For example:
812
813 [diff "jpg"]
814 textconv = exif
815 cachetextconv = true
816
817
818 This will cache the result of running "exif" on each blob
819 indefinitely. If you change the textconv config variable for a diff
820 driver, Git will automatically invalidate the cache entries and
821 re-run the textconv filter. If you want to invalidate the cache
822 manually (e.g., because your version of "exif" was updated and now
823 produces better output), you can remove the cache manually with git
824 update-ref -d refs/notes/textconv/jpg (where "jpg" is the name of
825 the diff driver, as in the example above).
826
827 Choosing textconv versus external diff
828 If you want to show differences between binary or
829 specially-formatted blobs in your repository, you can choose to use
830 either an external diff command, or to use textconv to convert them
831 to a diff-able text format. Which method you choose depends on your
832 exact situation.
833
834 The advantage of using an external diff command is flexibility. You
835 are not bound to find line-oriented changes, nor is it necessary
836 for the output to resemble unified diff. You are free to locate and
837 report changes in the most appropriate way for your data format.
838
839 A textconv, by comparison, is much more limiting. You provide a
840 transformation of the data into a line-oriented text format, and
841 Git uses its regular diff tools to generate the output. There are
842 several advantages to choosing this method:
843
844 1. Ease of use. It is often much simpler to write a binary to text
845 transformation than it is to perform your own diff. In many
846 cases, existing programs can be used as textconv filters (e.g.,
847 exif, odt2txt).
848
849 2. Git diff features. By performing only the transformation step
850 yourself, you can still utilize many of Git’s diff features,
851 including colorization, word-diff, and combined diffs for
852 merges.
853
854 3. Caching. Textconv caching can speed up repeated diffs, such as
855 those you might trigger by running git log -p.
856
857 Marking files as binary
858 Git usually guesses correctly whether a blob contains text or
859 binary data by examining the beginning of the contents. However,
860 sometimes you may want to override its decision, either because a
861 blob contains binary data later in the file, or because the
862 content, while technically composed of text characters, is opaque
863 to a human reader. For example, many postscript files contain only
864 ASCII characters, but produce noisy and meaningless diffs.
865
866 The simplest way to mark a file as binary is to unset the diff
867 attribute in the .gitattributes file:
868
869 *.ps -diff
870
871
872 This will cause Git to generate Binary files differ (or a binary
873 patch, if binary patches are enabled) instead of a regular diff.
874
875 However, one may also want to specify other diff driver attributes.
876 For example, you might want to use textconv to convert postscript
877 files to an ASCII representation for human viewing, but otherwise
878 treat them as binary files. You cannot specify both -diff and
879 diff=ps attributes. The solution is to use the diff.*.binary config
880 option:
881
882 [diff "ps"]
883 textconv = ps2ascii
884 binary = true
885
886
887 Performing a three-way merge
888 merge
889 The attribute merge affects how three versions of a file are merged
890 when a file-level merge is necessary during git merge, and other
891 commands such as git revert and git cherry-pick.
892
893 Set
894 Built-in 3-way merge driver is used to merge the contents in a
895 way similar to merge command of RCS suite. This is suitable for
896 ordinary text files.
897
898 Unset
899 Take the version from the current branch as the tentative merge
900 result, and declare that the merge has conflicts. This is
901 suitable for binary files that do not have a well-defined merge
902 semantics.
903
904 Unspecified
905 By default, this uses the same built-in 3-way merge driver as
906 is the case when the merge attribute is set. However, the
907 merge.default configuration variable can name different merge
908 driver to be used with paths for which the merge attribute is
909 unspecified.
910
911 String
912 3-way merge is performed using the specified custom merge
913 driver. The built-in 3-way merge driver can be explicitly
914 specified by asking for "text" driver; the built-in "take the
915 current branch" driver can be requested with "binary".
916
917 Built-in merge drivers
918 There are a few built-in low-level merge drivers defined that can
919 be asked for via the merge attribute.
920
921 text
922 Usual 3-way file level merge for text files. Conflicted regions
923 are marked with conflict markers <<<<<<<, ======= and >>>>>>>.
924 The version from your branch appears before the ======= marker,
925 and the version from the merged branch appears after the
926 ======= marker.
927
928 binary
929 Keep the version from your branch in the work tree, but leave
930 the path in the conflicted state for the user to sort out.
931
932 union
933 Run 3-way file level merge for text files, but take lines from
934 both versions, instead of leaving conflict markers. This tends
935 to leave the added lines in the resulting file in random order
936 and the user should verify the result. Do not use this if you
937 do not understand the implications.
938
939 Defining a custom merge driver
940 The definition of a merge driver is done in the .git/config file,
941 not in the gitattributes file, so strictly speaking this manual
942 page is a wrong place to talk about it. However...
943
944 To define a custom merge driver filfre, add a section to your
945 $GIT_DIR/config file (or $HOME/.gitconfig file) like this:
946
947 [merge "filfre"]
948 name = feel-free merge driver
949 driver = filfre %O %A %B %L %P
950 recursive = binary
951
952
953 The merge.*.name variable gives the driver a human-readable name.
954
955 The ‘merge.*.driver` variable’s value is used to construct a
956 command to run to merge ancestor’s version (%O), current version
957 (%A) and the other branches’ version (%B). These three tokens are
958 replaced with the names of temporary files that hold the contents
959 of these versions when the command line is built. Additionally, %L
960 will be replaced with the conflict marker size (see below).
961
962 The merge driver is expected to leave the result of the merge in
963 the file named with %A by overwriting it, and exit with zero status
964 if it managed to merge them cleanly, or non-zero if there were
965 conflicts.
966
967 The merge.*.recursive variable specifies what other merge driver to
968 use when the merge driver is called for an internal merge between
969 common ancestors, when there are more than one. When left
970 unspecified, the driver itself is used for both internal merge and
971 the final merge.
972
973 The merge driver can learn the pathname in which the merged result
974 will be stored via placeholder %P.
975
976 conflict-marker-size
977 This attribute controls the length of conflict markers left in the
978 work tree file during a conflicted merge. Only setting to the value
979 to a positive integer has any meaningful effect.
980
981 For example, this line in .gitattributes can be used to tell the
982 merge machinery to leave much longer (instead of the usual
983 7-character-long) conflict markers when merging the file
984 Documentation/git-merge.txt results in a conflict.
985
986 Documentation/git-merge.txt conflict-marker-size=32
987
988
989 Checking whitespace errors
990 whitespace
991 The core.whitespace configuration variable allows you to define
992 what diff and apply should consider whitespace errors for all paths
993 in the project (See git-config(1)). This attribute gives you finer
994 control per path.
995
996 Set
997 Notice all types of potential whitespace errors known to Git.
998 The tab width is taken from the value of the core.whitespace
999 configuration variable.
1000
1001 Unset
1002 Do not notice anything as error.
1003
1004 Unspecified
1005 Use the value of the core.whitespace configuration variable to
1006 decide what to notice as error.
1007
1008 String
1009 Specify a comma separate list of common whitespace problems to
1010 notice in the same format as the core.whitespace configuration
1011 variable.
1012
1013 Creating an archive
1014 export-ignore
1015 Files and directories with the attribute export-ignore won’t be
1016 added to archive files.
1017
1018 export-subst
1019 If the attribute export-subst is set for a file then Git will
1020 expand several placeholders when adding this file to an archive.
1021 The expansion depends on the availability of a commit ID, i.e., if
1022 git-archive(1) has been given a tree instead of a commit or a tag
1023 then no replacement will be done. The placeholders are the same as
1024 those for the option --pretty=format: of git-log(1), except that
1025 they need to be wrapped like this: $Format:PLACEHOLDERS$ in the
1026 file. E.g. the string $Format:%H$ will be replaced by the commit
1027 hash.
1028
1029 Packing objects
1030 delta
1031 Delta compression will not be attempted for blobs for paths with
1032 the attribute delta set to false.
1033
1034 Viewing files in GUI tools
1035 encoding
1036 The value of this attribute specifies the character encoding that
1037 should be used by GUI tools (e.g. gitk(1) and git-gui(1)) to
1038 display the contents of the relevant file. Note that due to
1039 performance considerations gitk(1) does not use this attribute
1040 unless you manually enable per-file encodings in its options.
1041
1042 If this attribute is not set or has an invalid value, the value of
1043 the gui.encoding configuration variable is used instead (See git-
1044 config(1)).
1045
1047 You do not want any end-of-line conversions applied to, nor textual
1048 diffs produced for, any binary file you track. You would need to
1049 specify e.g.
1050
1051 *.jpg -text -diff
1052
1053
1054 but that may become cumbersome, when you have many attributes. Using
1055 macro attributes, you can define an attribute that, when set, also sets
1056 or unsets a number of other attributes at the same time. The system
1057 knows a built-in macro attribute, binary:
1058
1059 *.jpg binary
1060
1061
1062 Setting the "binary" attribute also unsets the "text" and "diff"
1063 attributes as above. Note that macro attributes can only be "Set",
1064 though setting one might have the effect of setting or unsetting other
1065 attributes or even returning other attributes to the "Unspecified"
1066 state.
1067
1069 Custom macro attributes can be defined only in top-level gitattributes
1070 files ($GIT_DIR/info/attributes, the .gitattributes file at the top
1071 level of the working tree, or the global or system-wide gitattributes
1072 files), not in .gitattributes files in working tree subdirectories. The
1073 built-in macro attribute "binary" is equivalent to:
1074
1075 [attr]binary -diff -merge -text
1076
1077
1079 If you have these three gitattributes file:
1080
1081 (in $GIT_DIR/info/attributes)
1082
1083 a* foo !bar -baz
1084
1085 (in .gitattributes)
1086 abc foo bar baz
1087
1088 (in t/.gitattributes)
1089 ab* merge=filfre
1090 abc -foo -bar
1091 *.c frotz
1092
1093
1094 the attributes given to path t/abc are computed as follows:
1095
1096 1. By examining t/.gitattributes (which is in the same directory as
1097 the path in question), Git finds that the first line matches.
1098 merge attribute is set. It also finds that the second line matches,
1099 and attributes foo and bar are unset.
1100
1101 2. Then it examines .gitattributes (which is in the parent directory),
1102 and finds that the first line matches, but t/.gitattributes file
1103 already decided how merge, foo and bar attributes should be given
1104 to this path, so it leaves foo and bar unset. Attribute baz is set.
1105
1106 3. Finally it examines $GIT_DIR/info/attributes. This file is used to
1107 override the in-tree settings. The first line is a match, and foo
1108 is set, bar is reverted to unspecified state, and baz is unset.
1109
1110 As the result, the attributes assignment to t/abc becomes:
1111
1112 foo set to true
1113 bar unspecified
1114 baz set to false
1115 merge set to string value "filfre"
1116 frotz unspecified
1117
1118
1120 git-check-attr(1).
1121
1123 Part of the git(1) suite
1124
1125
1126
1127Git 2.21.0 02/24/2019 GITATTRIBUTES(5)