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