Selectively Applying Hunks from a Patch
Learn how to cherry-pick specific hunks from a multi-hunk patch. Apply only the changes you want while rejecting others.
Detailed Explanation
Cherry-Picking Hunks
Not every change in a patch is wanted. The Diff Patch Applier lets you toggle each hunk on or off after the initial application, similar to git add -p (interactive staging).
Workflow
- Paste the original text and the full patch
- Click Apply — all hunks are applied and shown with checkboxes
- Review each hunk in the hunk list
- Uncheck hunks you want to reject
- The result updates automatically to reflect only accepted hunks
Example Scenario
You receive a patch with 4 hunks:
| Hunk | Change | Decision |
|---|---|---|
| 1 | Fix typo in comment | Accept |
| 2 | Add debug logging | Reject (not for production) |
| 3 | Fix null pointer bug | Accept |
| 4 | Rename variable | Reject (naming convention differs) |
How Selective Application Works
When hunks are toggled off, the engine replays the patch from scratch, applying only the accepted hunks. Line offsets are recalculated based on only the accepted changes, ensuring the result is internally consistent.
Important Consideration
If a rejected hunk is between two accepted hunks that are close together, the line offsets might shift. The tool handles this by recalculating offsets, but in rare cases you may need to manually adjust the result if hunks overlap or are very close.
Comparison with Git
This is similar to:
git add -p— interactively stage hunksgit checkout -p— interactively discard hunksgit stash -p— interactively stash hunks
The difference is that this tool works with any text, not just Git repositories.
Use Case
You downloaded a community patch for a configuration file but only want specific settings changed. Toggle off the unwanted hunks and apply only what you need.