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:
|
0 1 2 3 4 5 6 7 8 |
mkdir todo-app-2026 cd todo-app-2026 git init |
Create first file:
|
0 1 2 3 4 5 6 7 |
echo "# My Todo App 2026" > README.md echo "- Learn commit properly" >> README.md |
Stage it:
|
0 1 2 3 4 5 6 7 |
git add README.md git status # should show green "new file: README.md" |
Now — the magic:
|
0 1 2 3 4 5 6 |
git commit -m "Initial commit: add README with project title and first goal" |
What you’ll see (typical output):
|
0 1 2 3 4 5 6 7 8 |
[main (root-commit) a1b2c3d] Initial commit: add README with project title and first goal 1 file changed, 2 insertions(+) create mode 100644 README.md |
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:
|
0 1 2 3 4 5 6 7 |
- Create basic HTML structure - Style with CSS |
Stage & commit:
|
0 1 2 3 4 5 6 7 |
git add README.md git commit -m "Update README: add next development steps" |
Now create actual code files:
|
0 1 2 3 4 5 6 7 |
echo '<!DOCTYPE html><html><head><title>Todo</title></head><body><h1>Todo App</h1></body></html>' > index.html echo 'body { font-family: Arial; }' > style.css |
|
0 1 2 3 4 5 6 7 |
git add index.html style.css git commit -m "Add basic HTML skeleton and minimal CSS" |
Check history:
|
0 1 2 3 4 5 6 |
git log --oneline |
Typical output:
|
0 1 2 3 4 5 6 7 8 |
e5f6g7h Add basic HTML skeleton and minimal CSS d4e3f2g Update README: add next development steps a1b2c3d Initial commit: add README with project title and first goal |
Or prettier:
|
0 1 2 3 4 5 6 |
git lg # if you set the alias earlier |
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:
text012345678910Add user authentication endpoint- Implement JWT token generation- Add /login and /register routes- Include password hashing with bcrypttext0123456789Fix mobile menu not closing on link clickRoot cause: event propagation not stoppedSolution: added e.stopPropagation()
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 🚀
