Stage Files with git add Before Commit/prek

lesson tools active tools/stage-files-before-commit.md View on GitHub

Stage Files with git add Before Commit/prek

Rule

Always git add <files> before committing or running prek. New files must be staged first; prek validates the staged version, not your working directory.

Context

When you're about to commit or run prek after editing files. If you skip staging, Git may reject new files and prek may validate stale content instead of the fixes you just made.

Detection

Pattern

# ❌ Wrong: Commit untracked file directly
git commit new-file.md -m "add new file"
# Error: pathspec 'new-file.md' did not match any file(s) known to git

# ❌ Wrong: Run prek without staging fixes
vim scripts/fix.py  # fix issues
prek run       # still shows old errors

# ✅ Correct: Stage first, then commit/prek
git add new-file.md && git commit new-file.md -m "add new file"
git add scripts/fix.py && prek run

Outcome

Related

Match Keywords

pathspec did not match nothing added to commit Stashing unstaged prek shows old errors commit untracked file