Read Anthropic’s case study about Graphite Reviewer

How to create and apply Git patches

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


This guide will explain what a patch is, how to create them from Git diffs, and how to apply those patches to other repositories or branches.

A patch in Git is a text file that represents changes between two sets of files, or commits. It’s essentially the output of the git diff command, packaged in a format that can be applied to another set of files. Patches are often used to share changes between repositories without committing or pushing changes directly to a remote repository.

A typical Git patch file contains:

  • File paths: Show the file(s) being changed.
  • Diff markers: Lines beginning with diff --git indicate the start of changes for a specific file.
  • Hunk headers: Lines beginning with @@ that show line numbers of the change in the source and target files.
  • Changes: Lines added to a file are prefixed with +, and lines removed from a file are prefixed with -.

Here’s a brief example of what a patch file might look like:

Terminal
diff --git a/filename.txt b/filename.txt
index 4c2a4ad..d4b7f3a 100644
--- a/filename.txt
+++ b/filename.txt
@@ -1,3 +1,4 @@
-This is the original line
+This is the new line
+Another new line added
This line remains unchanged

If you want to create a patch for changes that you have made locally but are not yet committed, run:

Terminal
git diff > changes.patch

This command redirects the output of git diff to a file named changes.patch. This will generate a patch file containing the differences between the state of your local repository and the state of the remote repository.

To export changes from a specific commit as a patch:

Terminal
git format-patch -1 <commit_hash>

This command generates a .patch file for the commit specified by <commit_hash>.

To create a patch with the differences between two branches:

Terminal
git diff branch1..branch2 > branch_diff.patch

This command takes the diff between branch1 and branch2 and saves it as branch_diff.patch.

To apply a patch file in Git, you can use the git apply command. This command takes changes from a patch file and applies them to the current working directory.

Terminal
git apply <filename.patch>

If you have a patch file named changes.patch, you can apply it like this:

Terminal
git apply changes.patch

When you run git apply changes.patch, Git reads the modifications listed in the changes.patch file and applies them to the corresponding files in your working directory. This allows you to incorporate changes, such as bug fixes or feature additions, from an external source into your project without committing them immediately, enabling further adjustments or testing if needed.

  • Checking the patch: Before actually applying the patch, you can check if the patch can be applied cleanly:

    Terminal
    git apply --check changes.patch
  1. If the patch can be applied cleanly: In this case, there's no output, indicating that the patch can be applied without any issues.

  2. If the patch cannot be applied cleanly (e.g., due to conflicts):

    Terminal
    error: patch failed: changes.patch:1
    error: filename.txt: patch does not apply

    This output shows that there are conflicts in applying the patch to filename.txt, specifying where the patch application failed and noting that the patch does not apply.

Applying a patch modifies the files in your working directory. It's equivalent to making the changes yourself manually or merging another branch with those changes. However, unlike a merge or rebase, applying a patch does not move branch pointers or make any commit on its own. After applying a patch, you need to stage and commit the changes yourself.

For further reading on applying patches in Git, see the official Git documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2