This command lets you apply a patch file directly to your working directory without committing, making it easy to test or stage manual changes from diffs.
It reads a unified diff from the specified path (or standard input if you use -
), applies each hunk to matching files in the working tree, and leaves the index (staging area) untouched by default. You can control whitespace handling with --whitespace=<mode>
, preview changes with --stat
, or just check patch applicability with --check
.
Common variations include git apply --check <pathname>
to verify a patch without applying it, git apply --cached <pathname>
to stage changes without modifying files, and git apply --reverse <pathname>
to undo a patch. Closely related commands are git am
for applying mailbox-formatted patches and git diff | git apply -
for piping diffs directly; other complementary tools include git format-patch
for generating email-ready patches and git cherry-pick
for applying individual commits.