How to patch only a particular hunk from a diff

Viewed 7438

Is there a way to apply a single hunk from a diff to a file? For example, say I do a diff from file A and B, and that produces three chunks of differences, each denoted with something like...

@@ -971,30 +977,28 @@

...(in the case of unified diffs). I'd then want to be able to feed that diff into stdin, and ask patch to only apply hunk N.

The manual method would be to cut-and-paste the interesting hunks, but I'm not after that kind of a solution.

3 Answers

Some of the GUI diff/patch tools have a way to select blocks or even individual lines. I know TortoiseDiff from TortoiseSVN can work this way. I think I've seen windiff do it, but it's been a while since I had to use it.

As far as command line tools, I have not seen anything that will do what you are asking.

Emacs can not only edit diffs intelligently (including the ever-useful split-hunk operation), but can apply them one hunk at a time itself (avoiding the need to switch from the editor to run patch).

Related