Git patch Command
In this page:
Creating Patches with Diff
git diff > changes.patch captures the current uncommitted differences as a plain-text patch file, which anyone can apply to their own copy of the same codebase with git apply — a lightweight way to share a fix without needing repository access or a pull request.
Example: Creating Patches with Diff
git diff > changes.patch
Creating Patches with Format-Patch
git format-patch <commit> goes further than a raw diff: it produces one .patch file per commit, each formatted as a complete email-style message including the author, date, and commit message — designed specifically to preserve enough information that the receiving end can recreate the original commit exactly, not just its file changes.
Example: Creating Patches with Format-Patch
git format-patch a1b2c3d
Checking Patch Integrity
git apply --check (or --stat for a summary of what would change) lets you validate a patch against your current working directory before actually applying it, catching a patch that won't cleanly apply due to conflicting local changes without touching any files yet.
Example: Checking Patch Integrity
git apply --check changes.patch
git apply --stat changes.patch
Applying Patches
git apply <patch> applies a plain diff-style patch to your working directory as an unstaged change, updating file contents but not creating any commit — you'd still need to git add and commit the result yourself afterward.
Example: Applying Patches
git apply changes.patch
Applying and Committing Patches
git am <patch> is built specifically for patches from format-patch: it applies the changes AND recreates the original commit, using the author, date, and message embedded in the patch file — effectively transplanting someone else's commit into your repository as if you'd received it by pull.
Example: Applying and Committing Patches
git am 0001-fix-login-bug.patch
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: