Staging and Committing
Use git add to stage changes and git commit to save a permanent snapshot.
Syntax
git add <files>
git commit -m "message"A commit is a saved snapshot of your project at a point in time, with a message describing what changed.
Staging with git add
git add file.txt— stage one file.git add .— stage everything in the current folder.git add -A— stage all changes, including deletions.
Committing
Run git commit -m "message" to store the staged snapshot. Write messages in the imperative mood, for example “Add login form” rather than “Added login form”.
Example
git add index.html style.css
git commit -m "Add home page and base styles"
# Stage and commit tracked files in one go
git commit -am "Fix typo in heading"When to use it
- A developer stages only the two files related to a bug fix, keeping a half-finished feature out of the commit to maintain a clean history.
- A technical writer stages a README update separately from code changes to produce a focused, single-purpose commit.
- A team enforces that every commit has a descriptive message; using git commit -m keeps that message concise and inline.
More examples
Stage all changes and commit
Stages every modified and new file in the working tree, then saves them as a single named snapshot.
git add .
git commit -m "Add user registration form"Stage specific files selectively
Demonstrates selective staging so unrelated changes remain in the working directory and out of this commit.
git add src/auth.py tests/test_auth.py
git status
# Changes to be committed:
# modified: src/auth.py
# modified: tests/test_auth.py
git commit -m "Fix password hashing in auth module"Stage file hunks interactively
Patch mode lets you commit only some lines of a file, creating laser-focused commits even when a file has mixed changes.
# Launch interactive hunk selection
git add -p src/utils.py
# Git presents each changed section:
# Stage this hunk [y,n,q,a,d,s,?]?
# Press y to stage, n to skip each chunk
Discussion