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