Git/
Lesson

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.js

Let's decode that:

  • main, the branch you're on
  • root-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

02

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.

AI pitfall
ChatGPT and Copilot generate generic commit messages like "Update files", "Fix bug", or "Add changes". These are useless when you're trying to understand project history. Always write your own commit messages that explain the why, not just the what. If AI wrote the code, you still need to understand it well enough to describe it.

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 qualityExampleWhy 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:

  1. Separate subject from body with a blank line when you need details
  2. Limit the subject line to 50 characters: it should fit in a GitHub notification
  3. Capitalize the subject line: "Add feature" not "add feature"
  4. Don't end with a period: saves two characters, looks cleaner
  5. Use the imperative mood: "Fix bug" not "Fixed bug" or "Fixes bug"
  6. Wrap the body at 72 characters: for terminals and emails
  7. Use the body to explain what and why, not how: the code shows how
AI pitfall
AI-generated commit messages tend to describe what changed mechanically ("Update auth.js, add login function") instead of why it matters ("Add JWT authentication for mobile app support"). If AI generates your commit message, always rewrite it to capture the intent, not just the diff summary.

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 commit

Your 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 #142

Save and close the editor to commit.

03

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 --graph

The 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 routing
04

Fixing 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"
Critical warning
Only amend commits that exist only on your local machine. If you've already pushed to GitHub, amending creates a new commit hash, which confuses Git and causes headaches for anyone else working on the project. If you must fix a pushed commit, make a new commit that fixes it instead.
05

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 -3

Remember: 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.

06

Quick reference

CommandWhat it doesWhen to use it
git commit -m "msg"Commit with inline messageQuick, simple commits
git commitOpen editor for messageDetailed, multi-line messages
git commit --amendFix the last commitTypos, forgotten files (local only)
git log --onelineCompact history viewQuick overview
git log -5Last 5 commitsRecent context
javascript
# 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