Making commits
Overview
A commit is a snapshot of your project at a moment in time, plus a message explaining it. Commits are the building blocks of history: everything else in Git — branches, merges, undo — works in terms of commits.
Your first commit
git add index.html style.css
git commit -m "Create home page and base styles"[main (root-commit) 4f1a2b3] Create home page and base styles
2 files changed, 2 insertions(+)
create mode 100644 index.html
create mode 100644 style.cssGit tells you the branch (main), that this is the first commit (root-commit), the start of the commit’s ID (4f1a2b3), and a summary of what changed.
Without -m, Git opens your configured editor for the message. Write it, save and close the file, and the commit is made. Close without writing anything and the commit is cancelled.
Writing good commit messages
Your future self and your teammates will read these messages to understand why the code is the way it is. Compare:
| Unhelpful | Helpful |
|---|---|
fix |
Fix crash when cart is empty |
changes |
Add price filter to search page |
asdf |
Update copyright year in footer |
Fixed the bug Bob found |
Prevent duplicate orders on double click |
Conventions most teams follow:
- Imperative mood: “Add”, “Fix”, “Remove” — as if giving an order. A good test: the message should complete the sentence “If applied, this commit will…”.
- A short subject line — about 50 characters, at most 72 — with no full stop.
- For bigger changes, a blank line then a body explaining what and why (not how — the code shows how).
git commit # opens the editor for a multi-line messagePrevent duplicate orders on double click
The checkout button stayed enabled while the payment request was
in flight, so a second click sent a second order. Disable the
button until the request completes.Writing good commits in chapter 9 goes further, including Conventional Commits.
Deleting and renaming files
Deleting a file in your editor is a change like any other — you then stage the deletion:
rm contact.html
git add contact.html # stages the deletion (yes, "add" a deletion)git rm does both in one step:
git rm contact.html
git commit -m "Remove contact page until the form is ready"To stop tracking a file but keep it on disk (for example a config file you committed by mistake): git rm --cached config.local.json.
Renaming works the same way:
git mv style.css main.css
git commit -m "Rename style.css to main.css"Git doesn’t actually record renames. It stores snapshots, and when you look at history it notices that a file disappeared and a very similar file appeared, and reports a rename:
rename style.css => main.css (100%)The percentage is how similar the two files are. If you rename and heavily edit a file in the same commit, Git may show a delete plus an add instead. Rename in one commit and edit in the next to keep the history easy to follow.
The shortcut: git commit -a
git commit -am "Fix typo in home page heading"-a stages every tracked file that has been modified or deleted, then commits. It’s handy for small changes, with two caveats:
- it never adds new (untracked) files — you still need
git addfor those; - it stages everything that changed, which defeats the point of the staging area if you had unrelated edits.
What makes a good commit?
- One logical change. “Add search” and “Fix footer typo” are two commits, even if you did them in the same hour.
- Complete. Include the code, its test and the documentation update together.
- Working. Each commit should leave the project in a state that builds and runs. That makes it possible to go back to any commit, and later to find bugs with
git bisect. - Small enough to review. If the message needs “and” several times, it’s probably several commits.
How often should you commit? Whenever you’ve finished a small, meaningful step. Many small commits are much easier to work with than a few huge ones, and you can always combine commits later (Interactive rebase) — but you can’t easily split a giant one.
Empty commits and messages
Git refuses to commit if nothing is staged:
nothing to commit, working tree cleanor, if you have changes that aren’t staged:
no changes added to commit (use "git add" and/or "git commit -a")Both mean “you haven’t staged anything”. Run git status to see why.
Rhythm: edit →
git status→git add→git commit -m "Imperative, specific message". Repeat many times a day.
Build a history of focused commits
Starting from an empty repository, make four commits, each doing one thing: (1) add index.html and style.css together as the first version of the site; (2) add a contact.html page; (3) rename style.css to main.css using Git; (4) delete contact.html and fix the heading in index.html — but as two separate commits, so that's five commits in total. Write a clear message for each, then check that the log shows five commits and that the rename was detected.
mkdir site && cd site && git init -b main
echo "<h1>Welcom</h1>" > index.html
echo "body { font-family: sans-serif; }" > style.css
# 1. First commit: index.html and style.css
# 2. Add a contact page in its own commit
echo "<h1>Contact</h1>" > contact.html
# 3. Rename style.css to main.css
# 4. Remove contact.html (one commit) and fix the typo "Welcom" (another commit)
# 5. Check the historymkdir site && cd site && git init -b main
echo "<h1>Welcom</h1>" > index.html
echo "body { font-family: sans-serif; }" > style.css
# 1. First commit: index.html and style.css
git add index.html style.css
git commit -m "Create home page and base styles"
# 2. Add a contact page in its own commit
echo "<h1>Contact</h1>" > contact.html
git add contact.html
git commit -m "Add contact page"
# 3. Rename style.css to main.css
git mv style.css main.css
git commit -m "Rename style.css to main.css"
# 4. Remove contact.html (one commit) and fix the typo "Welcom" (another commit)
git rm contact.html
git commit -m "Remove contact page until the form is ready"
sed -i.bak 's/Welcom</Welcome</' index.html && rm index.html.bak
git commit -am "Fix typo in home page heading"
# 5. Check the history
git log --oneline # five commits, newest first
git show --stat HEAD~2 # rename style.css => main.css (100%)Summary
git commit -m "message"saves the staged changes as a new commit; without-m, Git opens your editor.- Write messages in the imperative mood that explain the change: “Add contact page”, not “changes”.
git rmandgit mvdelete and rename files and stage the change in one step.git commit -astages all tracked, modified files before committing — but never adds new files.- Make each commit one logical change that leaves the project working.