Sub topic 10
Resetting, logging, and replaying a session
This chapter assumes you have run a bisect with git bisect run and have a reproducer script that follows the exit code contract. It covers what a bisect session leaves in your repository, how to end it cleanly, and how to save and restore it.
What a session leaves behind
A bisect session changes your repository in three ways:
- Git detaches
HEADand checks out a different commit at every step. - Git records your answers (good, bad, skip) as refs under
refs/bisect/. - Git writes bookkeeping files inside
.git, such asBISECT_START(where you began),BISECT_LOG(every answer so far), andBISECT_TERMS(the words you are using for old and new).
This state lives on disk, so closing your terminal or rebooting does not end the session. Running git bisect log or git status later shows you are still in it. The state is stored per worktree, so a bisect in one linked worktree does not disturb another.
The session also ends in a surprising place. When git bisect run prints the first bad commit, your working tree is still checked out at that commit, on a detached HEAD. You are not back on your branch. You need to end the session yourself.
The lifecycle of a session
The diagram shows the states a session moves through and the commands that move it.
The next three sections cover reset, log, and replay.
Ending a session with reset
git bisect reset ends the session. It does three things:
- Checks out the commit or branch you were on when you ran
git bisect start, using the value saved inBISECT_START. - Deletes the
refs/bisect/refs. - Removes the bisect bookkeeping files.
git bisect resetIf you started on main, you land on main. If you started on a detached HEAD, you land on that same commit. You can also choose where to land by passing a commit:
git bisect reset v1.4.0This ends the session and checks out v1.4.0 instead of your starting point. Use it when you want to stay near the culprit and not go back to where you began.
Reset is safe to run at any time, including halfway through a search. It only discards the bisect state. It does not delete commits or branches.
Uncommitted changes
If your working tree has uncommitted changes that conflict with the target commit, the checkout in git bisect reset refuses to run. The same problem can stop bisect from moving to the next commit during the search. Before you start, commit or stash your work:
git stash push --include-untracked -m "before bisect"Build output is a common cause of this. If your check.sh runs make and the build writes tracked files, those changes can block the next checkout. A script that builds out of tree, or that runs git checkout -- . on a tracked build directory, avoids this.
Starting over
Start a new investigation from a clean state. If you are unsure whether a session is active, run git bisect reset first. When no session exists, it prints a message and does nothing harmful.
Saving a session with log
git bisect log prints every step of the current session. Redirect it to a file to save it:
git bisect log > bisect.logThe log looks roughly like this. The exact comment lines vary between Git versions, so treat this as an abridged example:
git bisect start 'HEAD' 'v1.4.0'
# bad: [9f3c2a1d] Merge branch 'feature/export'
git bisect bad 9f3c2a1d4e5b6a7c8d9e0f1a2b3c4d5e6f708192
# good: [51ab07c2] Release 1.4.0
git bisect good 51ab07c2a1b2c3d4e5f60718293a4b5c6d7e8f90
# bad: [c0de1234] Cache parsed config
git bisect bad c0de12345678901234567890abcdef1234567890
# first bad commit: [c0de1234] Cache parsed configLines that start with # are comments that record the commit subject for readers. The other lines are the commands you would type, with full commit IDs. This matters for two reasons:
- The log is a record of why Git ended up where it did. Attach it to a bug report, and a colleague can see which commits you marked and in what order.
- The log is a script. Git can run it again.
Save the log outside the repository, for example in ~/bisect-logs/. A file inside the working tree can get in the way of checkouts, or end up in a commit by accident.
You can run git bisect log at any point, not only at the end. Saving it before a risky step costs nothing. A common habit is to save the log just before git bisect reset, because reset deletes the record.
Restoring a session with replay
git bisect replay reads a log file and re-applies each command in order:
git bisect log > bisect.log
git bisect reset
git bisect replay bisect.logAfter the replay, you are in the same state as before: the same marked commits, the same checked-out commit, the same remaining range. Replay clears any existing session first, so you do not need to reset before it, though the sequence above shows both steps for clarity.
Replay needs every commit in the log to exist in your repository. If a colleague sends you a log, fetch their branches first. Otherwise Git fails when it reaches a commit ID it cannot find.
Fixing a wrong answer
Everyone eventually marks a commit wrong, or fixes check.sh halfway through and realizes earlier answers came from the broken script. Git has no command to undo one step, but replay gives you a way to do it:
- Save the log with
git bisect log > bisect.log. - Open
bisect.logand delete the line with the wrong answer. Delete thegit bisect goodorgit bisect badline, along with its#comment line if you like. Leave thegit bisect startline alone. - Run
git bisect reset, thengit bisect replay bisect.log. - Continue with
git bisect run ./check.sh, or answer by hand.
git bisect log > bisect.log
# edit bisect.log: remove the line that recorded the wrong answer
git bisect reset
git bisect replay bisect.log
git bisect run "$HOME/bin/check.sh"Replay keeps all the answers you trust, so you do not redo the early steps. This is useful when each test takes many minutes.
If the wrong answer was early in the log, the later answers may depend on it. Git picks each next commit from the answers so far, and the commits tested after the mistake were chosen on a bad assumption. Delete the wrong line and every line after it, then resume the search.
Resuming an interrupted run
You do not need replay to resume after an interruption. If you stop git bisect run with Ctrl-C, or your script exits with 128 or above to abort the run, the session stays active. Fix the problem, then run the same git bisect run command again. Git continues from the current state and does not start over.
A wrapper that always cleans up
A script that runs the whole hunt and saves the log is a safe habit. It makes sure you never forget to reset:
#!/bin/sh
# hunt.sh: run a bisect, keep the log, always return to the starting point.
set -eu
good_ref="$1"
check="$HOME/bin/check.sh"
logdir="$HOME/bisect-logs"
mkdir -p "$logdir"
log="$logdir/$(date +%Y%m%d-%H%M%S).log"
git bisect start HEAD "$good_ref"
status=0
git bisect run "$check" || status=$?
git bisect log > "$log"
git bisect reset
echo "log saved to $log"
exit "$status"Notes on this script:
check.shlives in$HOME/bin, outside the repository, so it does not change when Git swaps the checkout.status=0and|| status=$?stopset -efrom exiting before the log is saved when the run fails.- The log is written before the reset, because reset deletes the session.
- The script exits with the exit status of
git bisect run, so a caller can tell whether the run finished.
The first bad commit is also in the log, on the last # first bad commit: line. To see it later without a live session, search the saved file:
grep '^# first bad commit' ~/bisect-logs/*.logCommand summary
| Command | Effect on the session | Typical use |
|---|---|---|
| `git bisect log` | None. Prints the steps so far. | Save progress, attach to a report |
| `git bisect reset` | Ends it. Checks out the starting point. | Return to your branch after the result |
| `git bisect reset <commit>` | Ends it. Checks out `<commit>`. | Stay near the culprit |
| `git bisect replay <file>` | Clears the session, then re-applies the file. | Resume, share, or repair a session |
| `git bisect run <cmd>` | Continues the active session. | Resume after an interruption |
Common mistakes
- Forgetting to reset. You keep working on a detached
HEAD. Rungit statusif the branch name is missing from your prompt. - Resetting before saving the log. The record is gone. Save first, reset second.
- Replaying with missing commits. Fetch the right branches before you replay a log from someone else.
- Committing during the session. Those commits are on a detached
HEADand are easy to lose. - Editing the log but keeping later lines. Answers after a corrected line may rest on the mistake.
Exercise
You ran git bisect run ./check.sh and the output named a first bad commit. You want to keep a record of the session and return to your feature branch. Which two commands do you run, and in what order? What happens if you swap them?
Show answer
Run git bisect log > bisect.log, then git bisect reset. If you reset first, the session state is deleted, so git bisect log has nothing to print and the record is lost. Save the log before you reset.
Exercise
Halfway through a session you realize check.sh had a typo, and the third answer you gave (a git bisect good line) was wrong. How do you repair the session without redoing the first two steps?
Show answer
Save the log with git bisect log > bisect.log. Edit the file and delete the wrong git bisect good line and every answer line after it, because those commits were chosen using the bad answer. Run git bisect reset, then git bisect replay bisect.log to restore the first two trusted answers. Fix check.sh, then continue with git bisect run.