Search code examples
gitgit-bisect

What's the best way to change the working tree to a specific commit before bisecting?


My usual workflow when starting a git bisect session is:

git bisect start          # start a bisect session
git bisect bad            # I noticed that the current state is broken

Now, often I know that things worked in the past, but I don't know yet which version was good, so I usually want to revert my working tree to an older state, build, test, repeat until I find a working version which I can mark with git bisect good.

What's the best way to do the "revert my working tree to an older state" step? git reset --hard <some_good_rev>? git checkout <some_good_rev>? Something else? Please justify your answer.


Solution

  • I usually don't know where the good rev is, so I've got to find it. Guess how far back might be good. Let's say, 32 revs. Starting from the tip of a branch, and with clean working directories:

    $ git checkout HEAD~32
    

    Run the test. If it's a good rev, start bisecting. If it's not, go back some more:

    $ git checkout HEAD~32
    

    Rinse, lather, repeat.

    Why "git checkout?" Because "git reset" will change which commit the branch points to as its "tip", but git checkout will not.