When Cherry-Picking Might Fail
Learn how you can handle scenarios where cherry-pick might fail.
We'll cover the following
Cherry-pick not easily applied#
A case where the cherry-pick is not easily applied is outlined below. It’s exactly the same as the run-through in the previous lesson. But this time, the “crazy change” is not added at the top of the file, but in a line affected on the other branch also.
1 mkdir lgthw_cherry_pick_2
2 cd lgthw_cherry_pick_2
3 git init
4 echo change1 > file1
5 git add file1
6 git commit -am change1
7 echo change2 >> file1
8 git commit -am change2
9 git log --all --oneline --decorate --graph
10 git branch experimental
11 git checkout experimental
12 echo crazy change >> file1
That last line is different; you just appended it to the file rather than adding it to the top of the file like you did last time.
13 cat file1
14 git commit -am crazy
15 echo more sensible change >> file1
16 cat file1
17 git commit -am sensible
18 git log --all --oneline --decorate --graph
Again, remember to replace the ID below with the commit identifier you found in the git log
command above:
19 git checkout master
20 git cherry-pick ID
When cherry-picking the above, you will get a message like this:
error: could not apply 743d18e... sensible
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'
In which case, you need to follow the hints above.
As always, a git status
helps you see what’s going on.
21 git status
At this point, you should be able to figure out what to do next; resolve the conflict manually and git add
the resolved file.
Cherry-picking is often a simple and easier way to move changes between different branches, which can be very useful.
However, there are cases where cherry-picking does not work the way you might expect. This will be covered in a later part, which deals with edge cases and more subtle nuances of Git.