---
title: "Unified diff vs diff-match-patch format"
url: https://recursiveai.org/onediff/articles/unified-diff-vs-diff-match-patch/
description: "Choose unified diff for Git and line edits, or diff-match-patch for character edits between apps that support its patch format."
date: 2026-11-28
author: OneDiff team
---

# Unified diff vs diff-match-patch format

Choose unified diff for Git and line edits, or diff-match-patch for character edits between apps that support its patch format.

November 28, 2026 · OneDiff team

[OneDiff](https://recursiveai.org/onediff/) is a free, open-source .NET text diff and document comparison library from Recursive AI, licensed under [MIT or Apache-2.0](https://licenses.nuget.org/MIT%20OR%20Apache-2.0) ([NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff/1.0.1) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp)).

*Install · .NET 10*

```
dotnet add package RecursiveAI.OneDiff --version 1.0.1
```

[Diff Match Patch](https://github.com/google/diff-match-patch) (DMP) is Google’s character-level diff library, ported to many languages.

In a .NET 10 console project, replace `Program.cs` and run `dotnet run`.

*Program.cs*

```
using OneDiff;
 
const string before = "Status\nColor: red.\nDone\n";
const string after  = "Status\nColor: blue.\nDone\n";
var unified = Diff.Unified(before, after, "a/status.txt", "b/status.txt");
var dmp = Diff.PatchText(before, after);
 
Console.WriteLine("Unified:");
Console.Write(unified);
Console.WriteLine("Diff Match Patch:");
Console.Write(dmp);
Console.WriteLine($"Unified round trip: {Diff.ApplyUnified(before, unified).Text == after}");
Console.WriteLine($"DMP round trip: {Diff.ApplyPatchText(before, dmp).Text == after}");
```

*Output · Unified diff*

```
--- a/status.txt
+++ b/status.txt
@@ -1,3 +1,3 @@
 Status
-Color: red.
+Color: blue.
 Done
```

*Output · Diff Match Patch*

```
@@ -11,11 +11,12 @@
 or: 
-red
+blue
 .%0ADo
```

In this example, offsets count characters, and `%0A` is an encoded newline.

*Output · Applied to the original*

```
Unified round trip: True
DMP round trip: True
```

## How to read the two patch formats

In the [GNU unified format](https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html), `---` and `+++` name the old and new files. The hunk header `@@ -1,3 +1,3 @@` gives the starting line and line count on each side. A space marks a context line; `-` and `+` mark the whole line being removed or added. The unchanged lines help identify where the change belongs.

In [DMP patch text](https://github.com/google/diff-match-patch/wiki/Unidiff), there are no file headers. `@@ -11,11 +11,12 @@` describes text offsets and lengths, not lines. Only `red` and `blue` are marked as edits. Apply DMP patches in order: later offsets refer to the text after earlier patches have been applied.

## Choose by what receives the patch

### Send changes for review or apply them with Git

**Unified diff.** File names and line context identify the edit. [Git apply](https://git-scm.com/docs/git-apply) reads patch files and can check whether they apply without changing the file.

### Exchange edits to a long paragraph or single-line value

**DMP.** It [records character edits with encoded text context](https://github.com/google/diff-match-patch/wiki/Unidiff), so changing one word need not replace the full line. Include the document identifier separately.

### Apply to text that may have drifted

**Either, with result checks.** [OneDiff’s unified apply](https://github.com/recursive-ai-oss/onediff-csharp/blob/6443915c4136cc8c5a49fa2543322011665f309e/docs/patches.md#L19-L25) searches for matching lines and can drop outer context lines. Its [DMP apply](https://github.com/recursive-ai-oss/onediff-csharp/blob/6443915c4136cc8c5a49fa2543322011665f309e/docs/patches.md#L41-L43) can match near the expected offset even when the text differs.

The unified output above applied to `status.txt` with Git 2.53.0. The DMP output applied to the original string with Python’s [diff-match-patch 20241021](https://pypi.org/project/diff-match-patch/20241021/). That sample is ASCII. For text with emoji, agree on offset units: OneDiff uses UTF-16 code units by default; set `DmpOffsetUnit.CodePoint` on the patcher when exchanging with the Python port, which counts Unicode code points. See the [pinned offset and encoding rules](https://github.com/recursive-ai-oss/onediff-csharp/blob/6443915c4136cc8c5a49fa2543322011665f309e/docs/patches.md#L31-L39).

If the patch must apply only to the exact base, check a version ID or content hash first. If you allow drift, check every hunk or patch outcome before committing the result. A match near the expected position is not proof that the edit landed in the intended paragraph, especially when text repeats.

See the [OneDiff getting-started guide](https://recursiveai.org/onediff/docs/getting-started/) and the [patch format and apply reference](https://recursiveai.org/onediff/docs/patches/).
