Backporting/Replaying Changes using git format-patch and git apply / git-am

Today I was working on a number of different Git repos that needed exactly the same changes applied to them, including their commit message.

Because it was using a completely different Git repo, I couldn't use git cherry-pick to do this, so needed some way to apply the patch some way.

Luckily this works quite nicely with Git because one of the largest workflows within Git is sending patches via email.

We can utilise this approach to generate a patch:

$ git format-patch -1 --stdout HEAD > patch

Which generates the following file:

From 22cbe446a91ff0d63f7a7df9e794d878d7a5c340 Mon Sep 17 00:00:00 2001
From: Jamie Tanna <gitlab@jamietanna.co.uk>
Date: Tue, 17 Dec 2019 19:53:29 +0000
Subject: [PATCH] MP: Add notes 2019/12/ninpe

---
 content/mf2/2019/12/ninpe.md | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 content/mf2/2019/12/ninpe.md

diff --git a/content/mf2/2019/12/ninpe.md b/content/mf2/2019/12/ninpe.md
new file mode 100644
index 00000000..74b851a3
--- /dev/null
+++ b/content/mf2/2019/12/ninpe.md
@@ -0,0 +1,15 @@
+{
+  "kind" : "notes",
+  "slug" : "2019/12/ninpe",
+  "client_id" : "https://indigenous.realize.be",
+  "date" : "2019-12-17T19:53:00Z",
+  "h" : "h-entry",
+  "properties" : {
+    "published" : [ "2019-12-17T19:53:00Z" ],
+    "content" : [ {
+      "html" : "",
+      "value" : "For anyone having issues not seeing #MrRobot Season 4 Episode 11 in their Fire TV / Prime video account, try skipping through Ep 10 until it autoplays 11"
+    } ],
+    "syndication" : [ "https://brid.gy/publish/twitter" ]
+  }
+}
--
2.24.0

And then we can then feed this file into git apply:

$ git apply < patch

However, this doesn't quite work when you want to use the commit message, too. For this, we can use git am:

$ git am < patch

This may have issues if the patch doesn't apply cleanly, but once resolved, you'll have the commit applied with patch and commit message!

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #git.

This post was filed under articles.

Has this content helped you? Please consider supporting me so I can continue to create content like this!

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.