【Git Snippets】Manually find the commit that introduced a bug⭐

247 阅读2分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

本文同时参与「掘力星计划」,赢取创作大礼包,挑战创作激励金。

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 to manually find which commit in history introduced a bug.

  • Use git bisect start to start the process.
  • 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 (bad | good) marking each subsequent commit as "good" or "bad" depending if it has the bug or not.
  • 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 (bad | good)
$ git bisect reset [<commit>]

Rough Example

$ git bisect start
$ git bisect good 3050fc0de
$ git bisect bad c191f90c7
$ git bisect good # Current commit is good
$ git bisect bad # Current commit is buggy
# ... some time later the bad commit will be printed
$ git bisect reset # Goes to the original branch

Detailed Example🔥

Do you still not understand yet? Don't worry, here shows another detailed example to ensure you catch on it.

git bisect is a useful command to find which commit introduced an error.

The principle is simple: narrow down the history of code submissions in a dichotomy. The idea of "dichotomy" is to split the commit history into two parts, determine whether the problem is in the first or the second half, and continue this process until it narrows down to a single commit record.

This detailed example explains how to use this command with an example. Here is a codebase, clone it to local.

$ git clone git@github.com:bradleyboy/bisectercise.git
$ cd bisectercise

This repository is a web page - index.html. Open it in your browser.

On the page is a counter with two buttons. When you click the '+' button, you can see that instead of incrementing the counter, it decays, indicating that something is wrong with the code.

Now it's time to find out which code commit actually introduced the error. Firstly, check the code commit history.

$ git log --pretty=oneline

As we can see, this repository has a total of 101 commits. The earliest first submitted hash is 4d83cF.

git bisect start starts error checking in the following format:

git bisect start [end-id] [start-id]

In the code above, the "end point" is the most recent commit, and the "start point" is the more recent commit. This history between them is the extent of the error.

In this example, we select the entire code history. The starting point is the first commit 4D83CF and the end point is the most recent HEAD. Of course, you can specify other scopes as well.

$ git bisect start HEAD 4d83cf

After executing the command above, the codebase switches to the commit within the range, which in this case is commit 51(half).

Now refresh the browser and click the '+' button to see that it increments normally. Use git bisect good to indicate that this commit (51st) is ok.

$ git bisect good

Since there is no problem with the 51st commit, it means that the error was introduced later in the code's history. Execute the above command and Git automatically switches to the midpoint of the second half (commit 76).

Now refresh the browser and click the '+' button to see that the increment does not work properly. Use git bisect bad to signal that there is a problem with this commit (76).

$ git bisect bad

After the above command is executed, Git automatically switches to the midpoint of commit 51 to commit 76 (commit 63).

This process is then repeated until the offending commit is successfully found. Git will give you the following prompt.

b47892 is the first bad commit

Now that you've found the problematic commit, you can examine the code to determine exactly what went wrong.

Then, use git bisect reset command to exit the error check and return to the latest code commit.

$ git bisect reset

Now you can start fixing the bug.

(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.