The oldest and most universal way to hand your changes to someone else is a patch file. These days most changes travel through Git, but there are still times when you need a patch.
- When you keep only your own changes to IP received from a vendor or to open-source code
- When you move changes between isolated networks whose Git repositories are not connected
- When you get a review by pasting the changes directly into an email or issue tracker
This post covers making patch files with diff, applying them with patch, and the -p option that always confuses people.
Making a patch file: diff -urN
diff -urN <original> <modified> > changes.patch
| Option | Meaning |
|---|---|
-u |
Unified format. Shows context before and after the changed lines (the de facto standard) |
-r |
Compares directories recursively |
-N |
Treats a file present on only one side as compared with an empty file → newly added and deleted files are included in the patch |
A single file
diff -u config_old.txt config_new.txt > config.patch
A whole directory
diff -urN ip_v1.0/ ip_v1.0_fixed/ > ip_fix.patch
Reading the unified format
--- ip_v1.0/rtl/fifo.sv 2026-11-20 10:00:00
+++ ip_v1.0_fixed/rtl/fifo.sv 2026-11-27 15:30:00
@@ -42,7 +42,7 @@ module fifo #(
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n)
- wr_ptr <= 0;
+ wr_ptr <= '0;
else if (wr_en && !full)
wr_ptr <= wr_ptr + 1;
end
---is the original file,+++the modified one@@ -42,7 +42,7 @@: 7 lines starting at line 42 of the original became 7 lines starting at line 42 of the modified file- Lines starting with
-are deleted, lines starting with+are added, and lines starting with a space are context
Applying a patch
cd <directory to apply to>
patch -p<number> < changes.patch
The confusing -p option

-p is how many leading directories to strip from the paths written in the patch file. What remains must match relative to the current directory for the patch to apply.If the patch file says a/myproject/rtl/fifo.sv:
| Option | Path after stripping | Where to run it |
|---|---|---|
-p0 |
a/myproject/rtl/fifo.sv |
Where you can see a/ |
-p1 |
myproject/rtl/fifo.sv |
Where you can see myproject/ |
-p2 |
rtl/fifo.sv |
Inside myproject/ |
For a patch made with diff -urN ip_v1.0/ ip_v1.0_fixed/ above, the path is ip_v1.0/rtl/fifo.sv, so the usual way is to apply it with -p1 inside the original directory.
cd ip_v1.0
patch -p1 < ../ip_fix.patch
Trying it before applying
patch -p1 --dry-run < ../ip_fix.patch # check the result without changing anything
If the patch was already applied, or parts do not match because the original differs, you find out here first. Parts (hunks) that do not match are left in .rej files when you apply for real, so be sure to check them.
Reverting
patch -p1 -R < ../ip_fix.patch # -R: apply in reverse (back to the original)
How does this relate to Git?
The output of git diff is the same unified format, with a/ and b/ in front of the paths. That is why patches made by Git usually apply with patch -p1.
| Situation | Command |
|---|---|
| Work in progress as a patch | git diff > work.patch |
| Only staged changes | git diff --cached > staged.patch |
| Apply a patch (inside a Git repository) | git apply work.patch (test: git apply --check) |
| Commits as mail-format patches (with author and message) | git format-patch -3 (last 3 commits) |
| Apply mail-format patches as commits | git am 0001-*.patch |
Inside a Git repository, git apply and git am handle new files, deletions and permission changes more accurately. Where there is no Git, or in directories not managed by Git, diff/patch is still the simplest way.
Summary
- Create:
diff -urN original modified > x.patch(-Nincludes new files) - Apply:
patch -p1 < x.patchin the target directory, with--dry-runfirst -p<N>strips N leading directories from the paths in the patch- Revert:
patch -R; check failed parts in.rejfiles - Inside Git:
git diff/git apply/git format-patch/git am
Further reading
- Git basics: from installation to branches, conflicts and merge requests
- Practical Git commands and tips: stash, add -p, insteadOf
- GNU diffutils manual
- Git – git-apply, git-format-patch
Git (5 posts)
- Git Basics: From Installation to Branches, Conflicts and Merge Requests
- Practical Git Commands and Tips: stash, add -p, insteadOf
- Tidy Commit Messages with Gitmoji: the 20 Most Used
- Managing Large Binaries with Git LFS
- Exchanging Changes with diff and patch, Including the -p Option Reading now
Leave a Reply