You've staged your changes, and now they're sitting in the staging areaWhat is staging area?A holding zone in Git where you place changes you want to include in your next commit, letting you select exactly which modifications to save. waiting to be saved. The git commit command is the moment of truth, it's when Git permanently records your work as a snapshot in the project's history.
Think of it like clicking "Save" in a video game. That save point exists forever; you can always return to it. Every commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. is a save point for your code.
Your first commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.
Once you've staged your changes with git add, committing is straightforward:
git commit -m "Add initial project structure"The -m flag stands for "message" and lets you write your commit message inline. Git responds with something like:
[main (root-commit) a1b2c3d] Add initial project structure
3 files changed, 45 insertions(+)
create mode 100644 index.html
create mode 100644 style.css
create mode 100644 app.jsLet's decode that:
main, the branch you're onroot-commit, this is the very first commit (only shown for initial commits)a1b2c3d, the unique hash ID for this commit (abbreviated)- The message you wrote
- Stats about what changed
The art of writing commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. messages
Here's where many developers go wrong. Your commit messages are a love letter to your future self and your teammates. Six months from now, when something breaks and you're digging through history, you'll be grateful for clear, descriptive messages.
What makes a good message?
Good examples:
- "Fix navigation menu overlapping on mobile devices under 768px"
- "Add user authenticationWhat is authentication?Verifying who a user is, typically through credentials like a password or token. with bcryptWhat is bcrypt?A widely used password hashing algorithm that is intentionally slow to make brute-force cracking impractical. It automatically generates and embeds a salt in the hash output. password hashingWhat is hashing?A one-way mathematical transformation that turns data (like a password) into a fixed-length string that can't be reversed. Used to store passwords securely."
- "Refactor database queries to use parameterized statements (prevents SQL injectionWhat is sql injection?An attack where user input is inserted directly into a database query, letting the attacker read, modify, or delete data. Parameterized queries prevent it.)"
Bad examples:
- "fix" (Fix what? How? Why?)
- "asdf" (Keyboard smashing is not a message)
- "updated" (Everything is always being updated, this says nothing)
| Message quality | Example | Why it works / fails |
|---|---|---|
| Excellent | "Fix login redirect loop on expired sessions" | Specific problem, specific context |
| Good | "Add dark mode toggle to settings page" | Clear feature description |
| Vague | "Update styles" | Which styles? Why? |
| Useless | "stuff" | Tells you nothing |
The seven rules, and how to evaluate AI-generated messages
AI tools can generate commit messages for you, but they often produce vague or mechanical output. Use these rules to evaluate (and rewrite) what AI suggests:
- Separate subject from body with a blank line when you need details
- Limit the subject line to 50 characters: it should fit in a GitHub notification
- Capitalize the subject line: "Add feature" not "add feature"
- Don't end with a period: saves two characters, looks cleaner
- Use the imperative mood: "Fix bug" not "Fixed bug" or "Fixes bug"
- Wrap the body at 72 characters: for terminals and emails
- Use the body to explain what and why, not how: the code shows how
Multi-line commit messages
Sometimes a single line isn't enough. You need to explain context, trade-offs, or link to issue trackers. Skip the -m flag and let Git open your default editor:
git commitYour editor opens with:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Changes to be committed:
# modified: auth.js
#Write your detailed message:
Implement JWT authentication
Replace session-based auth with JSON Web Tokens to support
mobile app authentication. Tokens expire after 24 hours for
security. Refresh tokens allow seamless re-authentication.
Fixes #142Save and close the editor to commit.
Reading your commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. history
Once you've made commits, you'll want to look back at them:
# Full detailed log
git log
# Compact one-line format
git log --oneline
# Last 5 commits
git log -5
# With visual graph of branches
git log --oneline --graphThe git log output shows:
commit a1b2c3d4e5f6789012345678901234567890abcd
Author: Your Name <you@example.com>
Date: Mon Jan 15 10:30:00 2024
Add user registration form
- Create registration endpoint
- Add client-side validation
- Style with Tailwind CSS
commit b2c3d4e5f6789012345678901234567890abcdef
Author: Your Name <you@example.com>
Date: Mon Jan 15 09:15:00 2024
Set up Express server with basic routingFixing the last commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.
We've all been there: you commit, then immediately notice a typo, a missing file, or a better way to phrase your message. Git lets you amend the most recent commit:
# Stage the fix
git add forgotten-file.txt
# Amend the previous commit (keeps the same message)
git commit --amend --no-edit
# Or change the message too
git commit --amend -m "Better commit message"Committing like a pro
Here's the workflow you'll use every day:
# 1. Check what you've changed
git status
git diff
# 2. Stage the changes you want
git add src/components/Login.tsx
# 3. Commit with a descriptive message
git commit -m "Add login form with email validation"
# 4. Verify it worked
git log --oneline -3Remember: commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. early, commit often. Small, focused commits with clear messages are infinitely more valuable than massive commits with cryptic messages like "stuff". Your commit history should tell the story of your project, make it a story worth reading.
Quick reference
| Command | What it does | When to use it |
|---|---|---|
git commit -m "msg" | Commit with inline message | Quick, simple commits |
git commit | Open editor for message | Detailed, multi-line messages |
git commit --amend | Fix the last commit | Typos, forgotten files (local only) |
git log --oneline | Compact history view | Quick overview |
git log -5 | Last 5 commits | Recent context |
# Stage your changes
git add .
# Commit with a descriptive message
git commit -m "Initial commit: Add project files"
# View commit history
git log --oneline
# View detailed log
git log