TToolbox
⑃

Diff Patch

Generate Git-style unified diffs

About this tool

Git Unified Diff / Patch files (standardized by POSIX.1 and Git vcs) are the universal medium for open-source code reviews, air-gapped bug delivery, Linux kernel email patch workflows, and emergency hotfix reversions. By indexing contextual lines with `+` additions, `-` deletions, and `@@ -l,s +l,s @@` hunk coordinates, patches describe code changes with minimal byte footprints. This tool generates, validates, and inverts Git-compliant patch files entirely in local browser memory.

How to Use This Tool (Step-by-Step)

1

Paste Original & Modified Files

Paste your baseline source code into the left Original pane and updated code into the right Modified pane.

2

Configure Context Lines & Headers

Configure surrounding context lines (defaults to 3) and set virtual file path headers (e.g., `a/index.ts` to `b/index.ts`).

3

Copy or Download .patch File

The Myers diff engine compiles a standard Unified Diff hunk block. Copy the snippet or download a `.patch` file for `git apply`.

Strict Git Unified Diff Hunk Syntax

Computes exact `@@ -l,s +l,s @@` hunk range coordinates, ensuring 100% compatibility with native `git apply` and `patch` commands.

Instant Reverse Patch Inversion

Inverts additions and deletions with a single toggle, generating reverse rollout patches for instant production hotfix rollbacks.

100% Client-Side Air-Gapped

Diffing algorithms execute exclusively within local browser memory. Proprietary intellectual code bases are never uploaded.

Patch Creation Best Practices & CRLF Gotchas

  • •Normalize line endings (LF vs CRLF): Windows ` ` endings fail against Linux repositories. Ensure patch files use uniform Unix ` ` (LF).
  • •Never drop context lines to zero: Retaining 3 context lines provides necessary fuzzy anchoring for git tools to locate target line blocks.
  • •Avoid 'No newline at end of file' warnings: Ensure both source files end with standard line returns to keep patch hunks clean.

Code & Transformation Examples

Standard Git Unified Diff Anatomy Benchmark

Valid Git Patch Syntax
--- a/src/config.ts
+++ b/src/config.ts
@@ -10,4 +10,5 @@
 const port = 3000;
-const debug = false;
+const debug = true;
+const timeout = 5000;
 export default config;
Syntax Deconstruction
• --- 与 +++ : 指示被对比的旧文件路径与新文件路径
• @@ -10,4 +10,5 @@ : 坐标定位头 (原文件从第10行起占4行,新文件从第10行起占5行)
• 空格开头行 : 未变动的上下文对照行 (保证 patch 能被精准定位)
• - 减号开头  : 本次被删除的旧代码行
• + 加号开头  : 本次被新增的新代码行

* Leading space context lines allow the patch utility to anchor modifications accurately despite line drifts.

Terminal Git Apply Commands Cheat Sheet

Terminal Commands
# 1. 预先检查补丁是否能干净应用 (不修改实际文件)
git apply --check fix.patch

# 2. 正式应用补丁并修改工作区
git apply fix.patch

# 3. 逆向回滚撤销该补丁
git apply -R fix.patch
Handling Conflicts
若原文件有微小变动导致冲突,追加 --reject 参数(git apply --reject fix.patch),能将未能合入的冲突行单独导出为 .rej 文件人工核对!

* Testing with `git apply --check` guarantees safe, collision-free patch ingestion on production servers.

Frequently Asked Questions

Q: Why does the Linux kernel still rely on email patches over Pull Requests?

A: Patches are pure text reviewable in offline email clients and terminals, decoupling kernel development from proprietary single-vendor cloud platforms.

Q: What is the difference between git diff and git format-patch?

A: Basic diffs capture raw code changes. `git format-patch` encapsulates author metadata, commit messages, and PGP signatures for `git am` ingestion.

Q: How does the tool perform on massive multi-thousand line files?

A: Optimized Myers O(ND) diff pipelines process tens of thousands of lines in dozens of milliseconds with minimal memory footprint.

Related Tools

All processing happens locally in your browser. Your data is never uploaded to a server.
/