Version control · foundation

Git commit

A Git commit is an immutable history object that records a project tree, parent commit references, author and committer metadata, and a message; its object ID is derived from its content.

Why it matters

Understanding commits as snapshots in a parent graph clarifies staging, history rewriting, merging, reverting, and why amending produces a different commit rather than editing one in place.

Mental model

How to reason about git commit

The working tree is what you edit, the index is the proposed next snapshot, and commit writes that indexed snapshot with links to its parent history.

Analogy

The index is a tray of pages selected for publication. Committing binds exactly that tray into a numbered edition that cites the previous edition as its parent.

Examples

See the boundary, not just the happy path

Worked example · Commit selected changes

git add src/parser.ts && git commit -m "Handle quoted fields"

The commit includes the entire index: the parser change plus anything staged earlier. Unstaged edits stay out, so review git diff --staged first.

Worked example · Inspect the object

git show --stat HEAD

HEAD resolves to a commit whose comparison with its parent is displayed, even though the commit itself records a full tree snapshot.

Useful contrast · Amend rewrites identity

git commit --amend

Changing the snapshot, metadata, message, or parent creates a new object ID; collaborators may still reference the old commit.

Common mistakes

Misconceptions to remove early

Thinking a commit stores only a patch

Git can display parent-to-child differences efficiently, but the commit points to a complete project tree rather than defining itself as a diff.

Committing without reviewing the index

The index can differ from both the working tree and HEAD. Review git diff --staged so the commit boundary matches the intended change.

Quick check

Can you predict the result?

1. Which state supplies the file snapshot for a normal git commit?
  • The index, also called the staging area
  • Every untracked file on disk
  • Only the most recently edited file
Answer: The index, also called the staging area
2. Why does amending a commit change its object ID?
Answer: A commit's identity is derived from its tree, parents, metadata, and message, so changed content creates a new immutable object.

Keep building

Authoritative references

Make the idea retrievable.

Study this concept with spaced repetition, next to the commands where you use it.

Get Terminaster