Generate a Git patch for a specific commit
Export a commit with format-patch, apply it with git am, or create a raw diff; understand metadata, binary files, and conflict handling.
Use git format-patch -1 COMMIT to export one ordinary commit as an email-format patch. It includes the change, author information, and commit message. Applying it with git am creates a new commit; it does not guarantee the original commit ID because the parent and committer metadata can differ.
Replace COMMIT below with an actual commit ID or reference.
Export one commit or a series
# One selected commit
git format-patch -1 COMMIT
# The most recent three commits
git format-patch -3
# Commits on feature-x that are not reachable from main
git format-patch main..feature-x
# Write one patch into a directory
git format-patch -1 COMMIT -o patches/
The output usually has a name such as 0001-Fix-login-bug.patch. format-patch exports non-merge commits; do not assume it preserves a merge commit's topology. Review which commits the range selects before sending the files.
Apply an email-format patch
On the target branch, with a clean working tree:
git am 0001-Fix-login-bug.patch
Alternatively, request a three-way fallback when the necessary base objects are available:
git am --3way 0001-Fix-login-bug.patch
These are alternative ways to apply the same patch, not consecutive commands. git am retains the patch's author and message while creating a commit in the target repository.
For file changes without creating a commit:
git apply --check 0001-Fix-login-bug.patch
git apply 0001-Fix-login-bug.patch
Review and stage those changes yourself if you later want to commit them.
Choose email format or a raw diff
| Command | Output | Typical consumer |
|---|---|---|
git format-patch -1 COMMIT |
Patch with author and message metadata | git am |
git show --format= --binary COMMIT |
Patch text without the normal commit header | git apply |
git diff --binary A B |
Difference between two snapshots | git apply |
For example:
git show --format= --binary COMMIT > fix.patch
git apply --check fix.patch
Plain git show COMMIT normally includes a commit header, so --format= makes the raw-diff intent explicit. For a merge commit, choose the parent or snapshot comparison deliberately rather than relying on its default combined diff.
FAQ
What if git am encounters a conflict?
If a three-way fallback cannot resolve it, edit the conflicted files, stage the result with git add, and run git am --continue. Use git am --abort to return to the pre-application state. Three-way application requires the patch's base blobs to exist locally; it cannot invent missing context.
How are binary changes handled?
format-patch normally includes applicable binary changes unless configured or requested otherwise, such as with --no-binary. For a raw diff, generate it with git diff --binary or git show --binary. Modern git apply accepts binary patches without an extra --binary option; that option is retained for compatibility.
Can I email the patch to a maintainer?
Yes. Email-based review is the purpose of this format, and git send-email supports sending it through a configured mail service. Follow the project's contribution instructions and inspect the patch for unintended content before sending.
See git-format-patch, git-am, git-apply, and git-show.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan