The Hook (The "Byte-Sized" Intro)
A commit isn't "saving a file" — it's taking a photograph of your entire project at one moment in time. Every commit has a unique fingerprint (SHA-1 hash), an author, a timestamp, and a message. Years from now, you can time-travel back to any commit and see exactly what the project looked like. That's not a file save. That's a superpower.
📖 What is git commit?
git commit takes everything in the staging area and saves it as a permanent, immutable snapshot in your repository's history. Each commit records what changed, who changed it, when it changed, and why (via the message).
Conceptual Clarity
- A commit is a snapshot, not a diff — Git stores the complete state of every tracked file at that moment
- Each commit has a unique SHA-1 hash (e.g.,
a1b2c3d) generated from its contents - Commits form a chain — every commit points to its parent(s), creating a timeline
- The first commit in a repo is called the "root commit" (no parent)
- Commits are immutable — once created, they cannot be silently changed
What a commit object contains:
- A pointer to a tree object (the snapshot of files)
- A pointer to the parent commit(s)
- The author (name + email + timestamp)
- The committer (can differ from author in patch workflows)
- The commit message
Real-Life Analogy
Think of a commit like a frame in a film reel:
- Each frame captures the entire scene, not just the changes from the last frame
- Frames are numbered sequentially (commit hashes)
- You can jump to any frame and see the full picture
- You can't secretly change a frame without everyone noticing (integrity)
Visual Architecture
Why It Matters
- Time travel: Any commit can be revisited —
git checkout <hash>takes you back in time. - Accountability: Every commit is signed with an author and timestamp.
- Undo capability: Small, focused commits make
git revertprecise and safe. - Collaboration: Commits are the unit of sharing —
git pushsends commits,git pullreceives them.
Code
# ─── Basic commit ───
git add app.js
git commit -m "Add main application entry point"
# ─── Commit with a multi-line message ───
git commit -m "Add user authentication
- Implement login/logout endpoints
- Add JWT token validation
- Create auth middleware"
# ─── Skip staging: commit all tracked modified files ───
git commit -a -m "Fix typo in README"
# (-a automatically stages tracked, modified files — skips git add)
# ─── Commit with verbose diff (see changes while writing message) ───
git commit -v
# Opens editor showing the full diff alongside the message prompt
# ─── See the contents of the latest commit ───
git show
# Output: Shows the commit metadata + diff
# ─── See a specific commit ───
git show a1b2c3d
# ─── Inspect the commit object itself ───
git cat-file -p HEAD
# Output:
# tree f8d9e1a...
# parent e4f5g6h...
# author Nikhil <nikhil@example.com> 1706000000 +0000
# committer Nikhil <nikhil@example.com> 1706000000 +0000
#
# Add main application entry pointCommit Message Best Practices
| Rule | Example |
|---|---|
| Use imperative mood | "Add feature" not "Added feature" |
| Keep subject ≤ 50 chars | "Fix login redirect on timeout" |
| Capitalize the subject | "Add" not "add" |
| Don't end subject with a period | "Fix bug" not "Fix bug." |
| Separate subject from body with blank line | Subject → blank line → details |
| Explain why, not just what | "Fix race condition in auth — tokens expired before refresh" |
Key Takeaways
git commitcreates an immutable snapshot of everything in the staging area.- Each commit has a unique SHA-1 hash, author, timestamp, and message.
- Commits form a chain — each points to its parent, creating a traceable timeline.
- Write commit messages in imperative mood and explain the why.
- Use
-aflag carefully — it skips the staging area for tracked files.
Interview Prep
-
Q: What does a Git commit object contain? A: A commit object contains a pointer to a tree object (the file snapshot), a pointer to parent commit(s), the author and committer information (name, email, timestamp), and the commit message.
-
Q: Is a commit a diff or a snapshot? A: A commit is a snapshot — it records the complete state of every tracked file. Git may display commits as diffs for convenience, but internally each commit stores a full tree of file contents.
-
Q: What does
git commit -ado? A: The-aflag automatically stages all modified and deleted tracked files before committing. It skips thegit addstep but does NOT include untracked (new) files — those must still be added manually.