Sub topic 7
When a cherry-pick conflicts
This chapter assumes you can run git cherry-pick <commit> on a clean branch, read git status output, and edit a file with conflict markers from a merge. Here you will learn why a cherry-pick conflicts and how to finish, skip, or abort one.
Why conflicts happen
A cherry-pick replays the change that a commit introduced. Git does this as a three-way merge with these inputs:
- Base: the parent of the commit you are picking.
- Ours: the current
HEAD, which is the branch you are on. - Theirs: the commit you are picking.
Git applies the difference between base and theirs onto ours. If the same lines changed on your branch, or the surrounding code is gone, Git cannot choose and stops. This is common when you port a fix to an older release branch, because the code around the fix has drifted.
In a cherry-pick, "ours" and "theirs" are the reverse of what many people expect from a rebase. "Ours" is the branch you are standing on. "Theirs" is the commit being copied.
The recovery loop
git cherry-pick 3f2a9c1
# CONFLICT reported
git status
# edit the conflicted files, then:
git add path/to/resolved-file
git cherry-pick --continue
# or give up:
git cherry-pick --abortgit status lists the files under "Unmerged paths" and says you are currently cherry-picking a commit. Open each file and look for markers like these:
<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> 3f2a9c1 (Raise request timeout)The section above ======= is your branch. The section below is the picked commit. Edit the file to the result you want and delete all three marker lines. Then git add the file, which tells Git the conflict is resolved, and run git cherry-pick --continue. Git opens your editor with the original commit message so you can adjust it.
Seeing more context
Plain markers show only two sides. Git can also show the base, which makes it easier to see what each side changed:
git config merge.conflictStyle diff3
git checkout --merge path/to/fileThe second command rebuilds the conflict markers for that file using the new style. If you want one side wholesale, take it with git checkout --theirs -- path/to/file (the picked commit) or --ours (your branch), then git add the file.
Skip, abort, and quit
Each option does something different, and the differences matter most when you pick a range such as A..B.
| Command | Effect |
|---|---|
| `git cherry-pick --continue` | Commits your resolution and carries on with the rest of the range. |
| `git cherry-pick --skip` | Drops the current commit and moves to the next one in the range. |
| `git cherry-pick --abort` | Cancels the whole operation and restores the state from before it began. |
| `git cherry-pick --quit` | Forgets the operation but keeps the current files and any commits already made. |
With a range, Git creates commits one at a time. If the third pick conflicts, the first two are already committed. --abort removes them too, because it returns you to where you started. --quit leaves them in place.
Empty picks
Sometimes the change already exists on your branch. Git then stops with a message that the previous cherry-pick is now empty. This is not a conflict. Run git cherry-pick --skip to drop that commit. If you want an empty commit to record the pick, run git commit --allow-empty and then --continue.
Common errors
- Forgetting
git add.--continuerefuses to run while any path is unmerged. Rungit statusto find the file you missed. - Leaving markers in a file.
git adddoes not check file contents. Rungit diff --checkbefore continuing to catch leftover<<<<<<<lines. - Starting with a dirty tree. A cherry-pick can refuse to start if your uncommitted changes overlap the files it touches. Stash first, as covered earlier in this tutorial.
- Resolving blindly. A resolved cherry-pick can compile and still be wrong. Build and run the tests on the target branch before you push.
Exercise
You run git cherry-pick A..B and the third commit in the range conflicts. You decide the range is the wrong one to pick. Which command returns the branch to its state before the first pick, and why does --quit not do this?
Show answer
git cherry-pick --abort restores the branch to the state before the whole operation began, so the two commits already created are removed. --quit only clears Git's record of the operation. It keeps the two finished commits and leaves the conflicted files in your working tree.