Git 2 reference · Investigating and Tools
git bisect Command (Git)
Binary-search the history for the commit that introduced a bug, manually or automatically with a test script.
Git 2
Syntax
git bisect start BAD GOOD git bisect run SCRIPT git bisect reset
Parameters
| Parameter | Description |
|---|---|
start BAD GOOD |
A bad and a good commit. |
run CMD |
Automate: exit 0 = good, 1-127 = bad. |
reset |
Finish and go back. |
Examples
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
for i in 1 2 3 4 5 6; do v=$(( i >= 4 ? 0 : 1 )); echo "change $i ok=$v" > status.txt; git add status.txt; git commit -q -m "Change $i"; done
git bisect start HEAD HEAD~6 > /dev/null
git bisect run grep -q 'ok=1' status.txt 2>/dev/null | grep -A 6 'is the first bad commit' | grep -E 'first bad|Change'
git bisect reset > /dev/null
Output
6207c7baa1803756aa5cc74e7b7fcc9c54a6b2b3 is the first bad commit
Change 4Run this on your computer — see “Get Started” for setup.
git init -q -b main app && cd app
echo '# Shop' > README.md
git add README.md && git commit -q -m 'Initial commit'
for i in 1 2 3 4 5 6 7 8; do echo "build $i" > v; git add v; git commit -q -m "v$i"; done
git bisect start HEAD HEAD~8
git bisect good
git bisect bad | head -1
git bisect reset > /dev/null
Output
Bisecting: 3 revisions left to test after this (roughly 2 steps) [8d0215b17750e3937de7b02723125dfd566d2f46] v4 Bisecting: 1 revision left to test after this (roughly 1 step) [ca76f40d30751a3cd9dc928a8fabfcb6ee485382] v6 Bisecting: 0 revisions left to test after this (roughly 0 steps)
Run this on your computer — see “Get Started” for setup.
Related
git log· Show commit history: one line per commit, as a graph, filtered by author, date, path or message.git blame· Show who last changed each line of a file and in which commit, to find the context of a line of code.- Finding Bugs with git bisect · tutorial