12 minlesson

Staging and Committing Changes

Staging and Committing Changes

Git's staging area gives you precise control over what goes into each commit. This lesson covers the workflow from editing files to creating permanent history.

The Three Areas of Git

Understanding Git's architecture is key to mastering it:

1┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
2│ Working Tree │───▶│ Staging Area │───▶│ Repository │
3│ (your files) │ │ (index) │ │ (.git) │
4└─────────────────┘ └─────────────────┘ └─────────────────┘
5 edit git add git commit
  • Working Tree: Your actual files on disk
  • Staging Area: Files marked for the next commit
  • Repository: Permanent commit history

Checking Status

The git status command shows the current state:

bash
1$ git status
2On branch main
3Your branch is up to date with 'origin/main'.
4
5Changes not staged for commit:
6 (use "git add <file>..." to update what will be committed)
7 (use "git restore <file>..." to discard changes in working directory)
8 modified: README.md
9
10Untracked files:
11 (use "git add <file>..." to include in what will be committed)
12 new-file.txt
13
14no changes added to commit (use "git add" and/or "git commit -a")

Short Status

For a compact view, use -sb:

bash
1$ git status -sb
2## main...origin/main
3 M README.md
4?? new-file.txt

Status codes:

  • M = Modified
  • A = Added (staged)
  • D = Deleted
  • ?? = Untracked
  • R = Renamed

Staging Files

Stage Specific Files

bash
1$ git add README.md
2$ git add new-file.txt

Stage All Changes

bash
1$ git add . # Everything in current directory
2$ git add -A # Everything in entire repository

Stage Interactively

Review and select specific hunks:

bash
1$ git add -p README.md

This shows each change and asks:

  • y = stage this hunk
  • n = skip this hunk
  • s = split into smaller hunks
  • q = quit

Reviewing Changes

Unstaged Changes

See what you've modified but not staged:

bash
1$ git diff
2diff --git a/README.md b/README.md
3index f0e1d2c..a5b6c7d 100644
4--- a/README.md
5+++ b/README.md
6@@ -1 +1,3 @@
7 # Demo Repo
8+
9+This is a demo repository for learning Git.

Staged Changes

See what will be committed:

bash
1$ git diff --cached

Or equivalently:

bash
1$ git diff --staged

Committing

Basic Commit

bash
1$ git commit -m "docs: add description to README"
2[main 2b3c4d5] docs: add description to README
3 1 file changed, 2 insertions(+)

Commit with Editor

For longer messages, omit -m:

bash
1$ git commit

This opens your editor. Write:

  • First line: Short summary (50 chars)
  • Blank line
  • Body: Detailed explanation

Conventional Commit Format

Use prefixes to categorize commits:

PrefixUse For
feat:New features
fix:Bug fixes
docs:Documentation
style:Formatting (no code change)
refactor:Code restructuring
test:Adding tests
chore:Maintenance tasks

Example:

1feat: add user authentication
2
3- Implement JWT token generation
4- Add login/logout endpoints
5- Create user session middleware

Inspecting History

Basic Log

bash
1$ git log
2commit 2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c
3Author: Ada Lovelace <ada@example.com>
4Date: Sun Nov 30 10:15:00 2025 +0000
5
6 docs: add description to README

Compact Log

bash
1$ git log --oneline
22b3c4d5 docs: add description to README
31a2b3c4 docs: add initial README

Graph View

See branch structure:

bash
1$ git log --oneline --graph --decorate
2* 2b3c4d5 (HEAD -> main, origin/main) docs: add description to README
3* 1a2b3c4 docs: add initial README

Filter by Author or Date

bash
1$ git log --author="Ada"
2$ git log --since="2 weeks ago"
3$ git log --grep="fix"

Best Practices

Commit Often, Commit Small

Each commit should be a single logical change. This makes:

  • History easier to understand
  • Reverting specific changes possible
  • Code review more manageable

Write Good Messages

Bad:

fixed stuff

Good:

1fix: prevent crash when user input is empty
2
3The application crashed when users submitted forms without
4filling required fields. Added null checks to validation.

Review Before Committing

Always check git diff --cached before committing to ensure you're including exactly what you intend.

Key Takeaway

The staging area lets you craft precise commits. Use git status to understand state, git add -p for selective staging, git diff --cached to review, and write meaningful commit messages.