Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readGit

Skip redundant commits when cherry-picking

Use git cherry-pick --empty=drop to backport a range whose changes partly exist on the release branch, and check what Git added.

You need two commits from main on a release branch. Someone backported the fix in the first commit under a different commit ID. The second commit adds a regression check. A cherry-pick of the range pauses at the fix because applying it leaves nothing to commit.

Git calls this a commit that becomes empty. The source commit changed a file. The release branch has that change, so the same pick leaves its tree as it was.

diagram

Only the regression check adds a commit

The release branch has the status fix before the pick, so the regression check is the only commit Git adds.

Drop the pick that adds no change

Start with a clean working tree and no cherry-pick in progress. In this example, older and newer are tags for the two adjacent source commits. Use the parent of older as the excluded side of the range so that the pick includes older.

git switch release
git cherry-pick --empty=drop 'older^..newer'
git log -2 --oneline

On release you see Add regression check above Backport status fix, with no second fix commit in between.

--empty=drop comes from Git 2.45. It applies when a source commit had a change and the result of applying it is empty. Git moves on to the next commit in the range. The decision follows the result of applying the change, so the original and backported commit IDs can differ.

Keep the stops that still need a decision

A conflict still stops the sequence. Inspect the conflicting files and resolve them before continuing. If you ran the range without the flag and Git has stopped on an empty pick, check that the expected change is present. Then git cherry-pick --skip moves past that commit.

A commit that starts empty, such as a marker used to trigger CI, is a separate case. --empty=drop still stops there unless you tell Git how to handle originally empty commits. Use --allow-empty if those markers belong in the release history too.

After the pick, run the regression check on the release branch. The status fix can be present and the check can still depend on another change from main.