Sub topic 5
Checking what is actually being committed
This chapter assumes you know the difference between the staging area (the index) and the working tree, and that you can write a short shell script. It builds on the pre-commit hook from the previous chapter.
The problem: two copies of every file
When you run git add on part of a file, the index holds one version and your working tree holds another. A pre-commit hook runs before the commit is created, and the commit records the index. If your hook reads files from disk, it checks the working tree. That is not what will be committed.
This causes two kinds of error:
- A false pass. You fix a problem in the working tree but forget to stage the fix. The hook reads the clean file on disk and passes, but the broken staged version is committed.
- A false failure. You have an unrelated, unstaged problem in the same file. The hook reports it and blocks a commit that is actually fine.
Asking Git about the index
Many Git commands have a --cached (or --staged) option that reads the index instead of the working tree. The simplest hook you saw earlier uses it:
#!/bin/sh
# .githooks/pre-commit
# Reject the commit if the staged changes contain whitespace errors or conflict markers.
if ! git diff --cached --check; then
echo "pre-commit: fix the problems above or commit with --no-verify." >&2
exit 1
fi
exit 0git diff --cached compares the index with HEAD, so it shows exactly what the commit will add. With --check, Git exits non-zero when it finds whitespace errors or leftover conflict markers in those changes. The hook only inspects lines you are about to commit.
Listing the staged files
To run your own tool, you first need the list of files. Use --name-only and filter by change type:
git diff --cached --name-only --diff-filter=ACM--diff-filter=ACM keeps files that were Added, Copied or Modified. It drops Deleted files, which no longer exist and would make a linter fail with "file not found". You can add a pathspec such as -- '*.sh' to limit the list to one file type.
Reading the staged content
A file name alone is not enough. If you pass the name to a linter, the linter opens the working tree copy. To check the staged version, read it from the index with git show ":path". The leading colon means "the version in the index".
#!/bin/sh
# .githooks/pre-commit
# Run shellcheck on the staged version of each added, copied or modified .sh file.
status=0
for f in $(git diff --cached --name-only --diff-filter=ACM -- '*.sh'); do
git show ":$f" | shellcheck - || status=1
done
exit $statusHow it works:
git diff --cached --name-onlylists the staged.shfiles.git show ":$f"prints the staged content of each file.shellcheck -reads that content from standard input.- The
|| status=1records a failure but lets the loop continue, so the developer sees every problem in one run. - The hook exits with
$status, so one failing file blocks the commit.
Why not just check the working tree?
Sometimes a tool cannot read from standard input and needs a real file on disk. You have three common options:
| Approach | What it does | Trade-off |
|---|---|---|
| Read from the index with `git show` | Checks exactly what is staged | Needs a tool that accepts standard input |
| Export the index to a temporary directory | Gives tools real files that match the index | Slower, and you must clean up the directory |
| Stash unstaged changes first | Makes the working tree match the index | Risky if the hook fails midway and leaves a stash behind |
Hook managers such as the pre-commit framework automate the stash approach, which is one reason people choose them. The later chapter on hook managers covers that.
Common mistakes
- Running
eslint src/orpytestin a hook. These read the working tree and check every file, not only staged ones. - Forgetting
--diff-filter, so deleted files are passed to a tool. - Using
git diffwithout--cached. That compares the working tree with the index, so it shows only your unstaged changes, the opposite of what you want. - Auto-fixing a file in the hook and not re-staging it. The fix lands in the working tree, but the commit still holds the old content.
Exercise
A developer stages a file with a lint error, then fixes the error in the editor without running git add again. Their pre-commit hook runs shellcheck script.sh on the file path. What happens, and how do you change the hook to avoid it?
Show answer
The hook passes, because shellcheck reads the fixed working tree copy. The commit still contains the staged version with the error. Change the hook to read the staged content with git show ":script.sh" | shellcheck -, so the check runs on the index version and fails as it should.
Exercise
Why does the loop in the shellcheck hook use || status=1 instead of calling exit 1 on the first failure?
Show answer
With || status=1, the loop checks every staged file and reports all failures in one run. The hook then exits with a non-zero status at the end. Calling exit 1 immediately would stop at the first failing file and hide the rest.