Writing Good Commit Messages
Picture a box of family photos with nothing written on the back. Today you know who is in each one and where it was taken. In ten years, nobody will. A short note on the back, "Advitha's first day at school, June 2026", keeps the photo useful for good.
A commit message is that note. Git saves your work whatever you write, but the message decides whether anyone, including you in six months, can understand the history later. People read messages when they look for the change that broke something, when they review a teammate's work, and when they decide whether a commit is safe to undo.
In this lesson, you will learn the shape of a good message, the few rules most teams follow, and the optional Conventional Commits style.
Why the Message Matters
Here is a small project whose four commits were made in a hurry:
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "first"
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "changes"
echo 'Term 1 fee: $12,500' > fees.txt
git commit --quiet -am "fixed stuff"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "update"
git log --oneline7a62efb (HEAD -> main) update
92bfe92 fixed stuff
6af753e changes
10bd332 first--quiet only hides the receipt that each git commit prints, so that the history is easier to see. Now try to answer two simple questions from that history: when did the term 1 fee change, and which commit added the late fee? You can't, without opening every commit.
Here is the same work again in a second folder, with clear messages:
cd ..
mkdir fee-tracker-2
cd fee-tracker-2
git init --quiet
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "Add project README"
echo 'Term 1 fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term 1 fee"
echo 'Term 1 fee: $12,500' > fees.txt
git commit --quiet -am 'Correct the term 1 fee to $12,500'
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git log --oneline33274f6 (HEAD -> main) Add the late fee
02e7476 Correct the term 1 fee to $12,500
d2b5c16 Add the term 1 fee
3d5008a Add project READMEThe files are the same. The work is the same. But this history answers both questions at a glance.
The Shape of a Commit Message
A commit message has two parts, separated by one empty line:
Add the hostel fee <- subject line
Students who stay in the school hostel pay a <- body
separate fee each term. The office asked for it to
be listed with the other fees, so parents see the
full amount in one place.| Part | What it holds | The usual rule |
|---|---|---|
| Subject | A one-line summary of the change | About 50 characters, never more than 72. No period at the end. |
| Empty line | Nothing | Always there when a body follows |
| Body | Why the change was made, and anything a reader would not guess from the code | Lines wrapped at about 72 characters. Optional for small changes. |
The subject line is what git log --oneline, GitHub, GitLab and most tools show in their lists, so it has to make sense alone. Long subjects get cut off in those lists, which is where the 50 and 72 limits come from. Git itself accepts a message of any length. These are habits, not rules Git enforces.
The body is for why, not what. The changes themselves already show what changed, line by line. What they can't show is the reason: the request from the office, the bug report, the rule that made the old value wrong.
Navika adds the hostel fee with a subject and a body. As in the previous lesson, each -m becomes one paragraph:
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee" -m "Students who stay in the school hostel pay a
separate fee each term. The office asked for it to
be listed with the other fees, so parents see the
full amount in one place."
git log -1commit 1c3dbd45581ff27e760138c4b551db4c7957026f (HEAD -> main)
Author: Navika Anamaneni <navika@example.com>
Date: Mon Jan 5 10:33:00 2026 -0600
Add the hostel fee
Students who stay in the school hostel pay a
separate fee each term. The office asked for it to
be listed with the other fees, so parents see the
full amount in one place.In daily work, you would leave out -m and type the same text in your editor. That is easier for anything longer than one line.
Write the Subject as a Command
Most projects, including Git itself, write the subject in the imperative mood: the form you use to give an instruction, like "Add", "Fix" or "Remove". A simple test: the subject should finish the sentence "If applied, this commit will ..."
| Write | Not | Because |
|---|---|---|
Add the late fee | Added the late fee | "If applied, this commit will added..." doesn't work |
Fix the rounding of term fees | Fixes rounding | Say what is fixed, as an instruction |
Remove the old fee list | Removing old stuff | Name the thing removed |
Correct the term 1 fee to $12,500 | fee change | A reader knows the old and the new value |
Git uses the same style in the messages it writes for you, such as Merge branch 'main' and Revert "Add the late fee", so your messages and Git's read alike in the history.
Rules Most Teams Follow
- Start with a capital letter, and don't end with a period. The subject is a title.
- Keep the subject short. Aim for 50 characters. If you need "and" to describe it, the commit may be doing two things.
- Leave an empty line before the body. Without it, Git treats your whole message as one long subject (see Under the Hood).
- Explain why in the body. Mention the problem, the request or the decision that led to the change.
- One change per commit. A good message is easy to write when the commit does one thing.
git add -pfrom the "Git Add" lesson helps you split mixed work. - Never write "update", "changes", "fix" or "wip" alone. They are true of every commit, so they say nothing.
Conventional Commits
Some teams add a short type to the front of every subject, in a style called Conventional Commits. It is optional. Use it when your team or project asks for it.
<type>(<optional scope>): <description>| Type | Used for |
|---|---|
feat | A new feature for the user |
fix | A bug fix |
docs | Documentation only |
refactor | A change to the code that neither fixes a bug nor adds a feature |
test | Adding or correcting tests |
chore | Maintenance work, such as updating tools or build settings |
An exclamation mark after the type, as in feat!: remove the old fee format, marks a change that breaks things for existing users.
Navika's team uses this style for the next three commits:
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "feat: print a receipt after each payment"
echo 'Late fee: $300' >> fees.txt
git commit --quiet -am 'fix(fees): raise the late fee to $300'
echo "Run the app to see this term's fees." >> README.md
git commit --quiet -am "docs: explain how to see this term's fees"
git log --onelinec80f628 (HEAD -> main) docs: explain how to see this term's fees
127b904 fix(fees): raise the late fee to $300
d8c54f0 feat: print a receipt after each payment
1c3dbd4 Add the hostel fee
33274f6 Add the late fee
02e7476 Correct the term 1 fee to $12,500
d2b5c16 Add the term 1 fee
3d5008a Add project READMEBecause the type is always in the same place, tools can read it. Here, git log --grep finds only the bug fixes. --grep keeps the commits whose message matches a pattern, and ^fix means "starts with fix":
git log --oneline --grep="^fix"127b904 fix(fees): raise the late fee to $300Teams also use the types to build release notes automatically, and to decide whether a release is a small fix or a new feature.
Common Mistakes
Messages that say nothing. "update", "changes", "fixed stuff" and "wip" are true of every commit. Name the change.
Describing the code instead of the reason. "Change 12000 to 12500" repeats what the changes already show. "Correct the term 1 fee to $12,500" plus a body saying the office raised it in March tells the reader something new.
No empty line between the subject and the body. Git then joins the lines into one long subject, and every list shows a run-on sentence.
A subject that needs "and". "Add the hostel fee and fix the README typo" is two commits. Split them with git add -p or by staging files separately.
Copying the issue title and nothing else. "Bug 142" means nothing once the issue tracker changes. Say what the change does, and add the issue number in the body if your team wants it.
Fixing a bad message after pushing. git commit --amend changes the commit ID. For a commit you have not pushed, amend it. For one you have pushed, leave it and write better messages from now on.
Under the Hood
Git splits a message at the first empty line. Everything before it is the subject, and everything after it is the body. The --format option of git log can print each part on its own: %s is the subject and %b is the body.
git log -1 --format="%s" HEAD~3
git log -1 --format="%b" HEAD~3Add the hostel fee
Students who stay in the school hostel pay a
separate fee each term. The office asked for it to
be listed with the other fees, so parents see the
full amount in one place.HEAD~3 means "three commits before the newest one", which is the hostel fee commit. The subject and the body came out separately, because the message had an empty line between them.
Now see what happens without that empty line. Navika writes a two-line message with no gap:
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -am "Add the exam fee
The exam office confirmed the amount."
git log -1 --format="%s"
git log --oneline -1Add the exam fee The exam office confirmed the amount.
3eabd0a (HEAD -> main) Add the exam fee The exam office confirmed the amount.Git joined both lines into one subject. Every tool that shows subjects will now show that long line. One empty line would have prevented it.
Two other details are worth knowing:
- When you write the message in the editor, Git removes the lines that start with
#, the spaces at the ends of lines, and empty lines at the start and the end. With-m, it removes only the extra spaces and empty lines, so a#you type is kept. - The message is stored as plain text inside the commit object, after an empty line, as you saw with
git cat-file -pin the "Git Commit" lesson. Because the message is part of what the commit ID is calculated from, changing even one letter of it makes a new commit with a new ID.
Interview Corner
What makes a good commit message?
A short subject line, about 50 characters, in the imperative mood ("Add the late fee"), with no period. Then an empty line, then a body wrapped at about 72 characters that explains why the change was made. The commit itself should contain one logical change, so the message can describe it in one line.
Why should the subject be in the imperative mood?
The subject describes what the commit does when it is applied, so it reads as an instruction: "If applied, this commit will add the late fee." It also matches the messages Git writes itself, such as "Merge branch" and "Revert", so the whole history reads consistently.
Why is the empty line after the subject important?
Git and the tools built on it treat everything up to the first empty line as the subject. Without the empty line, the body is joined to the subject, and lists such as git log --oneline and pull request titles show one very long line.
What are Conventional Commits?
A message format where the subject starts with a type and an optional scope, such as feat:, fix(fees): or docs:, and ! marks a breaking change. It makes history easy to filter, and tools use it to write release notes and choose version numbers automatically.
Should the message say what changed or why?
Mainly why. The changes themselves show what changed, line by line. The message should give the reason and any context a reader could not get from the code, with the subject summarizing the change in a few words.
Practice
Challenge: subject or body? Continue in the same repository. Add the line Sports fee: $700 to fees.txt and commit it with this command, which has two -m options:
git commit -am "Add the sports fee" -m "Requested by the sports teacher, Srihitha."
Then print the subject alone with git log -1 --format="%s", and the one-line history of the last two commits with git log --oneline -2.
What does Git print? Before you open the solution, decide whether the sports teacher's name appears in the one-line history.
Show solution
echo 'Sports fee: $700' >> fees.txt
git commit --quiet -am "Add the sports fee" -m "Requested by the sports teacher, Srihitha."
git log -1 --format="%s"
git log --oneline -2Add the sports fee
5e8b682 (HEAD -> main) Add the sports fee
3eabd0a Add the exam fee The exam office confirmed the amount.The name does not appear. The two -m options made two paragraphs with an empty line between them, so Add the sports fee is the subject and the sentence about the teacher is the body. --oneline shows only subjects, which is why the exam fee commit below it still shows its run-on subject.
Checkpoint. Run git log --oneline -20 in any project you have, and pick the three least useful messages. Rewrite each one on paper as a subject that passes the "If applied, this commit will ..." test, and add a one-sentence body explaining why.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git commit -m "Subject" -m "Body" | Commit with a subject and a body |
git commit | Write a longer message in your editor |
git log --oneline | See the subject of every commit |
git log -1 --format="%s" | Print the subject of the newest commit |
git log -1 --format="%b" | Print the body of the newest commit |
git log --oneline --grep="^fix" | Find commits whose message starts with "fix" |
git config --global commit.template <file> | Start every commit message from a template |
Was this tutorial helpful?