Home > Text Processing & Search > patch

patch: Apply Changes (Patches) to Files

The 'patch' command is used to apply changes to original files using patch files (.patch) generated by the 'diff' command. This tool allows for efficient modification of source code or updating configuration files.

Overview

patch is a utility that reads output files (also known as patch files) created by the diff command and applies changes to original files. These patch files typically record what content has been added, deleted, or modified line by line. The patch command modifies the original files based on this information. Before the advent of version control systems (like CVS, Git, etc.), it was a primary method for sharing code changes.

Key Features

The main features of the patch command are as follows:

  • Uses patch files generated by the diff command.
  • Widely used for updating source code, changing configuration files, etc.
  • Can detect and apply some changes even if the original file does not exactly match the patch file.
  • Also provides a function to revert changes to their original state (reverse).

Main Options

Frequently used patch command options are grouped by purpose.

1) Applying and Reverting Patches

2) Help

Generated command:

Try combining the commands.

Description:

`patch` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn the functionalities of the patch command through various usage examples.

Apply a Patch File

patch -p1 -i my_changes.patch

Applies changes to the original file using 'my_changes.patch'. The `-p1` option ignores the first directory component in the patch file's path.

Revert Changes

patch -p1 -R -i my_changes.patch

Reverts the applied patch content to its original state using the `-R` option. Used to revert files to their pre-patched state.

Apply Patch via Pipe

diff -u original_file.txt new_file.txt | patch -p0 original_file.txt

Directly passes the output of the diff command to the patch command via a pipe (`|`), applying changes without creating a separate patch file.

Preview Patch Application

patch --dry-run -p1 -i my_changes.patch

Uses the `--dry-run` option to preview what changes would occur if the patch were applied.

Installation

patch is included by default in most Linux distributions. If the package is not present, you can install the `patch` or `diffutils` package as follows:

Debian/Ubuntu

sudo apt update && sudo apt install -y patch

RHEL/CentOS/Fedora

sudo dnf install -y patch

Arch Linux

sudo pacman -S --needed patch

Tips & Cautions

Here are some important points to consider when using the patch command.

Tips

  • The unified format generated by `diff -u` is the most common patch format. Using this format generally leads to a higher success rate for patch application.
  • The `-p` option is crucial for ignoring path information in the patch file. You should check the patch file's header to specify the correct level. For example, if the path is `a/src/file.c`, using `-p1` will apply the patch to `src/file.c`.
  • If a patch fails to apply, a file with a `.rej` extension is created. This file contains the rejected parts of the patch and can be referred to for manual correction.
  • It is recommended to create a backup of your original files before applying a patch.

Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands