Video in progress

Middle 🌿 Git

A bug in prod was fixed on `main`, but it is needed on the `release/2.3` branch too. How do you do that without merging all of `main`?

With git cherry-pick <hash> you copy exactly that fix commit onto the release branch. Git takes the commit's diff and applies it as a new commit on the current branch.

📖 Answer

git switch release/2.3
git pull
git cherry-pick -x 4e8f1a7
git push origin release/2.3

The -x flag adds "(cherry picked from commit 4e8f1a7...)" to the end of the message, which makes it easy later to track which fix was copied where. I always add it.

Several commits: git cherry-pick a1b2c3 d4e5f6, or a range a1b2c3^..d4e5f6.

What to watch out for:

  • the fix may depend on other code that is not in the release yet: a conflict, or worse, code that does not compile. Always run the tests on the release branch;
  • cherry-pick creates a new hash. If you later merge release/2.3 into main, Git usually recognizes the identical change, but sometimes gives a conflict;
  • a process of "we cherry-pick everything" is a sign that the branching strategy is broken.

Many teams choose the opposite way: the fix is made on the release branch first and then merged into main, so the fix does not get forgotten on main.