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.tsChanged: src/config.ts
1export const config = {1export const config = {
2Removed apiUrl: "https://api.example.com",2Added apiUrl: "https://api.example.com/v2",
3Removed retries: 3,3Added 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.

Changes in the example
1
2 lines removed, 2 added
Lines
5 and 5
Original and changed text
Category
Diff format
Text diff guides

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:

change.patch
--- 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:

Shell
git apply --check change.patch
git apply change.patch

The 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:

Shell
patch -p1 < change.patch

The -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

Frequently asked questions

Does git apply create a commit?
No. It only changes the files in your working folder. Review the change with 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?
The lines in the patch don't match your file. Either the path points at a different file, or the file changed since the diff was made. Make the diff again from your current copy of the file and try once more.
What does patch -p1 mean?
It tells patch to remove the first folder from every path in the diff. The tool writes paths as a/name and b/name, so -p1 leaves just the name, which is the file patch should change.