blob: f605327946f1bc72a62a6129d7e783162853ad51 [file] [log] [blame]
Junio C Hamanod0587fd2005-06-22 02:30:17 -07001git-apply(1)
2============
Junio C Hamanod0587fd2005-06-22 02:30:17 -07003
4NAME
5----
Björn Gustavsson38a39642009-11-22 20:43:42 +01006git-apply - Apply a patch to files and/or to the index
Junio C Hamanod0587fd2005-06-22 02:30:17 -07007
8
9SYNOPSIS
10--------
Jonas Fonseca353ce812005-12-31 18:37:15 +010011[verse]
Junio C Hamano78fb67f2012-05-10 14:08:29 -070012'git apply' [--stat] [--numstat] [--summary] [--check] [--index] [--3way]
Miklos Vajnaf26c4942008-12-28 00:03:57 +010013 [--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse]
Andrew Ruder0b9a9dd2007-04-16 01:20:40 -050014 [--allow-binary-replacement | --binary] [--reject] [-z]
Štěpán Němec62b46982010-10-08 19:31:15 +020015 [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +020016 [--ignore-space-change | --ignore-whitespace ]
Štěpán Němec0adda932010-10-08 19:31:17 +020017 [--whitespace=(nowarn|warn|fix|error|error-all)]
Štěpán Němec62b46982010-10-08 19:31:15 +020018 [--exclude=<path>] [--include=<path>] [--directory=<root>]
Junio C Hamano6ecb1ee2008-08-25 01:05:31 -070019 [--verbose] [<patch>...]
Junio C Hamanod0587fd2005-06-22 02:30:17 -070020
21DESCRIPTION
22-----------
Björn Gustavsson38a39642009-11-22 20:43:42 +010023Reads the supplied diff output (i.e. "a patch") and applies it to files.
24With the `--index` option the patch is also applied to the index, and
Michał Kiedrowicz964498e2011-03-03 10:28:30 +010025with the `--cached` option the patch is only applied to the index.
Björn Gustavsson38a39642009-11-22 20:43:42 +010026Without these options, the command applies the patch only to files,
Thomas Ackermann2de9b712013-01-21 20:17:53 +010027and does not require them to be in a Git repository.
Junio C Hamanod0587fd2005-06-22 02:30:17 -070028
Brad King08b29822010-08-03 14:41:50 -040029This command applies the patch but does not create a commit. Use
30linkgit:git-am[1] to create commits from patches generated by
31linkgit:git-format-patch[1] and/or received by email.
32
Junio C Hamanod0587fd2005-06-22 02:30:17 -070033OPTIONS
34-------
Junio C Hamano12dd6e82005-07-13 20:28:55 -070035<patch>...::
David J. Mellor1d035f82009-03-01 22:42:44 -080036 The files to read the patch from. '-' can be used to read
Junio C Hamano12dd6e82005-07-13 20:28:55 -070037 from the standard input.
Junio C Hamanod0587fd2005-06-22 02:30:17 -070038
Junio C Hamanod0587fd2005-06-22 02:30:17 -070039--stat::
40 Instead of applying the patch, output diffstat for the
Junio C Hamano12dd6e82005-07-13 20:28:55 -070041 input. Turns off "apply".
Junio C Hamanod0587fd2005-06-22 02:30:17 -070042
Junio C Hamano7d8b7c22005-10-28 02:43:31 -070043--numstat::
Björn Gustavssonf9821e22009-11-22 20:43:53 +010044 Similar to `--stat`, but shows the number of added and
David J. Mellor1d035f82009-03-01 22:42:44 -080045 deleted lines in decimal notation and the pathname without
Junio C Hamano2f895432006-12-26 00:15:26 -080046 abbreviation, to make it more machine friendly. For
47 binary files, outputs two `-` instead of saying
48 `0 0`. Turns off "apply".
Junio C Hamano7d8b7c22005-10-28 02:43:31 -070049
Junio C Hamanod0587fd2005-06-22 02:30:17 -070050--summary::
51 Instead of applying the patch, output a condensed
52 summary of information obtained from git diff extended
53 headers, such as creations, renames and mode changes.
Junio C Hamano12dd6e82005-07-13 20:28:55 -070054 Turns off "apply".
Junio C Hamanod0587fd2005-06-22 02:30:17 -070055
56--check::
57 Instead of applying the patch, see if the patch is
Björn Gustavssonc34ec652009-11-22 20:53:29 +010058 applicable to the current working tree and/or the index
Junio C Hamano12dd6e82005-07-13 20:28:55 -070059 file and detects errors. Turns off "apply".
Junio C Hamanod0587fd2005-06-22 02:30:17 -070060
61--index::
Björn Gustavssonf9821e22009-11-22 20:43:53 +010062 When `--check` is in effect, or when applying the patch
Junio C Hamanod0587fd2005-06-22 02:30:17 -070063 (which is the default when none of the options that
64 disables it is in effect), make sure the patch is
65 applicable to what the current index file records. If
Björn Gustavssonc34ec652009-11-22 20:53:29 +010066 the file to be patched in the working tree is not
Junio C Hamanod0587fd2005-06-22 02:30:17 -070067 up-to-date, it is flagged as an error. This flag also
68 causes the index file to be updated.
69
Jonas Fonseca5684ed62006-08-25 02:56:55 +020070--cached::
David J. Mellor1d035f82009-03-01 22:42:44 -080071 Apply a patch without touching the working tree. Instead take the
72 cached data, apply the patch, and store the result in the index
Björn Gustavssonf9821e22009-11-22 20:43:53 +010073 without using the working tree. This implies `--index`.
Jonas Fonseca5684ed62006-08-25 02:56:55 +020074
Junio C Hamano78fb67f2012-05-10 14:08:29 -070075-3::
76--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 incompatible
82 with the `--reject` and the `--cached` options.
83
Miklos Vajnaf26c4942008-12-28 00:03:57 +010084--build-fake-ancestor=<file>::
Thomas Rast0b444cd2010-01-10 00:33:00 +010085 Newer 'git diff' output has embedded 'index information'
Junio C Hamanod88156e2005-10-14 21:56:46 -070086 for each blob to help identify the original version that
87 the patch applies to. When this flag is given, and if
David J. Mellor1d035f82009-03-01 22:42:44 -080088 the original versions of the blobs are available locally,
Johannes Schindelin7a988692007-09-17 23:34:06 +010089 builds a temporary index containing those blobs.
90+
91When a pure mode change is encountered (which has no index information),
92the information is read from the current index instead.
Junio C Hamanod88156e2005-10-14 21:56:46 -070093
Stephan Beyer32402402008-06-08 03:36:09 +020094-R::
95--reverse::
Jonas Fonseca5684ed62006-08-25 02:56:55 +020096 Apply the patch in reverse.
97
98--reject::
Thomas Rast0b444cd2010-01-10 00:33:00 +010099 For atomicity, 'git apply' by default fails the whole patch and
Jonas Fonseca5684ed62006-08-25 02:56:55 +0200100 does not touch the working tree when some of the hunks
101 do not apply. This option makes it apply
Junio C Hamanob32d37a2006-08-27 17:51:05 -0700102 the parts of the patch that are applicable, and leave the
Junio C Hamano89380452006-08-27 15:53:20 -0700103 rejected hunks in corresponding *.rej files.
Jonas Fonseca5684ed62006-08-25 02:56:55 +0200104
Junio C Hamanod88156e2005-10-14 21:56:46 -0700105-z::
Björn Gustavsson64485b42009-11-22 20:43:20 +0100106 When `--numstat` has been given, do not munge pathnames,
107 but use a NUL-terminated machine-readable format.
108+
109Without this option, each pathname output will have TAB, LF, double quotes,
110and backslash characters replaced with `\t`, `\n`, `\"`, and `\\`,
111respectively, and the pathname will be enclosed in double quotes if
112any of those replacements occurred.
Junio C Hamanod88156e2005-10-14 21:56:46 -0700113
Daniel Barkalowe36f8b62006-01-31 00:36:24 -0500114-p<n>::
115 Remove <n> leading slashes from traditional diff paths. The
116 default is 1.
117
Eric W. Biederman47495882006-04-10 03:33:06 -0600118-C<n>::
119 Ensure at least <n> lines of surrounding context match before
120 and after each change. When fewer lines of surrounding
Jonas Fonseca74237d62006-06-07 20:32:33 +0200121 context exist they all must match. By default no context is
Eric W. Biederman47495882006-04-10 03:33:06 -0600122 ever ignored.
123
Jonas Fonsecaf58bb6f2006-09-17 12:55:38 +0200124--unidiff-zero::
Thomas Rast0b444cd2010-01-10 00:33:00 +0100125 By default, 'git apply' expects that the patch being
Jonas Fonsecaf58bb6f2006-09-17 12:55:38 +0200126 applied is a unified diff with at least one line of context.
127 This provides good safety measures, but breaks down when
Björn Gustavssonf9821e22009-11-22 20:43:53 +0100128 applying a diff generated with `--unified=0`. To bypass these
129 checks use `--unidiff-zero`.
Jonas Fonsecaf58bb6f2006-09-17 12:55:38 +0200130+
David J. Mellor1d035f82009-03-01 22:42:44 -0800131Note, for the reasons stated above usage of context-free patches is
Jonas Fonsecaf58bb6f2006-09-17 12:55:38 +0200132discouraged.
133
Junio C Hamano12dd6e82005-07-13 20:28:55 -0700134--apply::
Jonas Fonseca5684ed62006-08-25 02:56:55 +0200135 If you use any of the options marked "Turns off
Thomas Rast0b444cd2010-01-10 00:33:00 +0100136 'apply'" above, 'git apply' reads and outputs the
David J. Mellor1d035f82009-03-01 22:42:44 -0800137 requested information without actually applying the
Junio C Hamano12dd6e82005-07-13 20:28:55 -0700138 patch. Give this flag after those flags to also apply
139 the patch.
140
Junio C Hamanoe4337052005-11-09 20:53:54 -0800141--no-add::
142 When applying a patch, ignore additions made by the
Wincent Colaiuta71a98832007-12-12 09:14:20 +0100143 patch. This can be used to extract the common part between
Jonathan Nieder2fd02c92008-07-03 00:55:07 -0500144 two files by first running 'diff' on them and applying
Junio C Hamanoe4337052005-11-09 20:53:54 -0800145 the result with this option, which would apply the
David J. Mellor1d035f82009-03-01 22:42:44 -0800146 deletion part but not the addition part.
Junio C Hamanod0587fd2005-06-22 02:30:17 -0700147
Stephan Beyer32402402008-06-08 03:36:09 +0200148--allow-binary-replacement::
149--binary::
Junio C Hamano2b6eef92006-09-06 22:45:21 -0700150 Historically we did not allow binary patch applied
151 without an explicit permission from the user, and this
152 flag was the way to do so. Currently we always allow binary
153 patch application, so this is a no-op.
Junio C Hamano27dedf02005-11-16 21:32:44 -0800154
Jonas Fonseca5684ed62006-08-25 02:56:55 +0200155--exclude=<path-pattern>::
156 Don't apply changes to files matching the given path pattern. This can
157 be useful when importing patchsets, where you want to exclude certain
158 files or directories.
159
Junio C Hamano6ecb1ee2008-08-25 01:05:31 -0700160--include=<path-pattern>::
161 Apply changes to files matching the given path pattern. This can
162 be useful when importing patchsets, where you want to include certain
163 files or directories.
164+
Björn Gustavssonf9821e22009-11-22 20:43:53 +0100165When `--exclude` and `--include` patterns are used, they are examined in the
Junio C Hamano6ecb1ee2008-08-25 01:05:31 -0700166order they appear on the command line, and the first match determines if a
167patch to each path is used. A patch to a path that does not match any
168include/exclude pattern is used by default if there is no include pattern
169on the command line, and ignored if there is any include pattern.
170
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200171--ignore-space-change::
172--ignore-whitespace::
173 When applying a patch, ignore changes in whitespace in context
174 lines if necessary.
175 Context lines will preserve their whitespace, and they will not
176 undergo whitespace fixing regardless of the value of the
177 `--whitespace` option. New lines will still be fixed, though.
178
Junio C Hamano91af7ae2007-11-24 11:57:41 -0800179--whitespace=<action>::
180 When applying a patch, detect a new or modified line that has
181 whitespace errors. What are considered whitespace errors is
182 controlled by `core.whitespace` configuration. By default,
183 trailing whitespaces (including lines that solely consist of
184 whitespaces) and a space character that is immediately followed
185 by a tab character inside the initial indent of the line are
186 considered whitespace errors.
187+
188By default, the command outputs warning messages but applies the patch.
Danijel Tasoveb006cc2009-02-28 21:03:54 +0100189When `git-apply` is used for statistics and not applying a
Junio C Hamano91af7ae2007-11-24 11:57:41 -0800190patch, it defaults to `nowarn`.
191+
David J. Mellor1d035f82009-03-01 22:42:44 -0800192You can use different `<action>` values to control this
Junio C Hamano91af7ae2007-11-24 11:57:41 -0800193behavior:
Junio C Hamano8273c792006-03-02 00:45:48 -0800194+
195* `nowarn` turns off the trailing whitespace warning.
196* `warn` outputs warnings for a few such errors, but applies the
Junio C Hamano91af7ae2007-11-24 11:57:41 -0800197 patch as-is (default).
198* `fix` outputs warnings for a few such errors, and applies the
199 patch after fixing them (`strip` is a synonym --- the tool
David J. Mellor1d035f82009-03-01 22:42:44 -0800200 used to consider only trailing whitespace characters as errors, and the
Thomas Ackermann2de9b712013-01-21 20:17:53 +0100201 fix involved 'stripping' them, but modern Gits do more).
Junio C Hamano8273c792006-03-02 00:45:48 -0800202* `error` outputs warnings for a few such errors, and refuses
203 to apply the patch.
204* `error-all` is similar to `error` but shows all errors.
Junio C Hamano8273c792006-03-02 00:45:48 -0800205
Petr Baudisf847c072006-11-18 06:05:11 +0100206--inaccurate-eof::
Jonathan Nieder0979c102008-07-03 00:37:18 -0500207 Under certain circumstances, some versions of 'diff' do not correctly
Jonas Fonseca5684ed62006-08-25 02:56:55 +0200208 detect a missing new-line at the end of the file. As a result, patches
Jonathan Nieder0979c102008-07-03 00:37:18 -0500209 created by such 'diff' programs do not record incomplete lines
Jonas Fonseca5684ed62006-08-25 02:56:55 +0200210 correctly. This option adds support for applying such patches by
211 working around this bug.
212
Stephan Beyer32402402008-06-08 03:36:09 +0200213-v::
214--verbose::
Jonas Fonseca5684ed62006-08-25 02:56:55 +0200215 Report progress to stderr. By default, only a message about the
216 current patch being applied will be printed. This option will cause
217 additional information to be reported.
Junio C Hamano8273c792006-03-02 00:45:48 -0800218
Johannes Schindelinc14b9d12008-06-27 18:43:09 +0100219--recount::
220 Do not trust the line counts in the hunk headers, but infer them
221 by inspecting the patch (e.g. after editing the patch without
222 adjusting the hunk headers appropriately).
223
Junio C Hamanof5563882008-07-06 18:36:01 -0700224--directory=<root>::
David J. Mellor1d035f82009-03-01 22:42:44 -0800225 Prepend <root> to all filenames. If a "-p" argument was also passed,
Johannes Schindelinc4730f32008-07-01 00:44:47 +0100226 it is applied before prepending the new root.
Junio C Hamanof5563882008-07-06 18:36:01 -0700227+
228For example, a patch that talks about updating `a/git-gui.sh` to `b/git-gui.sh`
229can be applied to the file in the working tree `modules/git-gui/git-gui.sh` by
230running `git apply --directory=modules/git-gui`.
Johannes Schindelinc4730f32008-07-01 00:44:47 +0100231
Junio C Hamano8273c792006-03-02 00:45:48 -0800232Configuration
233-------------
234
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200235apply.ignorewhitespace::
236 Set to 'change' if you want changes in whitespace to be ignored by default.
237 Set to one of: no, none, never, false if you want changes in
238 whitespace to be significant.
Junio C Hamano8273c792006-03-02 00:45:48 -0800239apply.whitespace::
240 When no `--whitespace` flag is given from the command
241 line, this configuration item is used as the default.
242
Sven Verdoolaegee06c5a62007-08-15 19:22:09 +0200243Submodules
244----------
Thomas Rast0b444cd2010-01-10 00:33:00 +0100245If the patch contains any changes to submodules then 'git apply'
Sven Verdoolaegee06c5a62007-08-15 19:22:09 +0200246treats these changes as follows.
247
Björn Gustavssonf9821e22009-11-22 20:43:53 +0100248If `--index` is specified (explicitly or implicitly), then the submodule
Sven Verdoolaegee06c5a62007-08-15 19:22:09 +0200249commits must match the index exactly for the patch to apply. If any
250of the submodules are checked-out, then these check-outs are completely
251ignored, i.e., they are not required to be up-to-date or clean and they
252are not updated.
253
Björn Gustavssonf9821e22009-11-22 20:43:53 +0100254If `--index` is not specified, then the submodule commits in the patch
David J. Mellor1d035f82009-03-01 22:42:44 -0800255are ignored and only the absence or presence of the corresponding
Sven Verdoolaegee06c5a62007-08-15 19:22:09 +0200256subdirectory is checked and (if possible) updated.
Junio C Hamano8273c792006-03-02 00:45:48 -0800257
Brad King08b29822010-08-03 14:41:50 -0400258SEE ALSO
259--------
260linkgit:git-am[1].
261
Junio C Hamanod0587fd2005-06-22 02:30:17 -0700262GIT
263---
Christian Couder9e1f0a82008-06-06 09:07:32 +0200264Part of the linkgit:git[1] suite