Sub topic 10
Resetting, logging, and replaying a session
This chapter assumes you can start a bisect session, mark commits good or bad, and run git bisect run with a script. Here you learn how to end a session cleanly, save its steps, and replay them later.
What a session changes in your repository
A bisect session is a temporary state. While it runs, Git detaches HEAD and checks out the commit it wants you to test. It also stores the session's marks inside the .git directory.
If you stop after the result appears, your working tree still sits on a commit from the middle of history. Any new commit you make there is not on a branch. This is the most common bisect mistake.
Ending a session with reset
git bisect reset ends the session. It deletes the stored marks and checks out the branch you were on when you ran git bisect start.
git bisect resetYou can also name a commit to land on instead of the original branch:
git bisect reset mainRun git status afterward. It should show a branch name, not "HEAD detached at ...".
Saving a session with the log
Git records every step of a session: the start, each good or bad mark, each skip, and each run. git bisect log prints that record as a list of commands.
git bisect log > bisect.logSave the log before you reset. After a reset, the record is gone from Git. A saved log is also useful as evidence. You can attach it to a bug report so a colleague can see which commits you tested and what you decided about each one.
Replaying a session
git bisect replay reads a log file and runs its steps again. It rebuilds the same session state.
git bisect log > bisect.log
git bisect reset
git bisect replay bisect.logAfter the replay, Git is back in the middle of the session, on the same commit it would have chosen.
Replay is useful in three situations:
- You marked a commit wrongly. Edit the log, delete or correct the bad line, and replay it. You keep all your other results.
- You want to continue on another machine. Copy the log and replay it there.
- You want to repeat the search with a corrected script. Replay the marks, then run
git bisect runagain.
The log is plain text, so editing it is safe. The lines starting with # are comments that record the commit subjects. Replay ignores them.
How the pieces fit together
The diagram shows the life of a session and where the log and replay fit.
Common errors
- Forgetting to reset. You commit on a detached
HEADand then lose track of the work. If this happens, rungit reflogto find the commit and create a branch from it. - Resetting before logging. The record is gone. If you need it, start again, or rebuild it from your shell history.
- Replaying into a dirty session. Run
git bisect resetfirst, then replay. This gives you a clean starting state. - Replaying a log on a different history. If the commits named in the log do not exist in the other repository, replay fails. Fetch the missing history first.
Exercise
Exercise
You ran git bisect run ./check.sh and Git printed the first bad commit. You want to keep a record of the session and then return to your branch. In which order do you run git bisect log > bisect.log and git bisect reset, and why?
Show answer
Run git bisect log > bisect.log first, then git bisect reset. Reset deletes the stored session marks, so a log taken afterward has nothing to record. Saving first also lets you rebuild the session later with git bisect replay bisect.log.
Exercise
You noticed that you marked one commit as good during a manual session, but it was actually bad. How can you fix the session without repeating every test?
Show answer
Save the session with git bisect log > bisect.log, run git bisect reset, and edit the file to correct or remove the wrong mark. Then run git bisect replay bisect.log. Git restores your other marks and continues from the corrected state.