Home > Enterprise >  Can I use git to find which commit is needed to fix a branch?
Can I use git to find which commit is needed to fix a branch?

Time:02-08

I have a git tree which looks like this:

master (test passes)
|
|
|  stable (test fails)
|   |
|   /
 ---
|

There is a test which fails when run against the stable branch, but passes when run against the master branch. As it is a complicated multi-process test it's rather hard to debug.

Can I use git to find which commit from the master branch can be cherry-picked to the stable branch in order to fix the test?

There are many hundreds of commits on both branches so doing it manually would be painful.

CodePudding user response:

There are many hundreds of commits on both branches so doing it manually would be painful.

This is a job for git-bisect.

Start the bisect, git bisect start.

You know it's failing on stable, mark it as bad. git bisect bad stable.

You know it's passing on master, mark it as good: git bisect good master

Bisect will now select a commit between the two and check it out. Run your test. If it passes, git bisect good. It it fails, git bisect bad.

Bisect will pick another commit. Do the same thing.

Repeat until Git finds the commit where it started failing. Because it is doing a binary search, searching hundreds of commits should take about 20-30 commits. You can automate the process by writing a script to do the build and testing and passing it to git bisect run my_script <args>.

This depends on a single commit being the cause of the failure. If you have not been testing consistently during development there might be a newer commit which introduces the bug again (a regression). You may need to bisect again beginning with a newer "good" commit.

For more details, see the git-bisect documentation. They provide very clear instructions. Also this answer.

  •  Tags:  
  • Related