Skip to content

Git Patch Tutorial

Overview

The git patch command is used to create, apply, and manage patches in Git. A patch file contains a list of changes made to a set of files, which can be used to share changes with others or to apply changes across different branches or repositories.

What is a Git Patch?

A Git patch is a text file that contains a list of changes between two versions of a file or set of files. It includes information about what has been added, removed, or modified. Patches can be used to transfer changes between repositories or branches without using Git's built-in merge or rebase commands.

Creating a Patch

To create a patch file, you can use the git format-patch command. This command generates a patch file from commits in your repository.

Basic Syntax

git format-patch [options] <since-commit>

Example: Create a Patch for the Last Commit

To create a patch for the most recent commit:

git format-patch -1 HEAD

This command generates a patch file named 0001-<commit-message>.patch.

Example: Create a Patch for a Range of Commits

To create patches for the last 3 commits:

git format-patch -3 HEAD

This command generates patch files for the last 3 commits.

Applying a Patch

To apply a patch file, you use the git apply command.

Basic Syntax

git apply <patch-file>

Example: Apply a Patch File

To apply a patch file named 0001-fix-issue.patch:

git apply 0001-fix-issue.patch

Example: Apply a Patch with Reverse Option

To reverse the changes introduced by a patch:

git apply -R 0001-fix-issue.patch

Viewing a Patch

You can view the contents of a patch file using standard text viewing commands.

Example: View a Patch File

To view a patch file using cat:

cat 0001-fix-issue.patch

Or using less for paginated view:

less 0001-fix-issue.patch

Examples

Example 1: Creating and Applying a Patch

  1. Create a Patch for the Last Commit:

    git format-patch -1 HEAD
    
  2. Apply the Patch in Another Repository:

    git apply 0001-<commit-message>.patch
    

Example 2: Creating a Patch for a Specific Range

  1. Create Patches for a Range of Commits:

    git format-patch HEAD~3..HEAD
    
  2. Apply Multiple Patches:

    git am 0001-patch1.patch 0002-patch2.patch
    

Common Use Cases

  • Code Review: Sharing specific changes with reviewers for feedback.
  • Patching Across Repositories: Applying changes from one repository to another without merging.
  • Backup: Saving changes as patches to apply later or on different branches.

Summary

Git patches are a powerful way to manage and share changes in your codebase. By creating and applying patches, you can efficiently transfer changes between repositories or branches and streamline code reviews and integration processes.