add & commit
Stage changes and save snapshots
Skills taught: git.add, git.commit, git.staging
The staging area
Git has three zones: your working files, the staging area (index), and the committed history. git add moves changes to the staging area. git commit saves the staging area as a permanent snapshot.
# See which files have changed
git status
# Stage specific file
git add index.html
# Stage all changes in current directory
git add .
# Stage part of a file interactively (pick specific lines)
git add -p index.html
# Commit with a message
git commit -m "Add navigation menu to homepage"
# Stage and commit tracked files in one step (skips untracked files)
git commit -am "Fix typo in footer"
Writing good commit messages
A commit message should complete the sentence: 'If applied, this commit will...' Use the imperative mood. Keep the first line under 72 characters. Reference issue numbers when relevant.
# ✅ Good commit messages
git commit -m "Add dark mode toggle to settings page"
git commit -m "Fix login redirect loop on mobile Safari"
git commit -m "Remove deprecated getUserInfo() function"
git commit -m "Bump React from 18.2 to 18.3"
# ❌ Bad commit messages
git commit -m "fix"
git commit -m "changes"
git commit -m "WIP"
git commit -m "asdfgh"
# Multi-line commit message (opens your editor)
git commit
# First line: short summary (under 72 chars)
# Blank line
# Body: explain WHY this change was made, not what
Unstaging and undoing
# Unstage a file (keep the change in working tree)
git restore --staged index.html
# Discard changes in working tree (WARNING: unrecoverable!)
git restore index.html
# Amend the last commit message (before pushing)
git commit --amend -m "Better message"
# Amend to add a forgotten file (before pushing)
git add forgotten.js
git commit --amend --no-edit
Warning: Never commit .env files, API keys, passwords, or secrets. Add them to .gitignore immediately. If you accidentally commit a secret, treat it as compromised and rotate it — git history is permanent.
Frequently Asked Questions
What should I put in .gitignore?
node_modules/, .env files, build output (dist/, build/), OS files (.DS_Store, Thumbs.db), editor files (.vscode/, .idea/), compiled binaries. GitHub provides starter .gitignore templates for every language at github.com/github/gitignore.
Can I unstage something after git add?
Yes. git restore --staged <file> removes a file from the staging area without touching your working copy. The change stays in your working tree — it just won't be in the next commit.