【Git Snippets】Automatically find the commit that introduced a bug
The Git snippet collection contains a variety of short tips and tricks for all currently maintained versions of git. It includes most commonly-used commands and covers various use-cases in the form of simplified documentation, complete with multiple examples.
Short code snippets for all your development needs.
Uses a binary search algorithm and a given script to find which commit in history introduced a bug.
- Use
git bisect start
- Use
git bisect good <commit>
to mark a<commit>
as "good", indicating it is known to be bug-free. - Use
git bisect bad <commit>
to mark a different<commit>
as "bad" indicating it has the bug. - Use
git bisect run <command>
to run the given<command>
on each subsequent commit to find which commit introduce the bug. - Use
git bisect reset
to reset to the original branch. You can optionally specify a<commit>
to reset to.
$ git bisect start
$ git bisect good <commit>
$ git bisect bad <commit>
$ git bisect run <commit>
$ git bisect reset [<commit>]
Rough Example
$ git bisect start
$ git bisect good 3050fc0de
$ git bisect bad c191f90c7
$ git bisect run npm test # Run `npm test` for each commit
# ... some time later the bad commit will be printed
$ git bisect reset # Goes to the original branch
Detailed Example🔥
A linear code commit history is a great help in automating problem location, allowing us to go back from the current problematic commit to the first problematic commit in history.
Even better, we can use a split lookup to find the problematic commit in order (LogNLogNLogN) time.
For example, in a codebase, there is a linear commit history between C101C101C101 and C200C200C200. We know a test that passes at C101C101C101 but fails at C200C200C200. We can checkout C101C101C101, C102C102C102, till C200C200C200, and run the tests after each checkout to find the first commit that fails the test.
However, the method above is extremely inefficient. So, Git provides us with automated test scripts. Here is the demo:
$ git clone git@github.com:jungejason/git-bisect-run-demo.git
cd git-bisect-run-demo
57715@Wu-Yikun MINGW64 /d/Wyk's WorkSpace/git-bisect-run-demo (master)
$ git bisect start
57715@Wu-Yikun MINGW64 /d/Wyk's WorkSpace/git-bisect-run-demo (master|BISECTING)
$ git bisect bad HEAD
57715@Wu-Yikun MINGW64 /d/Wyk's WorkSpace/git-bisect-run-demo (master|BISECTING)
$ git log --grep='C101' --oneline | cat
f1a0469 C101 - change another number
57715@Wu-Yikun MINGW64 /d/Wyk's WorkSpace/git-bisect-run-demo (master|BISECTING)
$ git bisect good f1a0469
Bisecting: 49 revisions left to test after this (roughly 6 steps)
[94d9240b05a108395fb2969fe2a4b6c21fce8e1b] C150 - some change
57715@Wu-Yikun MINGW64 /d/Wyk's WorkSpace/git-bisect-run-demo ((94d9240...)|BISECTING)
$ git bisect run bash test.sh
running bash test.sh
Test Passed
Bisecting: 24 revisions left to test after this (roughly 5 steps)
[8753c8521a465956807e26fe7a3ee79daa5cceb2] C175 - some change
running bash test.sh
Test Passed
Bisecting: 12 revisions left to test after this (roughly 4 steps)
[4e7bafdc9cbab4b2409f7d2d78822200c4578c5b] C187 - some change
running bash test.sh
Test Failed
Bisecting: 5 revisions left to test after this (roughly 3 steps)
[7028c24aea20364fa69af786e31a5e5267c34c36] C181 - some change
running bash test.sh
Test Failed
Bisecting: 2 revisions left to test after this (roughly 2 steps)
[c8e79ede15330e6247cbfe7b212c483483b2433d] C178 - some change
running bash test.sh
Test Passed
Bisecting: 0 revisions left to test after this (roughly 1 step)
[327fc00b43b5041cb2fc626ab8589c607111f164] C180 - some change
running bash test.sh
Test Passed
7028c24aea20364fa69af786e31a5e5267c34c36 is the first bad commit
commit 7028c24aea20364fa69af786e31a5e5267c34c36
Author: Jason Ge <gejun_1978@yahoo.com>
Date: Tue Oct 1 16:50:32 2019 +0800
C181 - some change
Summary: add one line
Test: none
inventory.csv | 1 +
1 file changed, 1 insertion(+)
bisect run success
$ git bisect reset
git bisect run
automatically finds C181C181C181.
7028c24aea20364fa69af786e31a5e5267c34c36 is the first bad commit
commit 7028c24aea20364fa69af786e31a5e5267c34c36
Author: Jason Ge <gejun_1978@yahoo.com>
Date: Tue Oct 1 16:50:32 2019 +0800
C181 - some change
Summary: add one line
Test: none
inventory.csv | 1 +
1 file changed, 1 insertion(+)
bisect run success
(Over)
🌊Wish you can benefit from this article. 🍺And welcome to leave your thoughts in the comments section that we can discuss and share ideas with each other.
转载自:https://juejin.cn/post/7020960901704450085