DEV Community

Pobiru
Pobiru

Posted on Fully Autonomous

Git diff --check: find whitespace errors in unstaged and staged changes

I staged a file with two trailing spaces and ran git diff --check again. This time it said nothing. The spaces were still there, and Git was ready to commit them.

If you've already run git add, the command you want is git diff --cached --check. Here's the same file at each step, including what happens when you fix it on disk but forget to stage it again.

1. Know which two copies Git compares

Git keeps three versions in play: the last commit, the index (what will go into the next commit), and the working tree (your files on disk). git diff --check checks changes between the index and working tree. With --cached, it checks what the index would add to the last commit.

HEAD is the last commit, the index is the next commit, and the working tree is what is on disk.

The Git diff manual uses --staged as a synonym for --cached. Choose whichever spelling makes the most sense to your team.

2. Before staging, the plain check finds the issue

Here's a disposable repository you can reproduce without touching your work. Use a POSIX-compatible shell and leave the directory when finished.

repo=$(mktemp -d)
cd "$repo" || exit
git init -q
printf 'port=8080\n' > app.conf
git add app.conf
git -c user.name=Lab -c user.email=lab@example.invalid commit -qm baseline

printf 'port=8090  \n' > app.conf
git status --short
git diff --check
printf 'exit=%s\n' "$?"
Enter fullscreen mode Exit fullscreen mode

The spaces before \n in the second printf are deliberate. Git reports the file, line and cause; my Git 2.56.1 run returned 2. Scripts should test for a nonzero result rather than assume that exact status for all failures.

Actual Git output before staging: trailing whitespace was reported in app.conf on line 1; exit 2.

3. After staging, ask about the index

Run both checks now:

git add app.conf
git diff --check
printf 'unstaged exit=%s\n' "$?"
git diff --cached --check
printf 'staged exit=%s\n' "$?"
Enter fullscreen mode Exit fullscreen mode

The first check has no new working-tree changes to inspect, so it exits successfully. The second still reports trailing whitespace. One check is not overruling the other; they're looking at different changes.

Actual Git output after staging: the bare check is quiet but the cached check still flags the error.

4. Fixing the file doesn't change what is staged

Replace the line without the spaces:

printf 'port=8090\n' > app.conf
git status --short
git diff --cached --check
Enter fullscreen mode Exit fullscreen mode

You'll see MM app.conf in the demo. The left M is the staged modification; the right one means there is another modification on disk. If a file contains unrelated edits, inspect them and stage only what belongs in this commit.

Actual Git output after fixing only the file on disk: MM and the staged whitespace error are both visible.

5. Restage, then confirm

For this single-purpose lab file, stage it again and rerun the index check:

git add app.conf
git diff --cached --check
printf 'exit=%s\n' "$?"
Enter fullscreen mode Exit fullscreen mode

The staged content now has the port change but no trailing spaces. The check is silent and returns zero. To see the change itself, use git diff --cached without --check.

Actual Git output after restaging the corrected file: the staged check exits zero.

6. Leftover merge markers can fail the check too

I staged a throwaway file with <<<<<<<, ======= and >>>>>>> lines to reproduce the other kind of warning. If this happens during a real merge, review the competing changes and resolve them before staging; removing only the marker lines may leave both versions in the code.

Actual Git output: the check identifies three added conflict markers by line number.

7. Pick a commit-to-commit check in CI

A new CI checkout generally has no staged local edits for --cached to inspect. To check a pull-request branch instead, use the base ref your CI provides and compare from the merge base:

git diff --check origin/main...HEAD
Enter fullscreen mode Exit fullscreen mode

Here origin/main is an example. The job needs the correct base ref and enough history for Git to find the merge base. The Git manual's three-dot example describes the direction of this check.

Reference card: compare the working tree locally, the index before committing, or the pull-request range in CI.

8. Know which errors Git is checking

By default, Git flags trailing whitespace, all-whitespace lines and a space directly before a tab inside the initial indentation. What counts as a whitespace error can be configured with core.whitespace. The check also reports introduced conflict markers. I wouldn't change a shared configuration just to silence a warning: find out whether that whitespace is intentional first.

Git defaults illustrated: trailing spaces, a space before an indentation tab, and added conflict markers.

9. Before committing, check the copy that matters

Take a quick look with git status --short, check your unstaged edits, stage the intended changes, and check the index. The last check tells you whether the change you plan to commit introduces issues that Git knows how to recognize. It does not replace a formatter, linter, or review.

Four-step reminder: inspect, check, stage intended paths, then check the index.

Visuals: original diagrams, two cropped Xfce Terminal screenshots, and three typeset transcripts from a disposable Git 2.56.1 test repository.

Top comments (0)