Frequently asked questions around Git and Version Control.
Git FAQ featured image

How to Create and Apply a Patch in Git

Patches are an alternative way to exchange code changes. Although today - with the widespread use of remote repositories, forks, and Pull Requests - exchanging code via patches isn't very common anymore, it can be a valuable tool in special situations.

By creating a patch, you can essentially "export" one or more commits into plain text files, which you can then easily send to someone else for integration.

In this short article, we'll look at how to both create and apply patches.

The Git Cheat Sheet

No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!

Using git format-patch to Create a Patch

To create a patch, we will use the git format-patch command. Most importantly, we must tell Git which commits exactly we want to be included in the patch. Let's take the following scenario as an example:

  • we are currently on a bugfix branch named "bugfix/broken-navigation"
  • we had based the branch on our local "master" branch
  • after some work, we have created two new commits on the bugfix branch
  • we now want to create a patch that contains these two new commits so we can send them to a colleague that wants to integrate them

We can achieve this with the following commands:

# Switch to the base branch that is missing the new changes
$ git checkout master

# Create patch files for all new commits in our bugfix branch
$ git format-patch bugfix/broken-navigation

The git format-patch command will include all of the commits that are contained in the specified branch ("bugfix/broken-navigation"), but not in the currently checked out branch ("master").

This will leave us with two patch files, which contents look approximately like this:

From 7173808e55679215179217cec8aac1ca0a213bb5 Mon Sep 17 00:00:00 2001
From: example 
Date: Tue, 7 July 2020 10:05:28 +0200
Subject: [PATCH 2/2] Change page structure

 about.html   | 2 +-
 index.html   | 4 ++--
 product.html | 0
 3 files changed, 3 insertions(+), 3 deletions(-)
 create mode 100644 product.html

diff --git a/about.html b/about.html
index 0c20c33..ee7cc3d 100644
--- a/about.html
+++ b/about.html
@@ -13,7 +13,7 @@
       <div id="navigation">
           <li><a href="index.html">Home</a></li>
-          <li><a href="about.html">About</a></li>
+          <li><a href="about.html">About Us</a></li>
           <li><a href="imprint.html">Imprint</a></li>

In case you'd prefer to have just a single file containing all of the commits, you can use the --stdout option:

$ git format-patch bugfix/broken-navigation --stdout > bugfix.patch

In any case, you'll be left with one or multiple .patch files which you can send to a colleague for review and integration.


Creating & Applying Patches in Tower

In case you are using the Tower Git client, creating and applying patches is very easy: you can simly select the commits you want to save as a patch files - and later apply them just as quickly.

Using git am to Apply a Patch

The receiver of the patch file(s) can then apply the changes using the git am command:

# Switch to the branch where the changes should be applied
$ git checkout master

# Apply the patch
$ git am bugfix.patch

# Check what has happened in the commit log
$ git log

In the commit history, you should now find that the new commit(s) have been added!

Learn More

About Us

As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.

Just like with Tower, our mission with this platform is to help people become better professionals.

That's why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.