Patch collaboration quick reference
# Generate patches from the last 3 commits
git format-patch -3 -o /tmp/patches/
# Generate a single commit as a patch
git format-patch -1 <sha> --stdout > change.patch
# Include a cover letter for a series
git format-patch main..feature --cover-letter -o /tmp/patches/
# Apply a series with three-way fallback
git am --3way /tmp/patches/*.patch
# Apply a single patch without committing
git am --no-commit change.patch
# Resolve a conflict and continue (no editor prompt)
git add . && git am --resolved
# Skip a failing patch and continue the series
git am --skip
# Abort the entire am session
git am --abort
# Dry-run before applying a raw diff
git apply --check --3way change.diff
# Apply a raw diff to working tree only
git apply change.diff
# Apply a raw diff to index and working tree
git apply --index change.diff
# Reverse a previously applied patch
git apply -R change.diff
# Show what a patch claims to change
git apply --stat change.diff
# Verify round-trip losslessness
git format-patch -1 HEAD --stdout > verify.patch
git reset --hard HEAD~1
git am verify.patch
git diff HEAD^ HEAD --stat
Key flags
| Flag |
When to use |
--3way |
Patch context drifted from the target tree. Always use in CI. |
--signoff |
Append Signed-off-by to certify authorship. |
--binary |
Patch range may include binary files. |
--stdout |
Emit single stream for pipe or artifact storage. |
--base=<sha> |
Record the merge base so the consumer can find common ancestors. |
--resolved |
Continue after conflict without editor prompt. |
--no-commit |
Inspect the patch effect before committing. |
--check |
Test that a diff applies without touching the tree. |
DevOps patterns
# CI gate: does the patch apply cleanly?
if ! git apply --check --3way /artifacts/change.diff; then
echo "patch does not apply to this tree"
exit 1
fi
# CI apply with authorship preserved
git am --3way --signoff /artifacts/patches/*.patch
# Backport from main to release branch
cd /repos/target
git checkout release/v2.3
git am --3way /tmp/backport/0001-*.patch
# Rebase a patch series locally
git am /tmp/patches/*.patch
git rebase -i <base>
git format-patch <base> -o /tmp/patches-v2/