Bash
Git Bisect
Binary search to locate problem commits.
By EZ4Code Team
gitbisect
Code
#!/bin/bash
# Start binary search
git bisect start
# Mark current (broken) version as bad
git bisect bad
# Mark known good version as good
git bisect good v1.0.0
# Or
git bisect good <commit-hash>
# Git auto-checks out middle commit
# Mark after testing
git bisect good # No issue
git bisect bad # Has issue
# Repeat until problem commit found
# Git shows: <hash> is the first bad commit
# End binary search
git bisect reset
# Automated bisect
# Write test script test.sh
cat > test.sh << 'EOF'
#!/bin/bash
npm test
exit $? # 0=good, 1-125=bad, 125=skip
EOF
chmod +x test.sh
git bisect start HEAD v1.0.0
git bisect run ./test.sh
# Git auto-tests each commit, finds issue
# Skip untestable commits
git bisect skip
# View progress
git bisect log
# Revert to previous state
git bisect visualize
git bisect view
# Scenario: locate bug-introducing commit
git bisect start
git bisect bad # Current version has bug
git bisect good v2.0.0 # v2.0.0 is good
# Git checkout middle commit
# Run tests...
git bisect bad # This commit has bug
# Continue bisecting...
# Finally found first bad commitExplanation
git bisect quickly locates commits that introduced issues via binary search; it can be automated with test scripts.
More Bash Snippets
Variables and Arrays in Bash
Assign variables, use command substitution, and work with arrays in Bash.
File Operations
File and directory management.
Text Processing
Text processing with grep, sed, awk.
Loops
for and while loops.
Conditionals
if and case conditional statements.
Functions
Function definition and parameters.