Sub topic 12
Putting it together: find a bug, then port the fix
This chapter combines stash, bisect, and cherry-pick in one scenario. It assumes you know add, commit, branch, and switch, and that you have read the earlier chapters on each command.
The scenario
You are halfway through a feature on main when a report arrives. Version v1.2.0 worked, but the current main crashes when a user exports an empty report. Customers on release-1.4 have the same bug and need a fix.
The plan has four steps:
- Shelve your unfinished work with stash.
- Find the commit that introduced the bug with bisect.
- Fix the bug on
main. - Copy the fix to
release-1.4with cherry-pick.
Step 1: Clear the working tree
Bisect checks out old commits. Uncommitted changes could block a checkout or get mixed into your test results, so shelve them first. Include untracked files, because new files can also affect a build.
git stash push -u -m "wip: export filters"
git statusgit status should report a clean working tree. If it doesn't, the stash did not capture everything. Check before you continue.
Step 2: Find the bad commit
Write a script that fails when the bug is present. This one assumes your project has a command that exports an empty report.
#!/bin/sh
# test-empty-export.sh
make -s || exit 125 # cannot build: skip this commit
./bin/export --input empty.csv > /dev/nullThe script exits with the exit code of its last command. If ./bin/export crashes, the code is non-zero and bisect marks the commit bad. Make the script executable and run the bisect:
chmod +x test-empty-export.sh
git bisect start HEAD v1.2.0
git bisect run ./test-empty-export.shKeep the script outside the tracked files, or commit it somewhere stable. Bisect checks out old commits, and a script that exists only in some of them will behave differently from one commit to the next.
When the run finishes, Git prints the first bad commit. Record its hash and message, then restore your checkout:
git bisect log
git bisect resetStep 3: Fix the bug on main
Once you are back on main, read the bad commit with git show <hash>. Make the smallest fix you can, and add a test. Keep the fix in its own commit with no unrelated edits, because cherry-pick copies a whole commit.
git add src/export.c tests/test_empty_export.c
git commit -m "Fix crash when exporting an empty report"Step 4: Port the fix to the release branch
Switch to the release branch and copy the commit. The -x option appends the original hash to the message, which helps anyone auditing the release later.
git switch release-1.4
git cherry-pick -x <fix-hash>If the bad commit was never on release-1.4, check that the bug exists there before you port the fix. Run the same test script on that branch. If the fix conflicts, resolve the files, run git add, and then git cherry-pick --continue. If it gets confusing, run git cherry-pick --abort and start again.
Step 5: Resume your own work
git switch main
git stash popIf pop reports a conflict, Git keeps the stash in the list. Resolve the conflict, then run git stash drop yourself once you are sure nothing is lost.
Common mistakes
| Mistake | Effect | Prevention |
|---|---|---|
| Starting bisect with uncommitted changes | Checkout may fail, or results are polluted | Stash first, with `-u` |
| Forgetting `git bisect reset` | You stay on a detached HEAD at an old commit | Always reset when the run ends |
| Mixing the fix with other changes | Cherry-pick brings extra code to the release | Commit the fix alone |
| Omitting `-x` | The origin of the commit is lost | Use `-x` on release branches |
| A test script that exits 1 for build failures | Bisect blames the wrong commit | Exit 125 when the commit cannot be tested |
Exercise
During a bisect run, your test script returns exit code 1 on a commit that does not compile, so bisect marks it bad. Why is this a problem, and what change fixes it?
Show answer
Exit code 1 means "bad", so bisect treats a build failure as the bug itself and may report a commit that has nothing to do with the regression. Make the script run the build step with make -s || exit 125, so bisect skips commits that cannot be tested.
Exercise
After fixing a bug on main, you switch to release-1.4 and run git cherry-pick -x <hash>. What does -x add, and why is it useful?
Show answer
It appends a line such as "(cherry picked from commit <hash>)" to the new commit's message. This lets you trace the release commit back to the original fix on main.