Jan 1st, 2021 - written by Kimserey with .
Git cherry-pick
is a command used to apply changes from an existing commit. In this post we’ll look at some example where cherry picking can be useful.
Let’s take the following example where we have a master branch an my-branch which has 3 commits.
1
2
3
E--F--G my-branch
/
A--B--C--D master
Cherry picking allows us to bring a speicifc commit into a branch. Here if we want to bring F
into master, we can start by identifying the commit hash of F
:
1
2
3
4
5
6
7
8
9
10
❯ git --no-pager log -10 --oneline --graph --all
* d75c3cb (my-branch) G
* b9a83bb F
* 5a78f04 E
| * 421b367 (HEAD -> master) D
| * d2a3dae C
|/
* 498420c B
* 202d11a A
So we can see that F
is b9a83bb
, if we want to look at the commit, we can use show
:
1
❯ git show b9a83bb
Then to bring it into master we can do:
1
2
❯ git checkout master
❯ git cherry-pick b9a83bb
Which will then result in a new commit added:
1
2
3
E--F--G my-branch
/
A--B--C--D--F' master
By default the commit message will be added to F'
. If we want to change the commit message we can use -e
:
1
❯ git cherry-pick -e b9a83bb
That will allow us to edit the commit message before committing.
If for any reason, there is a conflict bringing the commit, git will stop for conflict resolution.
1
2
3
4
5
6
7
❯ git cherry-pick b9a83bb
Auto-merging b.md
CONFLICT (content): Merge conflict in b.md
error: could not apply b9a83bb... Cherry pick this
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
Git status
will give us a hint on the possible commands forward:
1
2
3
4
5
6
7
8
9
10
11
12
❯ git status
On branch master
You are currently cherry-picking commit b9a83bb.
(fix conflicts and run "git cherry-pick --continue")
(use "git cherry-pick --skip" to skip this patch)
(use "git cherry-pick --abort" to cancel the cherry-pick operation)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: b.md
no changes added to commit (use "git add" and/or "git commit -a")
--continue
to continue after having resolve conflict and added to index--skip
to skip the current commit if we are cherry picking multiple commits--abort
to completely abort the cherry picking