← all lessons
Best Practices · lesson 12 of 18

Commit Hygiene

Write History Well

Why it matters

Commits are the documentation of your project’s evolution. Good commit hygiene makes debugging, reviewing, and understanding code dramatically easier.

Key concepts

The idea

The Book Chapter Analogy

Each commit should be like a book chapter:

The Atomic Commit

An atomic commit is the smallest change that makes sense on its own.

Bad:

"Update files"
- Fix login bug
- Add new feature
- Update README
- Refactor database

Good:

"Fix null pointer in login handler"
"Add password reset feature"
"Update README with new endpoints"
"Refactor database connection pooling"

Each can be reverted, cherry-picked, or understood independently.

Walkthrough

The Anatomy of a Good Commit Message

feat: add password reset functionality

Implement password reset flow with email verification.
Users can now request a password reset link that expires
after 24 hours.

- Add PasswordResetController
- Create email template
- Add expiration check middleware

Closes #234

Structure:

  1. Subject line: 50 chars max, imperative mood
  2. Blank line
  3. Body: Explain WHY, not what (the diff shows what)
  4. Footer: References, breaking changes

Conventional Commits

feat: add user authentication
fix: resolve race condition in queue
docs: update API documentation
style: format code with prettier
refactor: extract validation logic
test: add unit tests for auth
chore: update dependencies
perf: optimize database queries

What to Commit

✅ Commit❌ Don’t Commit
Source codeBuild artifacts (dist/, target/)
Config filesDependencies (node_modules/)
DocumentationSecrets (.env, credentials)
TestsIDE settings (.idea/, .vscode/)
.gitignoreLarge binaries

The .gitignore File

# Dependencies
node_modules/
vendor/

# Build artifacts
dist/
target/
*.pyc
__pycache__/

# Environment
.env
.env.local
*.pem

# IDE
.idea/
.vscode/
*.swp

# OS
.DS_Store
Thumbs.db

# Logs
*.log
logs/

Key takeaways

Dos & don’ts

✅ DO

❌ DON’T

Going deeper

Signed commits: git commit -S signs your commit with your GPG key. Proves you actually made the commit. GitHub shows “Verified” badge.

Commit hooks: Pre-commit hooks can enforce formatting, run tests, check for secrets. Tools: Husky (JS), pre-commit (Python), lefthook (Go).

Common mistakes

Committing secrets: Even if you remove them in the next commit, they’re in history forever. Use tools like git-secrets or gitleaks to prevent this.

Mega-commits: “Implement entire feature” commits are hard to review and revert. Break work into logical steps, commit each step.