]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-apply.txt
Sync with 2.31.4
[thirdparty/git.git] / Documentation / git-apply.txt
CommitLineData
d0587fd5
JH
1git-apply(1)
2============
d0587fd5
JH
3
4NAME
5----
38a39647 6git-apply - Apply a patch to files and/or to the index
d0587fd5
JH
7
8
9SYNOPSIS
10--------
353ce815 11[verse]
cff5dc09 12'git apply' [--stat] [--numstat] [--summary] [--check] [--index | --intent-to-add] [--3way]
f26c4940 13 [--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse]
0b9a9dd0 14 [--allow-binary-replacement | --binary] [--reject] [-z]
62b4698e 15 [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
d725fbde 16 [--ignore-space-change | --ignore-whitespace]
0adda936 17 [--whitespace=(nowarn|warn|fix|error|error-all)]
62b4698e 18 [--exclude=<path>] [--include=<path>] [--directory=<root>]
c536c075 19 [--verbose] [--unsafe-paths] [<patch>...]
d0587fd5
JH
20
21DESCRIPTION
22-----------
38a39647 23Reads the supplied diff output (i.e. "a patch") and applies it to files.
16a86d43
NTND
24When running from a subdirectory in a repository, patched paths
25outside the directory are ignored.
38a39647 26With the `--index` option the patch is also applied to the index, and
964498e7 27with the `--cached` option the patch is only applied to the index.
38a39647 28Without these options, the command applies the patch only to files,
2de9b711 29and does not require them to be in a Git repository.
d0587fd5 30
08b29826
BK
31This command applies the patch but does not create a commit. Use
32linkgit:git-am[1] to create commits from patches generated by
33linkgit:git-format-patch[1] and/or received by email.
34
d0587fd5
JH
35OPTIONS
36-------
12dd6e8c 37<patch>...::
1d035f85 38 The files to read the patch from. '-' can be used to read
12dd6e8c 39 from the standard input.
d0587fd5 40
d0587fd5
JH
41--stat::
42 Instead of applying the patch, output diffstat for the
12dd6e8c 43 input. Turns off "apply".
d0587fd5 44
7d8b7c21 45--numstat::
f9821e2b 46 Similar to `--stat`, but shows the number of added and
1d035f85 47 deleted lines in decimal notation and the pathname without
2f89543e
JH
48 abbreviation, to make it more machine friendly. For
49 binary files, outputs two `-` instead of saying
50 `0 0`. Turns off "apply".
7d8b7c21 51
d0587fd5
JH
52--summary::
53 Instead of applying the patch, output a condensed
54 summary of information obtained from git diff extended
55 headers, such as creations, renames and mode changes.
12dd6e8c 56 Turns off "apply".
d0587fd5
JH
57
58--check::
59 Instead of applying the patch, see if the patch is
c34ec655 60 applicable to the current working tree and/or the index
12dd6e8c 61 file and detects errors. Turns off "apply".
d0587fd5
JH
62
63--index::
d064702b
RP
64 Apply the patch to both the index and the working tree (or
65 merely check that it would apply cleanly to both if `--check` is
66 in effect). Note that `--index` expects index entries and
67 working tree copies for relevant paths to be identical (their
68 contents and metadata such as file mode must match), and will
69 raise an error if they are not, even if the patch would apply
70 cleanly to both the index and the working tree in isolation.
d0587fd5 71
5684ed6d 72--cached::
d064702b
RP
73 Apply the patch to just the index, without touching the working
74 tree. If `--check` is in effect, merely check that it would
75 apply cleanly to the index entry.
5684ed6d 76
cff5dc09
NTND
77--intent-to-add::
78 When applying the patch only to the working tree, mark new
79 files to be added to the index later (see `--intent-to-add`
80 option in linkgit:git-add[1]). This option is ignored unless
81 running in a Git repository and `--index` is not specified.
82 Note that `--index` could be implied by other options such
83 as `--cached` or `--3way`.
84
78fb67f3
JH
85-3::
86--3way::
923cd87a
JZ
87 Attempt 3-way merge if the patch records the identity of blobs it is supposed
88 to apply to and we have those blobs available locally, possibly leaving the
78fb67f3 89 conflict markers in the files in the working tree for the user to
c0c2a37a
JZ
90 resolve. This option implies the `--index` option unless the
91 `--cached` option is used, and is incompatible with the `--reject` option.
92 When used with the `--cached` option, any conflicts are left at higher stages
93 in the cache.
78fb67f3 94
f26c4940 95--build-fake-ancestor=<file>::
0b444cdb 96 Newer 'git diff' output has embedded 'index information'
d88156e9
JH
97 for each blob to help identify the original version that
98 the patch applies to. When this flag is given, and if
1d035f85 99 the original versions of the blobs are available locally,
7a988699
JS
100 builds a temporary index containing those blobs.
101+
102When a pure mode change is encountered (which has no index information),
103the information is read from the current index instead.
d88156e9 104
3240240f
SB
105-R::
106--reverse::
5684ed6d
JF
107 Apply the patch in reverse.
108
109--reject::
0b444cdb 110 For atomicity, 'git apply' by default fails the whole patch and
5684ed6d
JF
111 does not touch the working tree when some of the hunks
112 do not apply. This option makes it apply
b32d37a3 113 the parts of the patch that are applicable, and leave the
8938045a 114 rejected hunks in corresponding *.rej files.
5684ed6d 115
d88156e9 116-z::
64485b4a
BG
117 When `--numstat` has been given, do not munge pathnames,
118 but use a NUL-terminated machine-readable format.
119+
860cd699
AH
120Without this option, pathnames with "unusual" characters are quoted as
121explained for the configuration variable `core.quotePath` (see
122linkgit:git-config[1]).
d88156e9 123
e36f8b60 124-p<n>::
ee930754
JK
125 Remove <n> leading path components (separated by slashes) from
126 traditional diff paths. E.g., with `-p2`, a patch against
127 `a/dir/file` will be applied directly to `file`. The default is
128 1.
e36f8b60 129
47495887
EB
130-C<n>::
131 Ensure at least <n> lines of surrounding context match before
132 and after each change. When fewer lines of surrounding
74237d62 133 context exist they all must match. By default no context is
47495887
EB
134 ever ignored.
135
f58bb6fb 136--unidiff-zero::
0b444cdb 137 By default, 'git apply' expects that the patch being
f58bb6fb
JF
138 applied is a unified diff with at least one line of context.
139 This provides good safety measures, but breaks down when
f9821e2b
BG
140 applying a diff generated with `--unified=0`. To bypass these
141 checks use `--unidiff-zero`.
f58bb6fb 142+
1d035f85 143Note, for the reasons stated above usage of context-free patches is
f58bb6fb
JF
144discouraged.
145
12dd6e8c 146--apply::
5684ed6d 147 If you use any of the options marked "Turns off
0b444cdb 148 'apply'" above, 'git apply' reads and outputs the
1d035f85 149 requested information without actually applying the
12dd6e8c
JH
150 patch. Give this flag after those flags to also apply
151 the patch.
152
e433705d
JH
153--no-add::
154 When applying a patch, ignore additions made by the
71a9883d 155 patch. This can be used to extract the common part between
2fd02c92 156 two files by first running 'diff' on them and applying
e433705d 157 the result with this option, which would apply the
1d035f85 158 deletion part but not the addition part.
d0587fd5 159
3240240f
SB
160--allow-binary-replacement::
161--binary::
2b6eef94
JH
162 Historically we did not allow binary patch applied
163 without an explicit permission from the user, and this
164 flag was the way to do so. Currently we always allow binary
165 patch application, so this is a no-op.
27dedf0c 166
5684ed6d
JF
167--exclude=<path-pattern>::
168 Don't apply changes to files matching the given path pattern. This can
169 be useful when importing patchsets, where you want to exclude certain
170 files or directories.
171
6ecb1ee2
JH
172--include=<path-pattern>::
173 Apply changes to files matching the given path pattern. This can
174 be useful when importing patchsets, where you want to include certain
175 files or directories.
176+
f9821e2b 177When `--exclude` and `--include` patterns are used, they are examined in the
6ecb1ee2
JH
178order they appear on the command line, and the first match determines if a
179patch to each path is used. A patch to a path that does not match any
180include/exclude pattern is used by default if there is no include pattern
181on the command line, and ignored if there is any include pattern.
182
86c91f91
GB
183--ignore-space-change::
184--ignore-whitespace::
185 When applying a patch, ignore changes in whitespace in context
186 lines if necessary.
187 Context lines will preserve their whitespace, and they will not
188 undergo whitespace fixing regardless of the value of the
189 `--whitespace` option. New lines will still be fixed, though.
190
91af7ae5
JH
191--whitespace=<action>::
192 When applying a patch, detect a new or modified line that has
193 whitespace errors. What are considered whitespace errors is
194 controlled by `core.whitespace` configuration. By default,
195 trailing whitespaces (including lines that solely consist of
196 whitespaces) and a space character that is immediately followed
197 by a tab character inside the initial indent of the line are
198 considered whitespace errors.
199+
200By default, the command outputs warning messages but applies the patch.
eb006ccf 201When `git-apply` is used for statistics and not applying a
91af7ae5
JH
202patch, it defaults to `nowarn`.
203+
1d035f85 204You can use different `<action>` values to control this
91af7ae5 205behavior:
8273c79a
JH
206+
207* `nowarn` turns off the trailing whitespace warning.
208* `warn` outputs warnings for a few such errors, but applies the
91af7ae5
JH
209 patch as-is (default).
210* `fix` outputs warnings for a few such errors, and applies the
211 patch after fixing them (`strip` is a synonym --- the tool
1d035f85 212 used to consider only trailing whitespace characters as errors, and the
2de9b711 213 fix involved 'stripping' them, but modern Gits do more).
8273c79a
JH
214* `error` outputs warnings for a few such errors, and refuses
215 to apply the patch.
216* `error-all` is similar to `error` but shows all errors.
8273c79a 217
f847c07b 218--inaccurate-eof::
0979c106 219 Under certain circumstances, some versions of 'diff' do not correctly
5684ed6d 220 detect a missing new-line at the end of the file. As a result, patches
0979c106 221 created by such 'diff' programs do not record incomplete lines
5684ed6d
JF
222 correctly. This option adds support for applying such patches by
223 working around this bug.
224
3240240f
SB
225-v::
226--verbose::
5684ed6d
JF
227 Report progress to stderr. By default, only a message about the
228 current patch being applied will be printed. This option will cause
229 additional information to be reported.
8273c79a 230
c14b9d1e
JS
231--recount::
232 Do not trust the line counts in the hunk headers, but infer them
233 by inspecting the patch (e.g. after editing the patch without
234 adjusting the hunk headers appropriately).
235
f5563887 236--directory=<root>::
1d035f85 237 Prepend <root> to all filenames. If a "-p" argument was also passed,
c4730f35 238 it is applied before prepending the new root.
f5563887
JH
239+
240For example, a patch that talks about updating `a/git-gui.sh` to `b/git-gui.sh`
241can be applied to the file in the working tree `modules/git-gui/git-gui.sh` by
242running `git apply --directory=modules/git-gui`.
c4730f35 243
c536c075
JH
244--unsafe-paths::
245 By default, a patch that affects outside the working area
246 (either a Git controlled working tree, or the current working
247 directory when "git apply" is used as a replacement of GNU
248 patch) is rejected as a mistake (or a mischief).
249+
250When `git apply` is used as a "better GNU patch", the user can pass
251the `--unsafe-paths` option to override this safety check. This option
252has no effect when `--index` or `--cached` is in use.
253
76a8788c 254CONFIGURATION
8273c79a
JH
255-------------
256
da0005b8 257apply.ignoreWhitespace::
86c91f91
GB
258 Set to 'change' if you want changes in whitespace to be ignored by default.
259 Set to one of: no, none, never, false if you want changes in
260 whitespace to be significant.
8273c79a
JH
261apply.whitespace::
262 When no `--whitespace` flag is given from the command
263 line, this configuration item is used as the default.
264
76a8788c 265SUBMODULES
e06c5a6c 266----------
0b444cdb 267If the patch contains any changes to submodules then 'git apply'
e06c5a6c
SV
268treats these changes as follows.
269
f9821e2b 270If `--index` is specified (explicitly or implicitly), then the submodule
e06c5a6c
SV
271commits must match the index exactly for the patch to apply. If any
272of the submodules are checked-out, then these check-outs are completely
7560f547 273ignored, i.e., they are not required to be up to date or clean and they
e06c5a6c
SV
274are not updated.
275
f9821e2b 276If `--index` is not specified, then the submodule commits in the patch
1d035f85 277are ignored and only the absence or presence of the corresponding
e06c5a6c 278subdirectory is checked and (if possible) updated.
8273c79a 279
08b29826
BK
280SEE ALSO
281--------
282linkgit:git-am[1].
283
d0587fd5
JH
284GIT
285---
9e1f0a85 286Part of the linkgit:git[1] suite