Patching

git apply

Description

It is used to apply a patch to files and/or to the index. Patches are typically created using the git diff or git format-patch commands and can represent changes between different states of the repository. This command allows to take these patches and apply them to your working directory or staging area.

Usage

git apply [<options>] [<patch>...]

Options

--check: Check if the patch can be applied cleanly without actually applying it.

--index: Apply the patch to the index (staging area) as well as the working directory.

--reverse: Apply the patch in reverse (undo the changes).

--whitespace=<action>: Handle whitespace errors; <action> can be nowarn, warn, fix, or error

--reject: Leave the rejected hunks in corresponding .rej files instead of applying them partially.

--apply: Explicitly apply the patch (default action).

git apply --check patchfile.patch
git apply --index patchfile.patch
git apply --reverse patchfile.patch
git apply --whitespace=fix patchfile.patch
git apply --reject patchfile.patch
git apply --apply patchfile.patch

What It Does

  1. Applies Patches: Reads a patch file and applies the changes it contains to the working directory or index.

  2. Supports Various Options: Allows for customization of how the patch is applied, such as applying it in reverse or with fuzz tolerance.

Common Use Cases

-- Apply a Patch to the Working Directory
-- Applies the changes in patchfile.patch to the files in the working directory
git apply patchfile.patch

-- Apply a Patch with Fuzz Tolerance
-- Applies the patch even if there are minor differences (fuzz tolerance) and fixes whitespace errors.
git apply --apply --whitespace=fix patchfile.patch

-- Dry Run
-- Checks if the patch can be applied cleanly without actually applying it.
git apply --check patchfile.patch

Example Workflow

-- Create a patch file using git diff or git format-patch
-- This creates a patch file with the current differences in the working directory.
git diff > my_changes.patch

-- Apply the Patch
-- Applies the changes from my_changes.patch to your working directory
git apply my_changes.patch

-- Check Before Applying
-- checks if the patch can be applied cleanly without actually applying it
git apply --check my_changes.patch

-- Fix Whitespace Errors
-- applies the patch and fixes any whitespace errors encountered
git apply --whitespace=fix my_changes.patch

Example Output

When running git apply patchfile.patch. This indicates that the patch could not be applied cleanly to file.c at line 10.

git am

Description

It is used to apply patches from email files. It stands for "apply mailbox" and is typically used to apply patches generated by git format-patch, which creates email-like formatted patches. This command is particularly useful for applying a series of patches sent via email or saved in mailbox format.

Usage

git am [<options>] [(<mbox>|<Maildir>...)]

Options

-3 or --3way: Use 3-way merge if the patch fails to apply cleanly.

-s or --signoff: Add a Signed-off-by line to the commit message.

-skip: Skip the current patch and continue with the next one

--abort: Restore the original branch and abort the patch application

--continue: Continue applying patches after resolving conflicts.

-q or --quiet: Suppress all output.

--interactive: Allow user to interactively review and edit the patch before applying it.

git am -3 patchfile.mbox
git am -s patchfile.mbox
git am --skip
git am --abort
git am --continue
git am -q patchfile.mbox
git am --interactive patchfile.mbox

What It Does

  1. Applies Patches: Reads one or more email formatted patches and applies them sequentially to the current branch.

  2. Commits the Changes: Each patch is applied and committed, retaining the original commit message, author information, and timestamp.

Handling Conflicts: If a patch fails to apply cleanly, resolve the conflicts manually, then use git am --continue to proceed. If you decide not to apply the patch, use git am --skip to skip it or git am --abort to abort the process altogether.

Creating Patches: Use git format-patch to generate patches that are compatible with git am. This ensures the patches have the correct email-like format.

git format-patch -1 HEAD

Common Use Cases

-- Apply a Single Patch File
-- Applies the patch in patchfile.mbox to the current branch
git am patchfile.mbox

-- Apply Multiple Patch Files
-- Applies patch1.mbox and patch2.mbox sequentially to the current branch
git am patch1.mbox patch2.mbox

-- Apply All Patches in a Directory
-- Applies all patches in the specified directory
git am /path/to/patches/*

Example Workflow

-- Create Patches Using git format-patch
-- This creates email formatted patch files for the specified commit range
git format-patch -n <commit-range>

-- Apply the Patches
-- Applies all patch files in the current directory
git am *.patch

-- Resolve Conflicts and Continue
-- If a patch fails to apply cleanly, resolve the conflicts manually, then run
git am --continue

-- Abort the Patch Application
-- If we want to abort the process and revert to the original state
git am --abort

Example Output

When running git am patchfile.mbox

If a patch fails to apply cleanly

Last updated