Git: Create & Apply Patch using format-patch

Previously, I had written an article to create and apply patches using the git diff and git apply commands.

In this article, we will look into alternative git commands to create and apply git patches. Those commands are git format-patch (to create patch) and git am (to apply patch).

Check commits in your branches

Checkout to the master branch


> git checkout master 

Check the latest 5 commits in the current (master) branch


> git log --oneline -n 5 

87ea803 (HEAD -> master, origin/master, origin/HEAD) Update README.md (#904)
00d4c7d Add link
5831a34 Fix dupes
5a2aaae More clean up
5585126 Clean up

Show branches


> git branch 

* master
  test

Check the latest 5 commits in the branch named “test”


> git log test --oneline -n 5

04d3d11 (test) Update README
39fdf70 Update hello file
32547fa Add hello file
87ea803 (HEAD -> master, origin/master, origin/HEAD) Update README.md (#904)
00d4c7d Add link

We can see that test branch is 3 commits ahead of the master branch.

So, if we want to create a patch with the new commits in the test branch, then we can do it using format-patch command.

Create Patch

The format-patch command prepares each commit with its patch in one file per commit.

The format-patch command will create a separate patch file for each commit that is present in the specified branch that is not in the currently checked-out branch.

In our case, the format-patch will create a separate patch file for each commit present in the test branch that is not present in the master branch.


> git checkout test 

> git log --oneline -n 5 

04d3d11 (HEAD -> test) Update README
39fdf70 Update hello file
32547fa Add hello file
87ea803 (origin/master, origin/HEAD, master) Update README.md (#904)
00d4c7d Add link

> git format-patch master

0001-Add-hello-file.patch
0002-Update-hello-file.patch
0003-Update-README.patch

The files are created in the current working directory.


> cat 0001-Add-hello-file.patch

From 32547fa0c16d99f316965693d647d8775f7c59be Mon Sep 17 00:00:00 2001
From: Mukesh Chapagain <example@gmail.com>
Date: Sat, 14 Jan 2023 23:09:09 -0500
Subject: [PATCH 1/3] Add hello file

---
 hello.md | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 hello.md

diff --git a/hello.md b/hello.md
new file mode 100644
index 0000000..e69de29
--
2.24.3 (Apple Git-128)

> cat 0002-Update-hello-file.patch
From 39fdf70f60a071c0902c65a2a51c0cc479dc4e28 Mon Sep 17 00:00:00 2001
From: Mukesh Chapagain <example@gmail.com>
Date: Sun, 15 Jan 2023 21:17:03 -0500
Subject: [PATCH 2/3] Update hello file

---
 hello.md | 1 +
 1 file changed, 1 insertion(+)

diff --git a/hello.md b/hello.md
index e69de29..e965047 100644
--- a/hello.md
+++ b/hello.md
@@ -0,0 +1 @@
+Hello
--
2.24.3 (Apple Git-128)

Generate patch files in a specified directory


> git format-patch <branch> -o <directory>

Example:


> mkdir patches 

> git format-patch master -o patches

patches/0001-Add-hello-file.patch
patches/0002-Update-hello-file.patch
patches/0003-Update-README.patch

Limit the number of patches creation


# only generating 1 patch file for 1 latest commit 
> git format-patch -1 master -o patches 
patches/0001-Update-README.md-904.patch

Create patch for a specific commit


> git format-patch <commit-hash>

Example:


> git format-patch -1 39fdf70 -o patches
0001-Update-README.patch

Output all the commits patches into a single patch file


> git format-patch <branch> --stdout > <file-path>

Example:


> git format-patch master --stdout > patches/my-custom-fix.patch

Apply Git Patches

For the patches created by format-patch command, we use the git am command to apply the patch.


> git am <patch-file>

We check out the branch where we want to apply the patch.


> git checkout master 

And, then apply the patch


> git am patches/0001-Add-hello-file.patch

Hope this helps. Thanks.