Committing and good messages
Beginner Git CLI VS Code UI IntelliJ UI GitLab UI GitHub UI
Why this matters
A commit is the unit of history: what gets reviewed, reverted, blamed, cherry-picked and read. Good commits are small and say why. Bad commits are "fix" and "wip" and contain three unrelated things. The mechanics take a minute; the discipline is what makes you pleasant to work with.
The command
$ git commit -m "docs: describe the region and answer a data question (#28)"[main dcc61f1] docs: describe the region and answer a data question (#28)
2 files changed, 5 insertions(+), 1 deletion(-)The output: the branch, the new commit's short hash, the message, and a summary of what was recorded. Only staged changes went in. git status is clean afterwards (unless you left things unstaged), and git log --oneline -1 shows the commit.
Without -m, Git opens your editor (lesson 3.3) with a template; write the message, save, close. Saving an empty message cancels the commit.
What a good message looks like
docs: add Windows steps to installation guide (#12)- Subject line, about 50 characters, no trailing period. It appears in
git log --oneline, in merge requests, in release notes. - Imperative mood: "add", "fix", "remove", as if completing "this commit will…". Not "added", not "adding".
- A type prefix if the team uses one (the playground's CONTRIBUTING does):
feat,fix,docs,chore,test,refactor. This is the Conventional Commits style. - The issue number in parentheses or at the end, so GitLab and GitHub link it.
When the subject cannot explain why, add a body: a blank line, then prose. On the command line, a second -m starts the body:
$ git commit -m "feat: add Eagle Ridge trail (#21)" -m "Requested by the North region office. Distance measured on 2026-09-01."
$ git log -1commit 79c86c54a1eacac536b48ab0a0ff614ea6908f1a
Author: Ana <ana@northwind-trails.example>
Date: Mon Sep 7 09:30:00 2026 +0200
feat: add Eagle Ridge trail (#21)
Requested by the North region office. Distance measured on 2026-09-01.The body is where you put context nobody can recover later: what was tried, why this way, which discussion decided it. The diff shows what; the message shows why.
| Instead of | Write |
|---|---|
fix |
fix: handle empty distance in trail list (#15) |
updated docs |
docs: add Windows steps to installation guide (#12) |
changes from review |
docs: reword installation intro per review (#12) |
WIP |
commit anyway with a real subject; squash later if the team squashes |
Small commits
One commit per logical change: the typo fix and the new section are two commits, even if you made them in the same hour. Reasons you will feel quickly:
- Reviewers read a commit at a time; small ones get approved.
git revertundoes one commit; you cannot revert half of one.git log -- fileandgit blametell a clearer story.
If you edited more than one thing before committing, lesson 5.3 shows how to stage them separately.
The -a shortcut
git commit -a stages every modified or deleted tracked file and commits in one step. It never includes new (untracked) files:
$ git commit -am "docs: add maintainer note"[main c541c80] docs: add maintainer note
1 file changed, 2 insertions(+)Convenient when you changed exactly the files you meant; risky when you did not. git status first, as always.
Fixing the last commit: --amend
Forgot a file, or typed a wrong message? Amend replaces the last commit with a corrected one:
$ git add docs/faq.md
$ git commit --amend --no-edit--no-edit keeps the message; without it the editor opens so you can change it, or use -m:
$ git commit --amend -m "feat: add Coast Walk trail and FAQ entry (#29)"Amend rewrites the commit (new hash), so it follows the rule of lesson 2.7: only before pushing. After a push, make a new commit instead.
In the tools
Everything above. A useful check before committing: git diff --staged --stat. And after: git log --oneline -3.
- Stage what belongs in the commit (lesson 5.3). Type the message in the box at the top of Source Control; the first line is the subject, an empty line then the body.
- Click Commit (or Ctrl+Enter / ⌘Enter). If nothing is staged, VS Code asks whether to stage all and commit (the "smart commit" behaviour); say no while learning.
- To amend: the … menu next to Commit → Commit (Amend), or Command Palette → Git: Commit Staged (Amend).
- The Source Control Graph below the changes shows the new commit immediately.
- Open the Commit tool window (Alt+0 / ⌘0), tick the files, write the message in the box (subject on the first line, blank line, body).
- Click Commit (Ctrl+K / ⌘K). Commit and Push… does both; use plain Commit while learning.
- To amend: tick Amend above the message box before committing; the previous message loads and you can edit it.
- The message box remembers recent messages (the clock icon) and warns about long subject lines.
Editing a file in the browser ends with a Commit changes panel: a commit message (GitLab suggests one; replace it with a real subject), a Target branch, and a Start a new merge request checkbox. On a protected main, GitLab forces a new branch and a merge request. The Web IDE commits several files at once through its Source Control panel. Amending is not available on the web.
The pencil editor ends with Commit changes…, which opens a dialog: commit message, optional extended description (the body), and a choice between committing directly to the branch or creating a new branch and starting a pull request. Amending is not available on the web.
Common mistakes
- "Nothing to commit" after editing. Nothing was staged.
git addfirst (lesson 5.3). - A giant editor window appeared. You omitted
-m. Write the message and close, or close without saving to abort, then retry with-m. - Amending after pushing. Diverged branches and a rejected push. Add a new commit instead.
- Committing
-awith a stray change in another file. Readgit statusbefore-a. - Writing the message for yourself now instead of the reader later. "fix" means nothing in a month.
Try it yourself
Goal: make a commit with a body, then amend it to add a forgotten file.
- In the playground, add a row to
data/trails.csvand stage it. - Commit with a subject and a body using two
-mflags. Check withgit log -1. - Add a matching FAQ line in
docs/faq.md, stage it, and rungit commit --amend --no-edit. - Run
git show --stat HEADandgit log --oneline -2.
Expected result: git log -1 shows the two paragraphs; after amending, git show --stat lists both files under the same message, and the commit's hash differs from before the amend.
Show solution
The amended commit contains both files and the original message; the hash changed because the content changed. Since nothing was pushed, this is exactly what amend is for. If you had pushed between steps 2 and 3, the right move would have been a second commit, docs: add FAQ entry for the new trail.