Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Created June 18, 2022 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x-yuri/1a9a6d3f6ca27a1b2e0fb48d1b31550e to your computer and use it in GitHub Desktop.
Save x-yuri/1a9a6d3f6ca27a1b2e0fb48d1b31550e to your computer and use it in GitHub Desktop.
git: cherry-pick vs format-patch/am

git: cherry-pick vs format-patch/am

a.sh:

set -eux
rm -rf r1 r2

mkdir r1
(cd r1
git init
echo 'line 1
line 2
line 3' > a
git add a
git commit -m c1
sed -Ei 's/line 3/line 33/' a
git add a
git commit -m c2
git format-patch -1 HEAD)

mkdir r2
(cd r2
git init
echo 'line 11
line 2
line 3' > a
git add a
git commit -m c1
cat a
cat ../r1/0001-c2.patch
git am ../r1/0001-c2.patch)
$ sh a.sh
...
+ cat a
line 11
line 2
line 3
+ cat ../r1/0001-c2.patch
From 357abb75a3662ba0feb71779a0130332758c31e3 Mon Sep 17 00:00:00 2001
From: Yuri Kanivetsky <yuri.kanivetsky@gmail.com>
Date: Sat, 18 Jun 2022 10:04:15 +0300
Subject: [PATCH] c2

---
 a | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/a b/a
index a92d664..7bd0b60 100644
--- a/a
+++ b/a
@@ -1,3 +1,3 @@
 line 1
 line 2
-line 3
+line 33
-- 
2.36.1

+ git am ../r1/0001-c2.patch
error: patch failed: a:1
error: a: patch does not apply
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Applying: c2
Patch failed at 0001 c2
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

b.sh:

set -eux
rm -rf r1 r2

mkdir r1
(cd r1
git init
echo 'line 1
line 2
line 3' > a
git add a
git commit -m c1
sed -Ei 's/line 3/line 33/' a
git add a
git commit -m c2)

mkdir r2
(cd r2
git init
echo 'line 11
line 2
line 3' > a
git add a
git commit -m c1
git remote add r1 ../r1
git fetch r1
git --no-pager log --oneline --graph --all
git cherry-pick r1/master
git --no-pager log --oneline --graph --all)
$ sh b.sh
...
+ git --no-pager log --oneline --graph --all --decorate
* 281b8c1 (HEAD -> master) c1
* c9cc2e4 (r1/master) c2
* 0c19f0e c1
+ git cherry-pick r1/master
Auto-merging a
[master 061bb49] c2
 Date: Sat Jun 18 10:08:44 2022 +0300
 1 file changed, 1 insertion(+), 1 deletion(-)
+ git --no-pager log --oneline --graph --all --decorate
* 061bb49 (HEAD -> master) c2
* 281b8c1 c1
* c9cc2e4 (r1/master) c2
* 0c19f0e c1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment