Developer and Text Tools

How to Read and Apply a Git Patch File

Understand unified diffs and git format-patch files: hunks, context lines, renames, and how patches are applied with offsets and fuzz, or reversed.

Saizul Amin
Saizul Amin2 Nov 2026 · 3 min read
How to Read and Apply a Git Patch File

How to Read and Apply a Git Patch File

Patches are the oldest way of sharing code changes, and they are still everywhere: mailing lists for the Linux kernel and many other projects, hotfixes attached to support tickets, CI systems that export a diff. Reading them comfortably and applying them correctly is a useful skill. This guide explains the format and how to work with it, using the free git patch and diff viewer.

The unified diff format

A diff compares two versions of a file. In the unified format, used by git diff and diff -u, each file starts with two header lines, --- a/path for the old version and +++ b/path for the new one. The changes follow in hunks.

Hunks and context

A hunk begins with a line like @@ -12,7 +12,9 @@. The first pair means "starting at line 12 of the old file, 7 lines"; the second pair is the same for the new file. Inside, lines starting with a space are context and exist in both versions, lines starting with a minus were removed, and lines starting with a plus were added. Context lines, usually three before and after each change, are what make a patch robust: they let a tool find the right place even if line numbers changed.

Git's extras

Git adds a diff --git line and extended headers: new file mode, deleted file mode, rename from and rename to, mode changes such as making a script executable, and a similarity index for renamed files. Binary files show "Binary files differ" or an encoded binary patch.

format-patch: a commit in an email

git format-patch wraps each commit in an email-like file with From, Date and Subject headers, the commit message, a summary of changed files and then the diff. A file can contain several commits. Maintainers apply them with git am, which recreates the commits with their original authors and messages.

Reading a patch well

Start with the file list and the line counts: a patch that touches 40 files deserves a different review than one that touches two. Read side by side when changes are edits of existing lines, and look at word-level highlights to see small changes in long lines. Use the unified view for additions and deletions, where side by side wastes space.

How applying works

Applying a patch means finding each hunk's old lines in the target file and replacing them with the new ones. If the file is exactly the version the patch was made from, every hunk lands on its stated line. If someone added lines earlier in the file, the hunk is found a few lines away; tools report this as an offset and it is usually harmless.

Fuzz: useful and dangerous

When context lines near the edge of a hunk have also changed, an exact match fails. Fuzz allows a tool to ignore one or two context lines at the beginning and end. It often rescues a patch made against a slightly different version, but it can also place a change in the wrong spot when code is repetitive. Always review hunks that applied with fuzz.

Reversing a patch

Swapping plus and minus lines turns a patch into its inverse. Applying the reverse patch undoes the change, which is handy for testing whether a patch introduced a bug or for backing out a hotfix.

Rejected hunks

A hunk that cannot be placed is rejected. The usual causes are applying the patch to the wrong version, whitespace differences from an editor that re-indented the file, or overlapping changes. Check the file version first, then try ignoring whitespace, and only then resolve the change by hand.

Limits

Patches describe text changes. They do not carry merge history, and applying one outside Git loses author information unless you use git am. Binary patches need Git itself to apply. A patch that applies cleanly can still break the build, so run the tests afterwards.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages