Working with git format-patch Output
Apply patches generated by git format-patch. Understand the email-style format, multiple commits, and how to extract the diff portion.
Detailed Explanation
Applying git format-patch Patches
git format-patch generates patches in an email-like format that includes commit metadata. The Diff Patch Applier can process the diff portion of these patches.
format-patch Output Structure
From abc123 Mon Sep 17 00:00:00 2001
From: Author Name <author@example.com>
Date: Mon, 1 Jan 2024 12:00:00 +0000
Subject: [PATCH] Fix null pointer in user handler
Description of the change...
---
src/handler.js | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/handler.js b/src/handler.js
index abc1234..def5678 100644
--- a/src/handler.js
+++ b/src/handler.js
@@ -10,7 +10,8 @@
function handleUser(req, res) {
- const user = getUser(req.params.id);
- res.json(user.profile);
+ const user = getUser(req.params.id);
+ if (!user) return res.status(404).json({ error: "Not found" });
+ res.json(user.profile);
}
Extracting the Diff
The Diff Patch Applier's parser is designed to skip non-diff content and find the ---/+++ headers and @@ hunk headers. You can paste the entire format-patch output, and it will extract the diff portion automatically.
Multiple Commits
If you have a patch series (multiple commits), each will have its own diff --git section. The tool processes all diff sections it finds, applying hunks from each in order.
Stat Line
The summary line (1 file changed, 3 insertions(+), 2 deletions(-)) is informational and ignored by the parser. Only the actual diff lines affect the patch application.
Common Issues
- Binary patches —
git format-patchcan include binary diffs that this tool cannot process. These appear asBinary files differ - Rename detection — renamed files show as
rename from/towhich the parser skips - Mode changes —
old mode/new modelines are informational and ignored
Use Case
A contributor emailed you a git format-patch file for a project without a shared Git remote. Paste the patch content to preview and apply the changes to your local copy of the file.