Lesson Completion
Back to course

git commit

Beginner
12 minutes4.9GitPlay to LearnLearn by Story

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

flowchart LR C1["📸 Commit 1<br/>a1b2c3d<br/>root commit"] --> C2["📸 Commit 2<br/>e4f5g6h<br/>parent: a1b2c3d"] C2 --> C3["📸 Commit 3<br/>i7j8k9l<br/>parent: e4f5g6h"] C3 --> HEAD["🔖 HEAD"] style C1 fill:#1a1a2e,stroke:#e94560,color:#e94560 style C2 fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style C3 fill:#0f3460,stroke:#53d8fb,color:#53d8fb style HEAD fill:#0f3460,stroke:#53d8fb,color:#53d8fb

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 revert precise and safe.
  • Collaboration: Commits are the unit of sharing — git push sends commits, git pull receives them.

Code

bash
# ─── 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 point

Commit Message Best Practices

RuleExample
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 lineSubject → blank line → details
Explain why, not just what"Fix race condition in auth — tokens expired before refresh"

Key Takeaways

  • git commit creates 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 -a flag 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 -a do? A: The -a flag automatically stages all modified and deleted tracked files before committing. It skips the git add step but does NOT include untracked (new) files — those must still be added manually.

Topics Covered

Git BasicsGit Fundamentals

Tags

#git#commit#snapshot#beginner-friendly

Last Updated

2026-02-12