Reverse Applying a Patch to Undo Changes

Use reverse apply (unapply) to undo a previously applied patch. Recover the original file from a patched version and the diff.

Common Use Cases

Detailed Explanation

Reverse Applying a Patch

Reverse apply swaps the meaning of + and - lines in a patch, effectively undoing the changes. This is equivalent to patch -R on the command line.

When to Use Reverse Apply

  1. Undo a change — you applied a patch and want to go back
  2. Recover original — you have the patched file and the diff, but not the original
  3. Test revert — verify that reverting a patch produces the expected original

Example

Patched file (what you have):

function greet(name, greeting = "Hello") {
  console.log(greeting + ", " + name + "!");
  return true;
}

The patch that was applied:

--- a/greet.js
+++ b/greet.js
@@ -1,3 +1,3 @@
-function greet(name) {
-  console.log("Hello, " + name);
+function greet(name, greeting = "Hello") {
+  console.log(greeting + ", " + name + "!");
   return true;
 }

Result of reverse apply (recovered original):

function greet(name) {
  console.log("Hello, " + name);
  return true;
}

How It Works Internally

When reverse is enabled:

  • Lines starting with + are treated as lines to remove (they should exist in the current file)
  • Lines starting with - are treated as lines to add (they will be inserted)
  • Context lines ( prefix) remain unchanged
  • Hunk line numbers are swapped: oldStart/oldCount uses the + values and vice versa

Verify Round-Trip

A useful validation: applying a patch and then reverse-applying the same patch should produce the original file. If it does not, the patch may have been edited or the original file was different from what the patch expects.

Use Case

A hotfix patch was applied to production but caused a regression. You need to quickly revert the change while keeping the patch for reference and future re-application after the bug is fixed.

Try It — Diff Patch Applier

Open full tool