Sub topic 5
Where the script lives while Git changes the checkout
This chapter assumes you can run a manual git bisect session and that you know the exit code contract from earlier chapters: 0 means good, 1 to 127 except 125 means bad, 125 means skip, and 128 or higher aborts the session. You should also be comfortable with shell scripts and with your project's build and test commands.
The problem: the checkout changes under your script
git bisect run works by repeatedly checking out a commit from the history, running your command, and reading its exit status. Each checkout rewrites the files in your working tree to match that commit. This includes the file that holds your reproducer script, if the script is tracked by Git.
That causes three kinds of trouble:
- The script is missing. If you wrote
check.shonmainand committed it there, older commits may not contain it. Git removes it when it checks out an older commit, and the next step fails with "No such file or directory". The shell reports this as exit status 127, which Git reads as a bad commit. - The script is different. If an older commit contains an older
check.sh, you are no longer testing what you wrote. Each step may run a different version of the test. - Your edits are in the way. If the script is tracked and has uncommitted changes, Git may refuse to check out a commit that would overwrite it, or it may carry the changes along in a confusing way.
The first case is the most damaging, because it fails silently. Exit status 127 falls inside the "bad" range, so Git treats a missing script as a failing commit. Every step reports "bad", and the search converges on the oldest commit after the good endpoint. You then get a confident but meaningless answer.
The rule: keep the script outside the files Git manages
Put the script somewhere the checkout cannot touch. Two places work well:
- A directory outside the repository, such as
/tmpor~/bin. - An untracked and ignored location inside the repository, such as a directory listed in
.git/info/exclude.
The first option is simpler and is the one to prefer. Git never modifies files outside the working tree.
mkdir -p ~/bisect
cat > ~/bisect/check.sh <<'EOF'
#!/bin/sh
# check.sh: exit 0 = good, 1 = bad, 125 = cannot test this commit
make >/dev/null 2>&1 || exit 125
./run_regression_test.sh
EOF
chmod +x ~/bisect/check.shThen pass the full path to git bisect run:
git bisect start HEAD v1.4.0
git bisect run ~/bisect/check.sh
git bisect resetThe script runs with the repository root as its working directory. Its relative commands, such as make and ./run_regression_test.sh, therefore act on the commit that Git has just checked out. The script itself stays unchanged from step to step.
Why the working directory matters
Git runs the command from the top level of the working tree. You do not need to cd in the script. If you use relative paths in the script, they point at the files of the commit under test. Use absolute paths only for the script itself and for any helper files that must stay constant, such as a test input file.
#!/bin/sh
# ~/bisect/check.sh
# The input file lives outside the repo, so every commit sees the same data.
INPUT="$HOME/bisect/failing-input.json"
make >/dev/null 2>&1 || exit 125
./build/app --parse "$INPUT" >/dev/null 2>&1Here the reproducer input is a fixed file. If it were tracked and changed between commits, a failure might come from the changed input instead of the code you are searching.
Keeping the script inside the repository
Sometimes the script needs to live in the repository, for example because it uses repository-relative helpers or because your team wants to share it. You can still keep it away from the checkout.
Add the path to the local exclude file, which works like .gitignore but is never committed:
mkdir -p .bisect
cp ~/draft-check.sh .bisect/check.sh
chmod +x .bisect/check.sh
echo ".bisect/" >> .git/info/exclude
git status --shortBecause .bisect/ is ignored, Git does not track it. Checking out a different commit leaves the directory in place. The git status command should print nothing for it, which confirms that Git ignores it.
There is one catch. If some old commit tracks files under .bisect/, a checkout of that commit could conflict with your files. Pick a name that no commit in the range has used. You can check with git log --all --oneline -- .bisect | head. An empty result means the name is safe.
The script and the build output
The checkout also affects files your script generates. A build leaves objects, binaries, and caches in the working tree. When Git moves to another commit, some of these stay, because they are untracked or ignored. That can cause two problems:
- Stale output. A build tool may reuse an object file from a different commit and produce a binary that matches no commit. Most build tools compare timestamps, and a checkout updates the timestamps of the files it changes, so incremental builds usually behave. A tool that hashes content or uses a separate cache can still give you old results.
- Wasted time. A full rebuild at every step is slow. An incremental build is fast but riskier.
Choose the trade-off on purpose. If you suspect stale output, make the script clean first:
#!/bin/sh
# ~/bisect/check-clean.sh
git clean -fdxq -e .bisect/ || exit 125
make >/dev/null 2>&1 || exit 125
./run_regression_test.shDoing the search in a separate worktree
A linked worktree gives the bisect its own working directory and its own checked-out state, while it shares the repository history. Your main working copy, with its uncommitted work, stays untouched.
git worktree add ../bisect-work HEAD
cd ../bisect-work
git bisect start HEAD v1.4.0
git bisect run ~/bisect/check.sh
git bisect reset
cd -
git worktree remove ../bisect-workBisect state is stored per worktree, so a session in ../bisect-work does not interfere with one in the main directory. This is the safest setup when you have uncommitted changes, when builds are slow and you want to keep a warm build tree in the main copy, or when you want to run git clean freely.
Local changes and the starting state
A bisect session checks out commits, so it needs a clean enough working tree. If you have uncommitted changes to tracked files, a checkout may fail when the target commit also changes those files. Then git bisect run stops with an error instead of finishing.
You have three options:
- Commit the work on a temporary branch.
- Run
git stashbefore the session andgit stash popaftergit bisect reset. - Use a separate worktree, as shown above.
The worktree is usually the cleanest choice because it avoids restoring state by hand.
Summary of the choices
| Location of the script | Survives checkouts | Risk | Best for |
|---|---|---|---|
| Tracked file in the repository | No | Missing or changed at old commits, silent exit 127 | Nothing during a bisect |
| Directory outside the repository | Yes | None from Git; you must remember the full path | Most cases |
| Ignored directory inside the repository | Yes | Name clash with a tracked path in old commits | Scripts that need repository-relative helpers |
| Copy in a separate worktree | Yes | Extra disk space | Dirty working tree, slow builds, or use of `git clean` |
How the pieces fit together
Common errors
- Writing
git bisect run ./check.shwith a trackedcheck.sh. The path is relative to the checkout, so the file can vanish or change. Use a path outside the tree. - Using
~inside quotes, where the shell does not expand it. Write"$HOME/bisect/check.sh"when you quote the path. - Forgetting
chmod +x. A script without the execute bit fails with status 126 ("permission denied"), which Git counts as bad. You can also run it withsh /path/check.shto avoid the issue. - Using a relative path to a helper file outside the repository. The working directory is the repository root, so use an absolute path.
- Leaving the session open. After the search, run
git bisect resetto return to your original branch. Do this before you remove a worktree or delete the script.
Exercise
You wrote check.sh, committed it on main, and ran git bisect run ./check.sh with v1.0 as the good commit. The search finished in two steps and named the commit right after v1.0 as the first bad commit. The script did not exist at v1.0. Why is this result unreliable, and what two changes would fix the setup?
Show answer
When Git checked out older commits, check.sh was not in the working tree, so the shell failed with "No such file or directory" and exit status 127. Git counts 127 as bad, so every step looked bad and the search converged on the oldest commit in range. The first fix is to move the script outside the repository, for example to ~/bisect/check.sh, and run it by its full path. The second is to validate the script on the good and bad endpoints before starting, so a result of 127 on the good commit exposes the problem. Running the search in a separate worktree is an additional safeguard.
Exercise
Why does the chapter recommend .git/info/exclude over .gitignore for hiding a .bisect/ directory inside the repository?
Show answer
.gitignore is a tracked file that is part of the history, so editing it changes the working tree and can differ between commits. .git/info/exclude is stored inside the .git directory, is never committed, and does not change when Git checks out other commits. The ignore rule therefore applies at every step of the bisect.