{
    "mode": "info",
    "parameter": "git-apply",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/git-apply/json",
    "generated": "2026-07-11T09:16:52Z",
    "synopsis": "git apply [--stat] [--numstat] [--summary] [--check] [--index | --intent-to-add] [--3way]\n[--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse]\n[--allow-binary-replacement | --binary] [--reject] [-z]\n[-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]\n[--ignore-space-change | --ignore-whitespace]\n[--whitespace=(nowarn|warn|fix|error|error-all)]\n[--exclude=<path>] [--include=<path>] [--directory=<root>]\n[--verbose] [--unsafe-paths] [<patch>...]",
    "sections": {
        "NAME": {
            "content": "git-apply - Apply a patch to files and/or to the index\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "git apply [--stat] [--numstat] [--summary] [--check] [--index | --intent-to-add] [--3way]\n[--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse]\n[--allow-binary-replacement | --binary] [--reject] [-z]\n[-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]\n[--ignore-space-change | --ignore-whitespace]\n[--whitespace=(nowarn|warn|fix|error|error-all)]\n[--exclude=<path>] [--include=<path>] [--directory=<root>]\n[--verbose] [--unsafe-paths] [<patch>...]\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Reads the supplied diff output (i.e. \"a patch\") and applies it to\nfiles. When running from a subdirectory in a repository, patched paths\noutside the directory are ignored. With the --index option the patch is\nalso applied to the index, and with the --cached option the patch is\nonly applied to the index. Without these options, the command applies\nthe patch only to files, and does not require them to be in a Git\nrepository.\n\nThis command applies the patch but does not create a commit. Use git-\nam(1) to create commits from patches generated by git-format-patch(1)\nand/or received by email.\n",
            "subsections": []
        },
        "OPTIONS": {
            "content": "<patch>...\nThe files to read the patch from.  - can be used to read from the\nstandard input.\n",
            "subsections": [
                {
                    "name": "--stat",
                    "content": "Instead of applying the patch, output diffstat for the input. Turns\noff \"apply\".\n",
                    "long": "--stat"
                },
                {
                    "name": "--numstat",
                    "content": "Similar to --stat, but shows the number of added and deleted lines\nin decimal notation and the pathname without abbreviation, to make\nit more machine friendly. For binary files, outputs two - instead\nof saying 0 0. Turns off \"apply\".\n",
                    "long": "--numstat"
                },
                {
                    "name": "--summary",
                    "content": "Instead of applying the patch, output a condensed summary of\ninformation obtained from git diff extended headers, such as\ncreations, renames and mode changes. Turns off \"apply\".\n",
                    "long": "--summary"
                },
                {
                    "name": "--check",
                    "content": "Instead of applying the patch, see if the patch is applicable to\nthe current working tree and/or the index file and detects errors.\nTurns off \"apply\".\n",
                    "long": "--check"
                },
                {
                    "name": "--index",
                    "content": "Apply the patch to both the index and the working tree (or merely\ncheck that it would apply cleanly to both if --check is in effect).\nNote that --index expects index entries and working tree copies for\nrelevant paths to be identical (their contents and metadata such as\nfile mode must match), and will raise an error if they are not,\neven if the patch would apply cleanly to both the index and the\nworking tree in isolation.\n",
                    "long": "--index"
                },
                {
                    "name": "--cached",
                    "content": "Apply the patch to just the index, without touching the working\ntree. If --check is in effect, merely check that it would apply\ncleanly to the index entry.\n",
                    "long": "--cached"
                },
                {
                    "name": "--intent-to-add",
                    "content": "When applying the patch only to the working tree, mark new files to\nbe added to the index later (see --intent-to-add option in git-\nadd(1)). This option is ignored unless running in a Git repository\nand --index is not specified. Note that --index could be implied by\nother options such as --cached or --3way.\n\n-3, --3way\nAttempt 3-way merge if the patch records the identity of blobs it\nis supposed to apply to and we have those blobs available locally,\npossibly leaving the conflict markers in the files in the working\ntree for the user to resolve. This option implies the --index\noption unless the --cached option is used, and is incompatible with\nthe --reject option. When used with the --cached option, any\nconflicts are left at higher stages in the cache.\n\n--build-fake-ancestor=<file>\nNewer git diff output has embedded index information for each blob\nto help identify the original version that the patch applies to.\nWhen this flag is given, and if the original versions of the blobs\nare available locally, builds a temporary index containing those\nblobs.\n\nWhen a pure mode change is encountered (which has no index\ninformation), the information is read from the current index\ninstead.\n",
                    "long": "--intent-to-add"
                },
                {
                    "name": "-R, --reverse",
                    "content": "Apply the patch in reverse.\n",
                    "flag": "-R",
                    "long": "--reverse"
                },
                {
                    "name": "--reject",
                    "content": "For atomicity, git apply by default fails the whole patch and does\nnot touch the working tree when some of the hunks do not apply.\nThis option makes it apply the parts of the patch that are\napplicable, and leave the rejected hunks in corresponding *.rej\nfiles.\n",
                    "long": "--reject"
                },
                {
                    "name": "-z",
                    "content": "When --numstat has been given, do not munge pathnames, but use a\nNUL-terminated machine-readable format.\n\nWithout this option, pathnames with \"unusual\" characters are quoted\nas explained for the configuration variable core.quotePath (see\ngit-config(1)).\n",
                    "flag": "-z"
                },
                {
                    "name": "-p<n>",
                    "content": "Remove <n> leading path components (separated by slashes) from\ntraditional diff paths. E.g., with -p2, a patch against a/dir/file\nwill be applied directly to file. The default is 1.\n"
                },
                {
                    "name": "-C<n>",
                    "content": "Ensure at least <n> lines of surrounding context match before and\nafter each change. When fewer lines of surrounding context exist\nthey all must match. By default no context is ever ignored.\n"
                },
                {
                    "name": "--unidiff-zero",
                    "content": "By default, git apply expects that the patch being applied is a\nunified diff with at least one line of context. This provides good\nsafety measures, but breaks down when applying a diff generated\nwith --unified=0. To bypass these checks use --unidiff-zero.\n\nNote, for the reasons stated above usage of context-free patches is\ndiscouraged.\n",
                    "long": "--unidiff-zero"
                },
                {
                    "name": "--apply",
                    "content": "If you use any of the options marked \"Turns off apply\" above, git\napply reads and outputs the requested information without actually\napplying the patch. Give this flag after those flags to also apply\nthe patch.\n",
                    "long": "--apply"
                },
                {
                    "name": "--no-add",
                    "content": "When applying a patch, ignore additions made by the patch. This can\nbe used to extract the common part between two files by first\nrunning diff on them and applying the result with this option,\nwhich would apply the deletion part but not the addition part.\n",
                    "long": "--no-add"
                },
                {
                    "name": "--allow-binary-replacement, --binary",
                    "content": "Historically we did not allow binary patch applied without an\nexplicit permission from the user, and this flag was the way to do\nso. Currently we always allow binary patch application, so this is\na no-op.\n\n--exclude=<path-pattern>\nDon't apply changes to files matching the given path pattern. This\ncan be useful when importing patchsets, where you want to exclude\ncertain files or directories.\n\n--include=<path-pattern>\nApply changes to files matching the given path pattern. This can be\nuseful when importing patchsets, where you want to include certain\nfiles or directories.\n\nWhen --exclude and --include patterns are used, they are examined\nin the order they appear on the command line, and the first match\ndetermines if a patch to each path is used. A patch to a path that\ndoes not match any include/exclude pattern is used by default if\nthere is no include pattern on the command line, and ignored if\nthere is any include pattern.\n",
                    "long": "--binary"
                },
                {
                    "name": "--ignore-space-change, --ignore-whitespace",
                    "content": "When applying a patch, ignore changes in whitespace in context\nlines if necessary. Context lines will preserve their whitespace,\nand they will not undergo whitespace fixing regardless of the value\nof the --whitespace option. New lines will still be fixed, though.\n\n--whitespace=<action>\nWhen applying a patch, detect a new or modified line that has\nwhitespace errors. What are considered whitespace errors is\ncontrolled by core.whitespace configuration. By default, trailing\nwhitespaces (including lines that solely consist of whitespaces)\nand a space character that is immediately followed by a tab\ncharacter inside the initial indent of the line are considered\nwhitespace errors.\n\nBy default, the command outputs warning messages but applies the\npatch. When git-apply is used for statistics and not applying a\npatch, it defaults to nowarn.\n\nYou can use different <action> values to control this behavior:\n\no   nowarn turns off the trailing whitespace warning.\n\no   warn outputs warnings for a few such errors, but applies the\npatch as-is (default).\n\no   fix outputs warnings for a few such errors, and applies the\npatch after fixing them (strip is a synonym --- the tool used\nto consider only trailing whitespace characters as errors, and\nthe fix involved stripping them, but modern Gits do more).\n\no   error outputs warnings for a few such errors, and refuses to\napply the patch.\n\no   error-all is similar to error but shows all errors.\n",
                    "long": "--ignore-whitespace"
                },
                {
                    "name": "--inaccurate-eof",
                    "content": "Under certain circumstances, some versions of diff do not correctly\ndetect a missing new-line at the end of the file. As a result,\npatches created by such diff programs do not record incomplete\nlines correctly. This option adds support for applying such patches\nby working around this bug.\n",
                    "long": "--inaccurate-eof"
                },
                {
                    "name": "-v, --verbose",
                    "content": "Report progress to stderr. By default, only a message about the\ncurrent patch being applied will be printed. This option will cause\nadditional information to be reported.\n",
                    "flag": "-v",
                    "long": "--verbose"
                },
                {
                    "name": "--recount",
                    "content": "Do not trust the line counts in the hunk headers, but infer them by\ninspecting the patch (e.g. after editing the patch without\nadjusting the hunk headers appropriately).\n\n--directory=<root>\nPrepend <root> to all filenames. If a \"-p\" argument was also\npassed, it is applied before prepending the new root.\n\nFor example, a patch that talks about updating a/git-gui.sh to\nb/git-gui.sh can be applied to the file in the working tree\nmodules/git-gui/git-gui.sh by running git apply\n--directory=modules/git-gui.\n",
                    "long": "--recount"
                },
                {
                    "name": "--unsafe-paths",
                    "content": "By default, a patch that affects outside the working area (either a\nGit controlled working tree, or the current working directory when\n\"git apply\" is used as a replacement of GNU patch) is rejected as a\nmistake (or a mischief).\n\nWhen git apply is used as a \"better GNU patch\", the user can pass\nthe --unsafe-paths option to override this safety check. This\noption has no effect when --index or --cached is in use.\n",
                    "long": "--unsafe-paths"
                }
            ]
        },
        "CONFIGURATION": {
            "content": "apply.ignoreWhitespace\nSet to change if you want changes in whitespace to be ignored by\ndefault. Set to one of: no, none, never, false if you want changes\nin whitespace to be significant.\n\napply.whitespace\nWhen no --whitespace flag is given from the command line, this\nconfiguration item is used as the default.\n",
            "subsections": []
        },
        "SUBMODULES": {
            "content": "If the patch contains any changes to submodules then git apply treats\nthese changes as follows.\n\nIf --index is specified (explicitly or implicitly), then the submodule\ncommits must match the index exactly for the patch to apply. If any of\nthe submodules are checked-out, then these check-outs are completely\nignored, i.e., they are not required to be up to date or clean and they\nare not updated.\n\nIf --index is not specified, then the submodule commits in the patch\nare ignored and only the absence or presence of the corresponding\nsubdirectory is checked and (if possible) updated.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "git-am(1).\n",
            "subsections": []
        },
        "GIT": {
            "content": "Part of the git(1) suite\n\nGit 2.34.1                        02/26/2026                      GIT-APPLY(1)",
            "subsections": []
        }
    },
    "summary": "git-apply - Apply a patch to files and/or to the index",
    "flags": [
        {
            "flag": "",
            "long": "--stat",
            "arg": null,
            "description": "Instead of applying the patch, output diffstat for the input. Turns off \"apply\"."
        },
        {
            "flag": "",
            "long": "--numstat",
            "arg": null,
            "description": "Similar to --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\"."
        },
        {
            "flag": "",
            "long": "--summary",
            "arg": null,
            "description": "Instead of applying the patch, output a condensed summary of information obtained from git diff extended headers, such as creations, renames and mode changes. Turns off \"apply\"."
        },
        {
            "flag": "",
            "long": "--check",
            "arg": null,
            "description": "Instead of applying the patch, see if the patch is applicable to the current working tree and/or the index file and detects errors. Turns off \"apply\"."
        },
        {
            "flag": "",
            "long": "--index",
            "arg": null,
            "description": "Apply the patch to both the index and the working tree (or merely check that it would apply cleanly to both if --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."
        },
        {
            "flag": "",
            "long": "--cached",
            "arg": null,
            "description": "Apply the patch to just the index, without touching the working tree. If --check is in effect, merely check that it would apply cleanly to the index entry."
        },
        {
            "flag": "",
            "long": "--intent-to-add",
            "arg": null,
            "description": "When applying the patch only to the working tree, mark new files to be added to the index later (see --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. -3, --3way Attempt 3-way merge if the patch records the identity of blobs it is supposed to apply to and we have those blobs available locally, possibly leaving the conflict markers in the files in the working tree for the user to resolve. This option implies the --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. --build-fake-ancestor=<file> Newer git diff output has embedded index information for each blob to help identify the original version that the patch applies to. When this flag is given, and if the original versions of the blobs are available locally, builds a temporary index containing those blobs. When a pure mode change is encountered (which has no index information), the information is read from the current index instead."
        },
        {
            "flag": "-R",
            "long": "--reverse",
            "arg": null,
            "description": "Apply the patch in reverse."
        },
        {
            "flag": "",
            "long": "--reject",
            "arg": null,
            "description": "For atomicity, git apply by default fails the whole patch and does not touch the working tree when some of the hunks do not apply. This option makes it apply the parts of the patch that are applicable, and leave the rejected hunks in corresponding *.rej files."
        },
        {
            "flag": "-z",
            "long": null,
            "arg": null,
            "description": "When --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))."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "Remove <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."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "Ensure at least <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."
        },
        {
            "flag": "",
            "long": "--unidiff-zero",
            "arg": null,
            "description": "By default, git apply expects that the patch being applied is a unified diff with at least one line of context. This provides good safety measures, but breaks down when applying a diff generated with --unified=0. To bypass these checks use --unidiff-zero. Note, for the reasons stated above usage of context-free patches is discouraged."
        },
        {
            "flag": "",
            "long": "--apply",
            "arg": null,
            "description": "If you use any of the options marked \"Turns off apply\" above, git apply reads and outputs the requested information without actually applying the patch. Give this flag after those flags to also apply the patch."
        },
        {
            "flag": "",
            "long": "--no-add",
            "arg": null,
            "description": "When applying a patch, ignore additions made by the patch. This can be used to extract the common part between two files by first running diff on them and applying the result with this option, which would apply the deletion part but not the addition part."
        },
        {
            "flag": "",
            "long": "--binary",
            "arg": null,
            "description": "Historically we did not allow binary patch applied without an explicit permission from the user, and this flag was the way to do so. Currently we always allow binary patch application, so this is a no-op. --exclude=<path-pattern> Don't apply changes to files matching the given path pattern. This can be useful when importing patchsets, where you want to exclude certain files or directories. --include=<path-pattern> Apply changes to files matching the given path pattern. This can be useful when importing patchsets, where you want to include certain files or directories. When --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."
        },
        {
            "flag": "",
            "long": "--ignore-whitespace",
            "arg": null,
            "description": "When applying a patch, ignore changes in whitespace in context lines if necessary. Context lines will preserve their whitespace, and they will not undergo whitespace fixing regardless of the value of the --whitespace option. New lines will still be fixed, though. --whitespace=<action> When applying a patch, detect a new or modified line that has whitespace errors. What are considered whitespace errors is controlled by 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: o nowarn turns off the trailing whitespace warning. o warn outputs warnings for a few such errors, but applies the patch as-is (default). o fix outputs warnings for a few such errors, and applies the patch after fixing them (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). o error outputs warnings for a few such errors, and refuses to apply the patch. o error-all is similar to error but shows all errors."
        },
        {
            "flag": "",
            "long": "--inaccurate-eof",
            "arg": null,
            "description": "Under certain circumstances, some versions of diff do not correctly detect a missing new-line at the end of the file. As a result, patches created by such diff programs do not record incomplete lines correctly. This option adds support for applying such patches by working around this bug."
        },
        {
            "flag": "-v",
            "long": "--verbose",
            "arg": null,
            "description": "Report progress to stderr. By default, only a message about the current patch being applied will be printed. This option will cause additional information to be reported."
        },
        {
            "flag": "",
            "long": "--recount",
            "arg": null,
            "description": "Do not trust the line counts in the hunk headers, but infer them by inspecting the patch (e.g. after editing the patch without adjusting the hunk headers appropriately). --directory=<root> Prepend <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."
        },
        {
            "flag": "",
            "long": "--unsafe-paths",
            "arg": null,
            "description": "By default, a patch that affects outside the working area (either a Git controlled working tree, or the current working directory when \"git apply\" is used as a replacement of GNU patch) is rejected as a mistake (or a mischief). When git apply is used as a \"better GNU patch\", the user can pass the --unsafe-paths option to override this safety check. This option has no effect when --index or --cached is in use."
        }
    ],
    "examples": [],
    "see_also": [
        {
            "name": "git-am",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/git-am/1/json"
        }
    ]
}