git-apply β Apply a patch to files and/or to the index
| Use Case | Command | Description |
|---|---|---|
| π΅ Apply a patch to files | git apply patch.diff | Apply patch to working tree (no index involved) |
| π΅ Apply patch to index and working tree | git apply --index patch.diff | Also update the index |
| π΅ Apply patch to index only | git apply --cached patch.diff | Leave working tree untouched |
| π΅ Check if patch applies cleanly | git apply --check patch.diff | Dryβrun, detect errors |
| π΅ Show diffstat instead of applying | git apply --stat patch.diff | Summary of changes |
| π΅ Apply in reverse | git apply -R patch.diff | Undo a patch |
| π΅ Apply with 3βway merge | git apply --3way patch.diff | Attempt merge if blobs are available |
| π΅ Apply to a different directory | git apply --directory=modules/git-gui patch.diff | Prepend root to filenames |
git apply [--stat] [--numstat] [--summary] [--check] [--index | --intent-to-add] [--3way]
[--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse]
[--allow-binary-replacement | --binary] [--reject] [-z]
[-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
[--ignore-space-change | --ignore-whitespace]
[--whitespace=(nowarn|warn|fix|error|error-all)]
[--exclude=<path>] [--include=<path>] [--directory=<root>]
[--verbose] [--unsafe-paths] [<patch>...]
Reads the supplied diff output (i.e. "a patch") and applies it to files. When running from a subdirectory in a repository, patched paths outside the directory are ignored. With the --index option the patch is also applied to the index, and with the --cached option the patch is only applied to the index. Without these options, the command applies the patch only to files, and does not require them to be in a Git repository.
This command applies the patch but does not create a commit. Use git-am(1) to create commits from patches generated by git-format-patch(1) and/or received by email.
--stat, but shows the number of added and deleted lines in decimal notation and the pathname without abbreviation, to make it more machine friendly. For binary files, outputs two - instead of saying 0 0. Turns off "apply".--check is in effect). Note that --index expects index entries and working tree copies for relevant paths to be identical (their contents and metadata such as file mode must match), and will raise an error if they are not, even if the patch would apply cleanly to both the index and the working tree in isolation.--check is in effect, merely check that it would apply cleanly to the index entry.--intent-to-add option in git-add(1)). This option is ignored unless running in a Git repository and --index is not specified. Note that --index could be implied by other options such as --cached or --3way.--index option unless the --cached option is used, and is incompatible with the --reject option. When used with the --cached option, any conflicts are left at higher stages in the cache.*.rej files.--numstat has been given, do not munge pathnames, but use a NULβterminated machineβreadable format. Without this option, pathnames with "unusual" characters are quoted as explained for the configuration variable core.quotePath (see git-config(1)).<n> leading path components (separated by slashes) from traditional diff paths. E.g., with -p2, a patch against a/dir/file will be applied directly to file. The default is 1.<n> lines of surrounding context match before and after each change. When fewer lines of surrounding context exist they all must match. By default no context is ever ignored.--unified=0. To bypass these checks use --unidiff-zero. Note, for the reasons stated above usage of contextβfree patches is discouraged.--exclude and --include patterns are used, they are examined in the order they appear on the command line, and the first match determines if a patch to each path is used. A patch to a path that does not match any include/exclude pattern is used by default if there is no include pattern on the command line, and ignored if there is any include pattern.--whitespace option. New lines will still be fixed, though.core.whitespace configuration. By default, trailing whitespaces (including lines that solely consist of whitespaces) and a space character that is immediately followed by a tab character inside the initial indent of the line are considered whitespace errors. By default, the command outputs warning messages but applies the patch. When git-apply is used for statistics and not applying a patch, it defaults to nowarn. You can use different <action> values to control this behavior:
strip is a synonym β the tool used to consider only trailing whitespace characters as errors, and the fix involved stripping them, but modern Gits do more).error but shows all errors.<root> to all filenames. If a -p argument was also passed, it is applied before prepending the new root. For example, a patch that talks about updating a/git-gui.sh to b/git-gui.sh can be applied to the file in the working tree modules/git-gui/git-gui.sh by running git apply --directory=modules/git-gui.--unsafe-paths option to override this safety check. This option has no effect when --index or --cached is in use.- can be used to read from the standard input.no, none, never, false if you want changes in whitespace to be significant.--whitespace flag is given from the command line, this configuration item is used as the default.If the patch contains any changes to submodules then git apply treats these changes as follows.
If --index is specified (explicitly or implicitly), then the submodule commits must match the index exactly for the patch to apply. If any of the submodules are checkedβout, then these checkβouts are completely ignored, i.e., they are not required to be up to date or clean and they are not updated.
If --index is not specified, then the submodule commits in the patch are ignored and only the absence or presence of the corresponding subdirectory is checked and (if possible) updated.
Part of the git(1) suite
Generated by phpman v4.9.25-8-g32d6339 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-13 03:47 @216.73.217.22
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-pro / taotoken.net / www.chedong.com - original format