Skip to content
$ lynuxpro
← Git

LESSON 14 OF 21 · ADVANCED · 20 MIN

Cherry-pick, bisect and blame

HOW IT WORKSgit bisect: binary search for a bug

v1.0.0goodc5c6test: bad?c7HEADbad
1/4

A bug exists now but not in v1.0.0. Somewhere between are many commits.

$ git bisect start HEAD v1.0.0

Three commands turn Git from a storage system into a debugging and maintenance toolkit. git blame shows who last changed each line and in which commit. git bisect finds the exact commit that introduced a bug, even among thousands, by binary search. git cherry-pick copies a single commit from one branch to another, which is how hotfixes reach release branches. In this lesson you use all three to track down and fix a real bug in the shop project.

In simple words

  • git blame: shows who last changed each line of a file, and in which commit.
  • git bisect: finds the commit that introduced a bug by testing the middle of the history again and again.
  • git cherry-pick: copies one commit from another branch onto yours, such as a hotfix.

The bug

git config --global user.name "Leela Kumar"git clone https://gitlab.com/acme/shop.gitcd shopcat pricing.pycat test.shbash test.shecho "exit code: $?"
SHELLbashutf-8 · 7L
TAX_RATE = 1.8def total(subtotal):    return round(subtotal * (1 + TAX_RATE), 2)#!/usr/bin/env bash# exits 0 when pricing is correctgrep -q 'TAX_RATE = 0.18' pricing.pyexit code: 1
OUTPUTterminalutf-8 · 9L

Customers are being charged 180% tax instead of 18%. The test script fails. Release v1.0.0 was correct. What happened?

git blame: who changed this line?

git blame annotates every line of a file with the commit, author and date of the last change to that line:

git blame pricing.py
SHELLbashutf-8 · 1L
c82322f (Ravi Kumar   2026-11-05  1) TAX_RATE = 1.8dd73235 (Ravi Kumar   2026-11-03  2) dd73235 (Ravi Kumar   2026-11-03  3) dd73235 (Ravi Kumar   2026-11-03  4) def total(subtotal):dd73235 (Ravi Kumar   2026-11-03  5)     return round(subtotal * (1 + TAX_RATE), 2)
OUTPUTterminalutf-8 · 5L

Line 1 was last changed in c82322f. Look at it:

git show c82322f --stat
SHELLbashutf-8 · 1L

Useful options: -L 10,20 for a range of lines, -w to ignore whitespace changes, and -C to follow lines moved or copied from other files. A file called .git-blame-ignore-revs (used with --ignore-revs-file) lets you skip big reformatting commits so blame points at the real author of each line.

Blame is an investigation tool, not a way to assign fault. The commit message and its merge request usually explain why a line changed, which is what you need.

git bisect: binary search through history

Blame works when you know which line is wrong. Often you only know "it worked in v1.0.0 and it is broken now". git bisect finds the first bad commit by testing the commit halfway between a known good and a known bad one, halving the range each time. 1,000 commits need only about 10 tests.

By hand

git bisect startgit bisect badgit bisect good v1.0.0
SHELLbashutf-8 · 3L
status: waiting for both good and bad commitsstatus: waiting for good commit(s), bad commit knownBisecting: 2 revisions left to test after this (roughly 2 steps)[c82322fd58574d608be2cf68df8d838427e08087] Bump dependencies and tidy pricing
OUTPUTterminalutf-8 · 4L

Git checked out a commit in the middle. Test it and tell Git the result:

bash test.sh; echo $?git bisect badbash test.sh; echo $?git bisect good
SHELLbashutf-8 · 4L
1Bisecting: 1 revision left to test after this (roughly 1 step)[a7b39ba9f809c9ddd8fdc0ce472bbd4e5ed0db4d] Add Dockerfile0c82322fd58574d608be2cf68df8d838427e08087 is the first bad commitcommit c82322fd58574d608be2cf68df8d838427e08087Author: Ravi Kumar <ravi@acme.dev>Date:   Thu Nov 5 22:00:00 2026 +0000    Bump dependencies and tidy pricing pricing.py       |   2 +- requirements.txt |   1 + 2 files changed, 2 insertions(+), 1 deletion(-)
OUTPUTterminalutf-8 · 14L

Always finish with git bisect reset, which returns you to the branch you started on:

git bisect reset
SHELLbashutf-8 · 1L

Automatically with git bisect run

If a script can tell good from bad (exit code 0 = good, 1–127 = bad, 125 = cannot test, skip), Git does the whole search for you:

