Sub topic 5
Where the script lives while Git changes the checkout
This chapter assumes you can write a basic shell script, know that git checkout rewrites the files in your working tree, and understand the exit code contract from the previous chapter (0 good, 1 to 127 bad except 125, 125 skip, 128 and above abort).
The problem: the checkout moves under your script
Each step of a bisect checks out a different commit. Git rewrites tracked files to match that commit. If your reproducer script is a tracked file, Git can change it or delete it mid-search.
Suppose you added check.sh to the repository last week and committed it. Bisect then checks out a commit from two months ago. That commit has no check.sh, so the file disappears. Git then runs ./check.sh, gets "No such file or directory", and the shell exits with code 127. Git reads 127 as "bad". Every step now looks bad, and bisect reports a wrong commit with full confidence.
An older version of the script is a quieter failure. The script exists at that commit but contains different logic, so you test a different thing at each step.
Three safe places for the script
You have three options.
- Outside the repository. Put the script in a directory such as
~/binor/tmpand call it by absolute path. Git never touches files outside the working tree. - Untracked and ignored. Keep the file inside the repository but never commit it, for example by listing it in
.git/info/exclude. Git leaves untracked files alone when it switches commits, unless an older commit tracks a file with the same path. In that case the checkout can fail or overwrite it. - Inline with
sh -c. Pass the check on the command line, so no file exists at all.
Option 1 is the most reliable, so use it by default.
mkdir -p ~/bisect
cp check.sh ~/bisect/check.sh
chmod +x ~/bisect/check.sh
git rm --cached check.sh # only if you had committed itThen start the search with the absolute path:
git bisect start HEAD v1.4.0
git bisect run ~/bisect/check.shPaths inside the script
Git runs the script from the top of the working tree. The script itself sits elsewhere, so avoid paths relative to its own location. Use paths relative to the repository, such as make or ./run_regression_test.sh. These files belong to the commit under test, which is what you want.
Do not copy the regression test into your external script folder. If the test lives in the repository, each commit should run its own version of it. If the test did not exist in old commits, keep a standalone copy outside the repository and call it from the script.
#!/bin/sh
# ~/bisect/check.sh
make >/dev/null 2>&1 || exit 125
~/bisect/standalone_test.shThe inline alternative
For short checks, sh -c avoids the question entirely. The quoted string is part of the command, so nothing in the working tree can replace it.
git bisect run sh -c 'make || exit 125; ./run_regression_test.sh'Once the check grows past one or two lines, move it to a file outside the repository. Long quoted strings are easy to get wrong.
How the pieces fit together
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Every commit reported bad, first bad commit is very old | Script missing at older commits, exit 127 | Move script outside the repository |
| `Permission denied` on every step | Script lacks the execute bit (exit 126) | Run `chmod +x` once |
| Checkout fails with "untracked working tree files would be overwritten" | Older commit tracks a file at your script's path | Use a path outside the repository |
| Results differ from running the script by hand | A different script version was checked out | Use one external copy |
Exercise
Your bisect ends by blaming a commit from the first month of the project, and you suspect the script. The script check.sh is committed in the repository. What probably happened, and how do you fix it?
Show answer
Commits older than the one that added check.sh do not contain the file, so running it fails with exit code 127, which Git treats as bad. Bisect then moves toward the oldest commits. Copy the script outside the repository, for example to ~/bisect/check.sh, make it executable, and run git bisect run with that absolute path.