What is a Commit in Git?
Understand what a Git commit is, how it stores an immutable snapshot with a SHA-1 hash, and how staging, commit messages, and history all fit together.
Expected Interview Answer
A Git commit is a permanent, immutable snapshot of the entire staged project state at a point in time, saved with a unique SHA-1 hash, an author, a timestamp, and a message describing what changed.
When you run `git commit`, Git takes everything currently in the staging area and packages it into a new commit object that points to a tree of file snapshots and to its parent commit (or commits, for a merge). Because each commit's hash is derived from its content and its parent's hash, commits form a tamper-evident, append-only chain — changing any commit changes its hash and every descendant hash after it. Commits are the atomic unit of history in Git: branches are just movable pointers to a commit, and operations like `log`, `diff`, `revert`, and `cherry-pick` all operate on commits. Good commit hygiene, small logical commits with clear messages, makes history genuinely useful rather than just a record.
- Creates a permanent, restorable checkpoint of the project
- Enables precise history review with `git log` and `git diff`
- Forms the basis for branching, merging, and reverting
- Immutable hash chain protects history from silent tampering
- Small, focused commits make code review and bisecting easier
AI Mentor Explanation
A commit is like officially closing out an over in the scorebook, locking in every ball bowled so it can never be secretly rewritten later. Once the umpire signs off, that over's record stands permanently, and any later dispute is resolved by reading back to that exact entry rather than guessing.
Step-by-Step Explanation
Step 1
Stage the changes
`git add <file>` moves edits from the working directory into the staging area, choosing exactly what will be included in the next commit.
Step 2
Create the commit
`git commit -m "message"` packages the staged content into a new commit object with a tree, parent pointer, author, timestamp, and message.
Step 3
Git computes the hash
The commit's SHA-1 hash is derived from its content and its parent's hash, so any later change to history produces a different hash chain.
Step 4
The branch pointer advances
The current branch (e.g. `main`) is updated to point at the new commit, and HEAD follows the branch unless you're in detached HEAD state.
Step 5
Inspect and reference it
`git log`, `git show <hash>`, and `git diff <hash1> <hash2>` let you inspect any commit; other commands like `cherry-pick` and `revert` operate on commit hashes directly.
What Interviewer Expects
- Describes a commit as an immutable snapshot, not a diff
- Knows a commit stores a tree, parent pointer, author, and message
- Understands commits are content-addressed via SHA-1
- Can explain the staging step that precedes a commit
- Recognizes branches as pointers to commits, not containers of commits
Common Mistakes
- Calling a commit a 'save' with no understanding of its structure
- Believing commits store only the changed lines rather than a full tree snapshot
- Forgetting that editing a past commit changes its hash and all descendants
- Skipping staging and assuming `git commit` grabs all working directory changes
- Writing vague commit messages like 'fix stuff' that make history useless
Best Answer (HR Friendly)
“A commit is like saving a labeled checkpoint of your work — it captures exactly what the project looked like at that moment, along with a note explaining what changed. Developers build a project's history one commit at a time, which makes it easy to track progress or undo a mistake.”
Code Example
$ git add src/app.py
$ git commit -m "Add health check endpoint"
[main 7a1c3de] Add health check endpoint
1 file changed, 12 insertions(+)
$ git show --stat 7a1c3de
commit 7a1c3de9f0...
Author: Jane Doe <[email protected]>
Date: Sun Jul 26 10:04:11 2026 +0000
Add health check endpoint
src/app.py | 12 ++++++++++++
1 file changed, 12 insertions(+)Follow-up Questions
- What information is stored inside a commit object?
- How does Git compute a commit's SHA-1 hash?
- What is the difference between `git commit` and `git commit --amend`?
- How do merge commits differ from regular commits?
- What makes a good commit message?
MCQ Practice
1. What does a Git commit represent?
A commit is a permanent, immutable snapshot of everything in the staging area at the time it was created.
2. What uniquely identifies a commit?
Every commit gets a SHA-1 hash computed from its tree, parent, author, timestamp, and message, making it uniquely identifiable and tamper-evident.
3. What step must happen before you can create a commit?
Changes must be staged (added to the index) with `git add` before `git commit` can turn them into a snapshot.
Flash Cards
What is a Git commit? — An immutable snapshot of the staged project state, identified by a SHA-1 hash and linked to a parent commit.
What does a commit object store? — A tree of file snapshots, a parent pointer, author/committer info, a timestamp, and a message.
Can you edit an old commit in place? — No — editing it produces a new hash, which changes it and every commit that came after it in the chain.
What must happen before committing? — Changes must be staged with `git add`, since `git commit` only captures what is in the staging area.