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

Example Workflow

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

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.

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.

circle-info

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.

Common Use Cases

Example Workflow

Example Output

When running git am patchfile.mbox

If a patch fails to apply cleanly

Last updated