git bisect start HEAD v1.0.0git bisect run ./test.shgit bisect reset
SHELLbashutf-8 · 3L
Bisecting: 2 revisions left to test after this (roughly 2 steps)[c82322fd58574d608be2cf68df8d838427e08087] Bump dependencies and tidy pricingrunning  './test.sh'Bisecting: 1 revision left to test after this (roughly 1 step)[a7b39ba9f809c9ddd8fdc0ce472bbd4e5ed0db4d] Add Dockerfilerunning  './test.sh'c82322fd58574d608be2cf68df8d838427e08087 is the first bad commit…bisect found first bad commit
OUTPUTterminalutf-8 · 9L

In real projects the script might run a unit test, build the app, or curl an endpoint. Bisect with a test script is one of the fastest ways to debug regressions in large codebases.

git cherry-pick: copy one commit

Now fix the bug on main:

printf 'TAX_RATE = 0.18\n\n\ndef total(subtotal):\n    return round(subtotal * (1 + TAX_RATE), 2)\n' > pricing.pybash test.sh && echo "test passes"git commit -am "Fix GST rate: 18% not 180%"
SHELLbashutf-8 · 3L

Suppose customers are also running an older release line, maintained on a branch created from v1.0.0, that needs one specific change from main, the Dockerfile, but nothing else. git cherry-pick applies the change from a single commit as a new commit on the current branch:

git switch -c release/1.0 v1.0.0git cherry-pick a7b39bagit log --oneline -n 3
SHELLbashutf-8 · 3L
Switched to a new branch 'release/1.0'[release/1.0 4f13792] Add Dockerfile Date: Thu Nov 5 10:00:00 2026 +0000 1 file changed, 5 insertions(+) create mode 100644 Dockerfile4f13792 (HEAD -> release/1.0) Add Dockerfilee8b5455 (tag: v1.0.0) Document how to run the appdd73235 Add pricing with 18% GST
OUTPUTterminalutf-8 · 8L

The new commit has the same change, author and message as the original, but a different hash, because its parent is different.

Options worth knowing:

OptionEffect
-xadd "(cherry picked from commit …)" to the message, for traceability
A..Bpick a range of commits
-n / --no-commitapply the changes without committing
--continue / --abortafter or instead of resolving a conflict

Conflicts during a cherry-pick are resolved like merge conflicts: edit, git add, git cherry-pick --continue.

When to use cherry-pick

  • Backporting a fix to a release or maintenance branch: the main use.
  • Rescuing a commit made on the wrong branch.
  • Not as a regular way to move features between long-lived branches: copying the same change many times creates duplicate commits and confusing merges later. Prefer merging.

A common, safe flow for hotfixes is "fix on main first, then cherry-pick to release branches", so the fix is never lost in the next release.

Real-world scenario: a performance regression

API latency doubled at some point in the last three weeks, across 400 commits. An engineer wrote a 10-line script that started the service, sent 200 requests and exited 1 if the 95th percentile was above 120 ms, or 125 if the build failed. git bisect run ./perf-check.sh tested nine commits in 25 minutes and pointed at a commit that added a database query inside a loop. git blame on that function led to the merge request, whose author fixed it the same day, and the fix was cherry-picked with -x onto the current release branch.

Common mistakes

  • Forgetting git bisect reset, leaving the repository on a detached commit.
  • A bisect script that fails for unrelated reasons (for example a broken build): exit 125 to skip those commits.
  • Cherry-picking instead of merging for regular work, creating duplicates.
  • Cherry-picking a fix to the release branch only, so it is missing from the next release on main.
  • Using blame to blame people. Use it to find context.

Practice

  1. Clone the shop project, run git blame pricing.py and find the commit that last changed line 1.
  2. Find the same commit manually with git bisect start, bad, good v1.0.0, testing with bash test.sh. Then git bisect reset.
  3. Repeat automatically with git bisect run ./test.sh.
  4. Fix the bug on main, then cherry-pick the fix onto a release/1.0 branch created from v1.0.0, using -x.

Cheat sheet

git blame file [-L 1,20] [-w] [-C]       who last changed each linegit bisect start [BAD [GOOD]]git bisect good | bad | skipgit bisect run ./script.sh               0 good, 1-127 bad, 125 skipgit bisect log | resetgit cherry-pick <commit> [-x]            copy a commit heregit cherry-pick A..B                     copy a rangegit cherry-pick --continue | --abort
OUTPUTterminalutf-8 · 8L

Key takeaways

  • git blame links each line to the commit that last changed it; read that commit for context.
  • git bisect finds the first bad commit by binary search; with git bisect run and a test script it is fully automatic.
  • git cherry-pick copies individual commits, mainly to backport fixes to release branches; use -x for traceability.
  • Fix on main first, then cherry-pick to older branches.