Sub topic 15
Backporting Fixes with Cherry-Pick
This chapter assumes you can create and switch branches, commit, merge, and resolve a basic conflict. It also assumes you know what a release branch is from the previous chapter.
Why backport
A release branch such as release/1.4 freezes a version that customers still run. A bug found later is usually fixed on main first, where new work happens. Merging main into the release branch would pull in every other change made since the branch was cut. A backport copies only the fix.
git cherry-pick does this. It takes the changes introduced by one commit and applies them as a new commit on your current branch. The new commit has a different hash from the original, because its parent is different.
The basic workflow
Fix the bug on main and merge it. Find the hash of the fix commit, then apply it to the release branch.
git switch release/1.4
git cherry-pick -x <commit-sha>The -x option appends a line such as (cherry picked from commit abc1234) to the new commit message. That line lets a reader trace the backport to its source, so use it whenever the original commit is on a shared branch.
Picking several commits
You can name more than one commit, or a range. Commits apply in the order you list them.
git cherry-pick -x abc1234 def5678
git cherry-pick -x abc1234^..def5678The second form includes abc1234 itself. Without the ^, a range A..B excludes A.
For a merge commit, Git cannot tell which parent counts as the mainline. Pass -m 1 to treat the first parent as the mainline. Squash merges avoid this problem because each one produces a single ordinary commit.
Handling conflicts
The release branch has diverged from main, so the fix may not apply cleanly. When a conflict happens, Git stops and marks the files. Resolve them the same way as in a merge, then continue.
git status
# edit the conflicted files
git add path/to/file
git cherry-pick --continueTo give up, run git cherry-pick --abort. It restores the branch to its state before you started.
Common errors
- Forgetting the fix on
main. Backport only after the fix exists on the main line. Otherwise the next release regresses the bug. - Picking part of a fix. If the fix depends on an earlier refactor, the pick may apply cleanly but behave wrongly. Run the tests on the release branch.
- Duplicate changes on merge. If the release branch is later merged into
main, Git usually recognizes identical changes. Repeated conflicts on the same lines suggest the picks were edited heavily. - Using it as a routine integration tool. Copying many commits by hand is a sign that the branches should be merged or rebased instead.
Finding what has not been backported
To list commits on main whose changes are not yet on the release branch, compare the two with git cherry.
git cherry -v release/1.4 mainLines starting with + have no equivalent on the release branch. Lines starting with - do. Git compares the change content, so it recognizes picks made with different hashes.
Exercise
You fixed a bug in commit abc1234 on main. Write the commands to backport it to release/1.4, and explain why the new commit has a different hash from abc1234.
Show answer
Run git switch release/1.4 and then git cherry-pick -x abc1234. The new commit has a different hash because a commit hash is computed from its contents, including its parent commit and metadata. The copy has a different parent on the release branch, so its hash differs even though the change is the same.
Exercise
During a cherry-pick, Git reports a conflict in app.js. List the steps to finish the pick, and the command to cancel it instead.
Show answer
Edit app.js to resolve the conflict markers, run git add app.js, then run git cherry-pick --continue. To cancel, run git cherry-pick --abort.