top of page
  • Linkedin

From Bug Fixes to Features: Everything You Need to Know About Patch Files

  • Writer: Avinash Ghadshi
    Avinash Ghadshi
  • Jun 3
  • 4 min read

Updated: Jul 10


ree

Patch files are a lightweight and powerful way to manage and share code changes, especially in collaborative environments. Whether you're contributing to open-source, managing production hot-fixes, or just sharing updates, patching is a skill worth mastering.


Patch file is a text file which contains difference between two versions of file(s). It contains instructions on what lines to add, remove, or change. patch files can be generated using diff or git diff command in Linux. This is nothing but the change-log of code.


Patch File Line-by-Line Meaning

└─# cat file1.patch
--- file1.txt	2025-05-30 09:00:30.000000000 +0530
+++ file1.txt	2025-05-30 14:33:29.357113107 +0530
@@ -1 +1,2 @@
-This is file1 at level4
\ No newline at end of file
+This is line 1 in file1 at level4
+This is line 2 in file1 at level4
  • --- filename  The original file (before changes).

  • +++ filename  The new file (after changes).

  • @@ ... @@ → Shows the location in the file where the change happens (line numbers and context).

  • \ No newline at end of file → A note from the diff tool saying the last line of the file doesn't end with a newline (\n).

  • - line  This line will be removed from the original file.

  • + line  This line will be added to the file.


Why Are Patches Needed?

Imagine, you're driving to work and suddenly your car hits a sharp object — your tire gets punctured.

Now, instead of replacing the entire tire with a brand new one, you simply patch the hole, pump some air, and continue your journey. You saved time, money, and effort — and your car is back on the road with just a small, targeted fix.


Similarly,

In software world, you have a script with 1000 lines. and you only need to change 5 lines. Rather than sending the entire file, a patch file says “Delete lines 246-250 and add a new 5 lines instead.

This is faster, lighter, and easier to review — just like tailoring only what’s necessary.


Patch files are useful when:

  • You want to share code changes without the full file.

  • Collaborators are on different systems or don’t use version control.

  • You're contributing to an open-source project that requires code review via patches.

  • Applying hot-fixes or quick updates to deployed systems.


How to Create a Patch File

There are two types of patch files.

  1. unified file

    Unified file patch can be created by using diff or git diff command

    1. Using diff

      • diff -u original.txt updated.txt > change.patch

    2. Using git diff

      • git diff > feature.patch Saves all current Git changes into feature.patch git diff file1.go file2.go > changes.patch for specific file

  2. context file

    1. Using diff

      • diff -u original.txt updated.txt > change.patch

    2. Using git diff

      The git diff command by default produces unified diffs, but you can create a context diff (similar to diff -c) using a special option.

      • git diff --diff-algorithm=default --unified=0 > context_style.diff

      This creates a diff that resembles a context diff, but Git doesn't generate exact -c style context diffs like the classic diff -c. Git focuses on unified format which is more compact and widely supported (e.g., by git apply, patch, etc.).

Unified Patch (Left) & Context Patch (Right)
Unified Patch (Left) & Context Patch (Right)

How to Apply a Patch File

To apply patch file, first you need to install patch command in your system.

To install patch command on RHEL / Rocky

dnf install patch

To install patch command on Ubuntu / Debian

apt-get install patch

Syntax

patch [OPTIONS] [ORIGINAL-FILE]

Basic Usage

patch < file.patch

This applies the patch to the file(s) mentioned in file.patch in the current directory.

If the patch was made with full file paths, you may not need to specify a file.


Most Useful Patch Options

Option

Description

-pN

Strip N leading components from file paths in the patch. Most common: -p1.

--dry-run

Test the patch without actually applying it (great for checking first).

-R or --reverse

Reverses a patch (undoes changes made by a patch).

--backup

Make backup copies of original files (adds .orig by default).

-i file.patch

Specify the patch file explicitly.

--verbose

Show detailed output of patching process.

--directory=DIR

Apply patch from inside the specified directory.

--strip=N

Same as -pN, used in GNU systems.

Apply a patch normally

patch -p1 < fix.patch

Apply a patch from a different directory

patch -p1 -d /path/to/project < fix.patch

Apply and create backups of original files

patch --backup -p1 < fix.patch
You’ll get files like file.c.orig.

Test patch before applying

patch --dry-run -p1 < fix.patch
No files are changed — useful for safety checks.

Reverse a patch (undo it)

patch -R -p1 < fix.patch

Apply patch to a single specific file

patch file1.txt < fix.patch
Useful if the patch only modifies one file and doesn't include path info.

How to Choose -p Value?

Look at the paths in your patch:

--- a/src/utils/logger.go
+++ b/src/utils/logger.go
  • -p1 → removes the first path component (a/ or b/).

  • -p2 → removes a/src/, leaving utils/logger.go.


So use:

patch -p1 < patch.diff
if you're in the project root.

Example

  • Consider below directory structure

Directory Structure
Directory Structure
  • below is the content of file1.txt in level3 directory

contents of file1.txt from level3 directory
contents of file1.txt from level3 directory
  • Let's create copy file1.txt in /tmp and create patch file at level3

    diff -u level1/level2/level3/file1.txt /tmp/file1.org.txt > level1/level2/level3/file1.patch
    diff -u level1/level2/level3/file1.txt /tmp/file1.org.txt > level1/level2/level3/file1.patch
  • Let's apply patch file with dry run

    patch --dry-run --verbose file1.txt < file1.patch
    patch --dry-run --verbose file1.txt < file1.patch
  • Applying patch with level

    patch --dry-run --verbose -p0 < file1.patch
    patch --dry-run --verbose -p0 < file1.patch
  • In this, we have not mentioned file to patch. Hence it is picking 2 files from from patch headers and asking user which file to patch.

  • let's do little modification to patch file and apply patch again

    Modified patch file
    Modified patch file
    patch --dry-run --verbose -p0 < file1.patch
    patch --dry-run --verbose -p0 < file1.patch
  • We have modified filename to exact filename and hit same patch command again. It works!

  • Let's move modified patch file to level1


    content list under folder level1
    content list under folder level1
  • If we hit patch --dry-run --verbose -p0 < file1.patch command our patch will failed and original file present in different folder.

  • let's use new option directory to specify original file folder

    patch --dry-run --verbose --directory level2/level3/ -p0 < file1.patch
    patch --dry-run --verbose --directory level2/level3/ -p0 < file1.patch

    Let's apply reverse patch

  • A reverse patch is used to undo changes made by a previous patch. It's essentially a rollback mechanism for file modifications — particularly useful in development, testing, and deployment workflows.


  • contents of file1.txt
    contents of file1.txt
    patch --verbose -R --directory level2/level3/ -p0 < file1.patch
    patch --verbose -R --directory level2/level3/ -p0 < file1.patch

Common Mistakes

Problem

Reason / Fix

patch: **** Can't find file

You're in the wrong directory, or -p is wrong.

Permission denied

Run with correct permissions or use sudo.

Patch applied but nothing changed

Already applied, or reversed incorrectly.


Comments


bottom of page