Chapter 7: Git Commit

Git commit! ☕

It’s February 13, 2026, Hyderabad lunch time, and this is the command that actually saves your work forever (locally). Everything before (add, staging, status) was preparation — commit is the real save button.

I’m going to explain it like we’re building your first real project together, step by step, with exact terminal outputs, analogies, common mistakes, and why good commits matter (especially when interviewers ask “explain your commit history”).

1. What does git commit really do? (Super simple)

git commit takes whatever is currently in the staging area (the green part from git status), wraps it in a nice little package called a commit object, and stores it permanently in your local .git/ folder.

Each commit is like:

  • A perfect photograph of your entire project at that exact second
  • With a label (commit message)
  • A timestamp
  • Your name & email (from git config)
  • A unique ID (SHA-1 hash, like a1b2c3d4…)
  • Pointer to the previous commit (so they chain together)

Once committed → it’s safe. You can delete files, break code, experiment wildly — you can always go back to any commit.

Important truth in 2026: Git never automatically commits anything. You must explicitly run git commit. That’s why people say “commit early, commit often” — small, frequent saves are better than one giant one at 2 a.m.

2. Basic Anatomy of a Commit (What You’ll See)

Every commit has:

  • Tree → snapshot of all files & folders
  • Parent → previous commit (except first one)
  • Author → you (name + email)
  • Committer → usually same as author
  • Date
  • Message → your description (most important part!)

3. Hands-on: Let’s Make Real Commits Right Now

Continue from previous lessons or start fresh:

Bash

Create first file:

Bash

Stage it:

Bash

Now — the magic:

Bash

What you’ll see (typical output):

text

Breakdown:

  • [main (root-commit) a1b2c3d] → new branch main, this is the very first commit (root), short hash a1b2c3d
  • 1 file changed, 2 insertions(+) → stats: what happened
  • create mode 100644 README.md → new file added

Congratulations! 🎉 Your first commit exists forever in .git/.

4. Everyday Commit Variations (Most Used in 2026)

Style Command Example When to use it Output / Notes
Normal (with message) git commit -m “Add login form HTML” Most common — quick one-liner message Fast, no editor opens
Detailed message git commit (no -m) When you want multi-line explanation Opens your editor (VS Code, nano…) — write subject + blank line + body
Commit all tracked changes (skip add) git commit -a -m “Fix typo in footer” Quick fixes on already-tracked files (does not add new files) Very convenient, but dangerous if you have junk staged
Amend last commit git commit –amend -m “Better message: added login + validation” Forgot to add a file or bad message? Fix last commit without new one Changes hash — never amend pushed commits!
Add to last commit (keep message) Make changes → git add . → git commit –amend –no-edit Forgot one small change Keeps original message, just updates snapshot

5. Let’s Make More Commits (Build the Story)

Edit README.md — add lines:

text

Stage & commit:

Bash

Now create actual code files:

Bash
Bash

Check history:

Bash

Typical output:

text

Or prettier:

Bash

You now have a beautiful linear history!

6. Good Commit Message Rules (What Pros & Interviewers Expect)

Bad: fix, update, changes, wip

Good (conventional in 2026):

  • Imperative mood → “Add”, “Fix”, “Refactor”, not “Added”, “Fixed”

  • Short subject line (≤50 chars) → first line

  • Blank line then body if needed (explain why, not what)

  • Examples:

    text
    text

7. Common Beginner Mistakes & Fixes

  • Forgot message → Git opens editor → just write & save (Ctrl+S, close)
  • git commit without staging anything → error: “nothing to commit”
  • Want to undo commit but keep changes? → git reset –soft HEAD~1
  • Want to throw away last commit completely? → git reset –hard HEAD~1 (dangerous!)
  • Pushed bad commit? → Never amend — use git revert instead

Quick Cheat Sheet – Commit Commands

  • Basic: git commit -m “message”
  • All tracked: git commit -a -m “message”
  • No staging needed (tracked only): git commit -am “quick fix”
  • Edit last: git commit –amend
  • See last commit details: git show
  • See full history: git log or git lg

Got the feeling? Commit = save point + story entry in your project’s diary.

Next?

  • Want to practice amending?
  • See how to write great messages?
  • Or move to push / pull / branches with commits?

Just say — we’re going strong! You’re officially committing like a developer now 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *