How to apply a unified diff with git apply or patch
By TechCompare · Updated · How we check
Set File name in the patch to the file's path, copy the diff and save it as change.patch. Run `git apply --check change.patch`, then `git apply change.patch`. Without git, use `patch -p1 < change.patch`.
The example
1 change: 2 lines removed, 2 added
| Original: src/config.ts | Changed: src/config.ts | ||||
|---|---|---|---|---|---|
| 1 | export const config = { | 1 | export const config = { | ||
| 2 | Removed | apiUrl: "https://api.example.com", | 2 | Added | apiUrl: "https://api.example.com/v2", |
| 3 | Removed | retries: | 3 | Added | retries: 5, |
| 4 | timeoutMs: 5000, | 4 | timeoutMs: 5000, | ||
| 5 | }; | 5 | }; | ||
A unified diff isn't only for reading. Save it as a file and git apply or patch can make the same change to another copy of the file, on another machine or in another repository. This page walks through it with a small patch made by the Text Diff Checker.
How this is calculated
The example changes two settings in a TypeScript config file. The file sits at src/config.ts in the project, so that's the name to put in File name in the patch. The tool then writes this:
--- a/src/config.ts
+++ b/src/config.ts
@@ -1,5 +1,5 @@
export const config = {
- apiUrl: "https://api.example.com",
- retries: 3,
+ apiUrl: "https://api.example.com/v2",
+ retries: 5,
timeoutMs: 5000,
};Save it in the root folder of the repository, the folder that holds src, and run these commands there:
git apply --check change.patch
git apply change.patchThe first command only tests the patch. It prints nothing when the patch fits and an error when it doesn't, without touching your files. The second one applies it. git apply changes the files in your working folder and doesn't stage or commit anything, so review the result with git diff and commit it as usual.
Without git, the classic patch tool does the same job:
patch -p1 < change.patchThe -p1 option strips the first folder from each path, which turns a/src/config.ts into src/config.ts. Add --dry-run to test it first.
When a patch doesn't apply, it's usually one of these:
- The path is wrong. The name after a/ and b/ has to be the file's path from the folder you run the command in, which for git is the root of the repository.
- The file changed. Git checks that the context lines and the removed lines match your copy exactly. If someone edited those lines since the diff was made, make the diff again from the current file.
- The spacing is different. A patch copied with Ignore whitespace on keeps the original spacing in its context lines, so it only fits the original text.
Verdict
Always run git apply --check or patch --dry-run first. It takes a second and tells you whether the patch fits before anything changes. If it fails, check the path first, then whether the file changed since the diff was made. A patch only fits the version of the file it was made from.
More Text Diff scenarios
Related guides
Frequently asked questions
Does git apply create a commit?
git diff, then stage and commit it yourself. git am is the command that turns emailed patches into commits, and it needs commit details that this tool's patches don't include.Why does git apply say the patch does not apply?
What does patch -p1 mean?
Related tools
JSON Formatter
Validate, format, and minify JSON. Browse it as a tree, run JSONPath queries, compare two documents, or validate against a JSON Schema.
Use toolCharacter Counter
Count characters, words, sentences, paragraphs, and lines. Check writing limits and count specific characters or phrases.
Use toolText Encoding Converter
Convert text to Base64, binary, hex, URL encoding and HTML entities, and back again.
Use tool