Commit
_Git · Reference cheat sheet_
Commit
Git · Reference cheat sheet
📖 Overview
A commit records a snapshot of the staged index with metadata (author, message, parent). Prefer small, purposeful commits with clear messages; amend only when safe (unpushed, intentional).
🧩 Core concepts
- Snapshot model — commit points at a tree; parents form history.
- Message — subject (+ optional body); conventional prefixes (
feat:,fix:) are team conventions. - Amend —
git commit --amendrewrites the latest commit (changes hash). - Sign-off / GPG —
-s/-Sfor DCO or signed commits when required. - Empty commits —
--allow-emptyfor CI triggers or markers (rare). - Hooks —
pre-commit/commit-msgcan block or rewrite messages.
💡 Examples
# Commit staged changes
git commit -m "feat: add login form validation"
# Stage tracked files and commit
git commit -am "fix: handle null user in session"
# Amend message only
git commit --amend -m "feat: add login form validation"
# Amend and include newly staged files (unpushed only)
git add src/login.ts
git commit --amend --no-edit
# Show last commit
git log -1 --stat
git show HEAD
# Skip hooks only if you must (usually avoid)
# git commit --no-verify -m "wip"⚠️ Pitfalls
- Amending a commit already pushed requires force-push — coordinate with the team.
-aignores untracked files — new files still needgit add.- Huge commits are hard to review and revert — split by concern.
- Never put secrets in commit messages or blobs; rewriting history later is painful.