Your first commits
Committing is what you'll do most in Git: many times a day. Let's master the everyday loop: edit → status → add → commit, plus reading history and diffs.
The everyday loop
| Command | What it does |
|---|---|
git status |
Shows what's changed, staged and untracked. Run it constantly. |
git add <file> |
Stage a file's current contents |
git add . |
Stage everything in the current folder |
git commit -m "msg" |
Save the staged snapshot with a message |
git diff |
Show unstaged changes (working directory vs staging area) |
git diff --staged |
Show staged changes (what will be committed) |
git log / git log --oneline |
Show history |
git show <commit> |
Show one commit's changes |
Why stage at all?
Staging lets you build focused commits. If you fixed a bug and changed some styling, commit them separately so each commit does one thing:
Shortcuts
git commit -am "Message" # stage all *tracked* modified files and commit (new files still need git add)
git add -p # (real Git) review and stage changes hunk by hunk — a pro favouriteWriting good commit messages
Your commit history is documentation for your future self and your team.
Short summary in imperative mood (≤ 50 chars)
Optional body explaining WHY the change was made, what problem
it solves, and any trade-offs. Wrap at ~72 characters.
Fixes #123| ✗ Bad | ✓ Good |
|---|---|
fix |
Fix login redirect loop on expired session |
changes |
Add price filter to product search |
WIP stuff asdf |
Extract cart total calculation into helper |
Fixed the bug in the thing |
Prevent duplicate orders on double-click |
Write the summary as if completing the sentence "If applied, this commit will… add price filter to product search."
Many teams use Conventional Commits: feat: add dark mode, fix: handle empty cart, docs: update README, refactor: …, test: …, chore: …. Tools can then generate changelogs automatically.
What should you commit?
- ✓ Source code, config, docs, tests and lock files (
package-lock.json) - ✗ Generated files (
dist/,build/), dependencies (node_modules/), OS junk (.DS_Store), and secrets (.env, API keys, passwords)
List what Git should ignore in a .gitignore file:
# .gitignore
node_modules/
dist/
.env
*.log
.DS_Store⚠️ If you commit a secret, assume it's leaked, even if you delete it in the next commit, because it's still in the history. Rotate (change) the key immediately.
Commit often
Small, frequent commits are easy to understand, review and undo. A good rhythm is to commit every time you reach a working state, often every 15–60 minutes.
Practice
In the terminal below, start a new repository and make three commits: create a todo.txt with one task; add a second task; then create a .gitignore containing secrets.txt. Create secrets.txt too and check that git status doesn't show it. (The simulator doesn't read .gitignore, but real Git does. Try this in your own terminal as well!) Finish with git log --oneline.
1.You edited a.js and b.js but only ran git add a.js. What does git commit save?
2.Which is the best commit message?