git cherry-pick: backport a fix to an older plugin branch
A plugin has a main branch for version 3 and a release/2.x branch for the customers who still run version 2. A security fix lands on main, and it is also needed in 2.x. Merging main into the old branch would bring all of version 3 with it. git cherry-pick copies just that one commit.
Problem: A fix for an escaping bug in the settings page was committed on main. Version 2.8 has the same bug, and the 2.x branch differs from main by 200 commits.
Solution: Check out release/2.x, run git cherry-pick -x <commit> to apply the change of that commit as a new commit, resolve conflicts if the surrounding code differs, run the tests of the old branch, and release a 2.8.1.
The basic backport
git log --oneline main -5 # find the fix # 9c4e2a1 Escape the API label on the settings page git switch release/2.x git cherry-pick -x 9c4e2a1 git log -1 # Escape the API label on the settings page # # (cherry picked from commit 9c4e2a1...)
cherry-pick takes the diff that the commit introduced and applies it on top of the current branch as a new commit with a new hash. -x adds the line “(cherry picked from commit …)” to the message, so the connection between the two commits stays visible, which is the convention for backports.
Several commits
git cherry-pick -x 9c4e2a1 b71d0f3 # two separate commits git cherry-pick -x a1b2c3d^..f6e5d4c # a range, including a1b2c3d git cherry-pick -x -n 9c4e2a1 b71d0f3 # apply both, commit nothing yet git commit -m "Backport: escaping fixes from 3.1"
A..B means “after A up to B”, so the ^ includes the first commit. -n (--no-commit) applies the changes to the working tree only, to combine several fixes into one backport commit.
Conflicts
When the code around the fix changed between the versions, git stops:
git cherry-pick -x 9c4e2a1 # CONFLICT (content): Merge conflict in includes/class-settings.php git status # see the conflicting files # edit the file, keep the 2.x code and add the fix git add includes/class-settings.php git cherry-pick --continue # or give up and return to the state before: git cherry-pick --abort
A conflict in a backport needs careful reading: the fix was written for the version 3 code, and the “obvious” resolution can bring 3.x code that does not exist in 2.x. Run the test suite of the old branch afterwards, not the one from main.
Merge commits
git cherry-pick -x -m 1 4d5e6f7
A merge commit has two parents, and git does not know which side’s change to apply; -m 1 takes the change relative to the first parent, the branch that was merged into. In practice it is cleaner to cherry-pick the individual commits of the pull request.
Fix first on the old branch?
The opposite direction, fixing on release/2.x and merging that branch forward into main, avoids duplicate commits and makes sure no fix is forgotten in newer versions. It works when the branches are merged forward regularly. With cherry-picks, keep a list (or a label in the issue tracker) of fixes that still need a backport.
NOTE: A cherry-picked commit is a different commit. If the same branches are merged later, git usually recognizes the identical change, but a conflict-resolved backport can conflict again. Finding which commit introduced a bug in the first place: git bisect.