What is a Git commit and what does it contain?
Model answer
A commit is an immutable snapshot of the whole project at one moment, plus the metadata that explains it. It is not a diff: Git stores the full state of the tree and computes differences when you ask.
Each commit holds a pointer to a tree (the directory snapshot), the parent commit hash (two for a merge, none for the first commit), the author and committer name, email and timestamp, and the commit message. Git hashes all of that into a unique SHA that identifies the commit.
git log -1 --format='%H %P %an %s'
git cat-file -p HEAD
Because the hash covers the parent hash too, each commit seals the history before it. Change any old commit and every later hash changes.
Branches and tags are only labels that point at a commit. Commit small, coherent changes with a clear message, because the commit is the unit you review, revert and cherry-pick.
Key points
- A commit is a full snapshot, not a diff
- Holds tree, parent hash, author, committer, message
- The SHA hash identifies the commit and covers its parents
- Branches and tags are just pointers to commits
Likely follow-ups
- Why does amending a commit give it a new hash?
- What is the difference between author and committer?
Common mistakes
- Believing a commit stores only the diff
- Thinking a branch is a copy of the files