Git Course 0%

Committing and good messages

Beginner Git CLI VS Code UI IntelliJ UI GitLab UI GitHub UI ≈ 14 min

What you will learn

  • What git commit records and what the output tells you
  • The shape of a good message: subject, type prefix, issue number, optional body
  • The -a shortcut, the editor flow, and --amend for the commit you just made

After this lesson you can

  • My commits are small, each about one thing, with messages the team's history reads well
  • I can fix the last commit's message or add a forgotten file without a second "oops" commit

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

Terminal
$ 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

Text
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:

Terminal
$ git commit -m "feat: add Eagle Ridge trail (#21)" -m "Requested by the North region office. Distance measured on 2026-09-01."
$ git log -1
commit 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 revert undoes one commit; you cannot revert half of one.
  • git log -- file and git blame tell 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:

Terminal
$ 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:

Terminal
$ 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:

Terminal
$ 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.

Common mistakes

  • "Nothing to commit" after editing. Nothing was staged. git add first (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 -a with a stray change in another file. Read git status before -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.

  1. In the playground, add a row to data/trails.csv and stage it.
  2. Commit with a subject and a body using two -m flags. Check with git log -1.
  3. Add a matching FAQ line in docs/faq.md, stage it, and run git commit --amend --no-edit.
  4. Run git show --stat HEAD and git 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.

Check yourself

1. Which subject line follows the conventions taught here?
2. git commit -a does not include…
3. When is git commit --amend appropriate?

Key terms

Commit Hash (SHA) Staging area HEAD