Backport Flows
A backport carries a fix from the newest code to an older supported line. Hotfixes on one release branch were the previous lesson; real projects often support several lines at once (a library with 1.x and 2.x users, an LTS release next to the current one), and the hard questions become which lines get a fix, how to adapt it when the old code differs, and how to know nothing was missed.
A support policy first
Backporting without a policy turns into "whoever shouts loudest gets a backport". Write the policy down in the repository (SECURITY.md or the README):
| Line | Status | Gets |
|---|---|---|
3.x (main) |
current | features, fixes, security |
2.4.x (release/2.4) |
maintenance | bug fixes and security fixes |
1.9.x (release/1.9) |
LTS, until 2026-12-31 | security fixes only |
| ≤ 1.8 | end of life | nothing; upgrade |
The policy decides the target list for each fix; the mechanics below are the same for every target.
A session: one fix, two backports, one conflict
The bug is in limits.txt, which changed shape between 1.9 and 2.4. The fix lands on main, picks cleanly onto release/2.4, and conflicts on release/1.9, where it has to be adapted by hand:
mkdir lib && cd lib && git init -q
printf 'max_size=10\n' > limits.txt
git add . && git commit -q -m "feat: limits"
git branch release/1.9
printf 'max_size=10\nmax_depth=5\n' > limits.txt
git commit -q -am "feat: depth limit"
git branch release/2.4
printf 'max_size=10\nmax_depth=5\ntimeout=30\n' > limits.txt
git commit -q -am "feat: timeout (3.0)"
sed -i 's/max_size=10/max_size=8/' limits.txt
git commit -q -am "fix(security): lower max_size to stop memory exhaustion"
fix=$(git rev-parse --short HEAD)
echo "--- backport to release/2.4"
git switch -q release/2.4
git cherry-pick -x "$fix" >/dev/null && echo "clean pick"
cat limits.txt
echo "--- backport to release/1.9"
git switch -q release/1.9
git cherry-pick -x "$fix" >/dev/null 2>&1 || echo "conflict in: $(git diff --name-only --diff-filter=U)"
cat limits.txt
printf 'max_size=8\n' > limits.txt # adapt the fix to the 1.9 code
git add limits.txt
GIT_EDITOR=true git cherry-pick --continue >/dev/null
git log -1 --format='%s%n%b' release/1.9--- backport to release/2.4
clean pick
max_size=8
max_depth=5
--- backport to release/1.9
conflict in: limits.txt
<<<<<<< HEAD
max_size=10
=======
max_size=8
max_depth=5
timeout=30
>>>>>>> 62ae56c (fix(security): lower max_size to stop memory exhaustion)
fix(security): lower max_size to stop memory exhaustion
(cherry picked from commit 62ae56c6eba38f5486ec61c7ef6db7250488960c)
The conflict is the normal case for older lines, not an accident: the fix touched lines that look different in 1.9. Resolve it so the intent of the fix is kept (lower the limit to 8), not so the text of the newer code is copied in (that would also smuggle in max_depth and timeout). Mention the adaptation in the commit message or PR description; the reviewer of a backport mainly checks this.
Finding what hasn't been backported
git cherry compares two branches by patch content, not commit id, so it recognises cherry-picked commits even though their ids differ. + means "not in the target yet", - means "an equivalent patch is already there":
mkdir lib && cd lib && git init -q
c() { echo "$2" > "$1"; git add "$1"; git commit -q -m "$3"; }
c a.txt a "feat: a"
git branch release/2.4
c b.txt b "fix: b crashes on empty input"
c c.txt c "feat: c"
c d.txt d "fix: d leaks a file handle"
git switch -q release/2.4
git cherry-pick -x main~2 >/dev/null
git switch -q main
echo "--- git cherry -v release/2.4 main"
git cherry -v release/2.4 main
echo "--- fixes still missing from release/2.4"
git cherry -v release/2.4 main | awk '$1 == "+" && $3 ~ /^fix/ {print}'
echo "--- the same, with log --cherry-pick"
git log --oneline --cherry-pick --right-only --no-merges release/2.4...main--- git cherry -v release/2.4 main
- e17a050344d68dcbbeae0cb912233b2a3cd722ef fix: b crashes on empty input
+ 8da2b2560661f9e0ce4a0150dd60d84165d4bc9d feat: c
+ 51f4164773d38c432046e296284e7660c92a79c7 fix: d leaks a file handle
--- fixes still missing from release/2.4
+ 51f4164773d38c432046e296284e7660c92a79c7 fix: d leaks a file handle
--- the same, with log --cherry-pick
51f4164 fix: d leaks a file handle
8da2b25 feat: c
git cherry fails to match a commit that was adapted during the backport (its patch differs), so the -x trailer remains the reliable link. A release checklist for maintenance branches usually includes "every fix: on main since the branch point is either backported or explicitly marked as not needed".
Automating backports
On GitHub and GitLab, teams usually drive backports with labels on the original pull request (backport release/2.4): after the PR is merged, a bot or workflow cherry-picks the merge commit onto each labelled branch and opens a backport PR, or comments that it conflicted and needs a human. The pick itself is the same git cherry-pick -x as above, so the trailer and the review still apply. Popular choices are the community "backport" GitHub Actions and GitLab's cherry-pick button on merge requests; whichever you use, keep the labels in sync with your support policy, and remove the labels of lines that reach end of life.
Test yourself
A backport conflicts because the old branch's code is different. How should you resolve it?
- Take the new branch's version of the whole file
- Keep the intent of the fix, written against the old code, and note the adaptation
- Skip the backport
- Merge main into the old release branch
What does a - line in git cherry -v release/2.4 main mean?
- The commit was reverted
- An equivalent patch already exists on release/2.4
- The commit failed CI
- The commit is a merge
Which document should decide whether a fix goes to the 1.9 line?
- Whoever asks first
- A written support policy listing each line's status
- The CI system
- The changelog