Committing: Good Messages, Amending, Empty Commits, Authors and Dates
Everything about git commit: writing messages people can use, multi-paragraph and file-based messages, committing selected paths, amending the last commit safely, author versus committer, committing for someone else, co-authors and trailers, setting dates, empty commits and useful options.
kvi5hnuCode Sprout team member10 min readOn this page
A commit is the unit of history: what you'll read, review, revert and search later. This article covers how to write commits that stay useful, and every common option of git commit, from messages to amending, authorship and dates.
What makes a good commit message
A commit message is read far more often than it's written: in git log, in code review, in git blame when someone wonders why a line exists. A widely used shape:
Add elevation profile to trail pages
Hikers asked how much climbing each trail involves. The profile is
drawn from the park's survey data in data/elevation.csv, sampled
every 100 m, and shown below the map.
Refs: TRAIL-142
The conventions behind it:
- A short summary line, about 50 characters, at most around 72. Tools show only this line in many places:
git log --oneline, pull request lists, email subjects. - The imperative mood: "Add", "Fix", "Remove", as if completing the sentence "If applied, this commit will ___". Git itself writes messages this way, such as "Merge branch 'x'" and "Revert '...'".
- A capital letter at the start and no full stop at the end of the summary.
- A blank line between the summary and the body. Git treats the first paragraph as the subject.
- A body that explains what and why, wrapped at about 72 characters. The diff already shows how. The message should say why the change was needed and anything a reader couldn't work out from the code.
- References to the issue or ticket, if your team uses a tracker, often as trailers at the end (see below) or at the start of the summary, such as
TRAIL-142: Add elevation profile.
Messages like fix, changes, wip or updated stuff tell a reader nothing. Neither does a summary that only names the ticket, because the reader has to go elsewhere to learn what changed. Many teams also adopt Conventional Commits, summaries such as feat: add elevation profile or fix(map): correct trail colours, which tools can use to generate changelogs and version numbers.
Ways to give the message
git init -q trail-guide
cd trail-guide
echo "# Trail guide" > README.md
git add README.md
git commit -q -m "Add README"
echo "Ridge loop: 8 km" > trails.txt
git add trails.txt
git commit -q -m "Add first trail" -m "The Ridge loop is the easiest trail in the park, so it's a good start for the list."
cat > msg.txt <<'MSG'
Add lake path
Measured with the park's GPS survey, not the old signposts,
which overstate the distance by about a kilometre.
MSG
echo "Lake path: 5 km" >> trails.txt
git add trails.txt
git commit -q -F msg.txt
git log --format="--- %h%n%B"
--- 664ca45
Add lake path
Measured with the park's GPS survey, not the old signposts,
which overstate the distance by about a kilometre.
--- 5bd49a7
Add first trail
The Ridge loop is the easiest trail in the park, so it's a good start for the list.
--- 61da1f1
Add README
-m "text"gives the message directly. Each extra-madds a paragraph, with a blank line between.-F filereads the message from a file, handy for long messages written by a script.-F -reads standard input.- No option at all opens your editor (article 2), with comment lines starting with
#that list what's being committed. Git removes the comment lines when you save. Saving an empty message aborts the commit, which is the easy way to cancel. -v(--verbose) also shows the full diff in the editor, below the message, so you can review the changes while you describe them. The diff isn't included in the message.commit.templatenames a file whose contents prefill the editor, useful for a team's message format.
Committing selected files
Normally you stage, then commit. You can also name paths on the git commit command line, which commits only those paths, as they are in the working tree, and ignores anything else that's staged:
git init -q trail-guide
cd trail-guide
echo "# Trail guide" > README.md
echo "Ridge loop: 8 km" > trails.txt
git add . && git commit -q -m "Start project"
echo "Notes and maps." >> README.md
git add README.md # staged, but not part of the next commit
echo "Lake path: 5 km" >> trails.txt
git commit -q -m "Add lake path" trails.txt
echo "--- the commit contains only trails.txt"
git show --stat --format="%s" HEAD
echo "--- README.md is still staged"
git status -s
--- the commit contains only trails.txt
Add lake path
trails.txt | 1 +
1 file changed, 1 insertion(+)
--- README.md is still staged
M README.md
This is the --only behaviour, the default when you give paths. git commit --include trails.txt (-i) instead commits the named paths plus everything already staged. Folders and patterns work as paths too: git commit -m "..." trails/.
Amending the last commit
git commit --amend replaces the most recent commit with a new one. Whatever is staged is added, and you can change the message. It's the fix for a forgotten file, a typo in the message, or a last-minute correction:
git init -q trail-guide
cd trail-guide
echo "# Trail guide" > README.md
git add README.md && git commit -q -m "Add README"
echo "Ridge loop: 8 km" > trails.txt
echo "Lake path: 5 km" > lake.txt
git add trails.txt
git commit -q -m "Add trials" # typo, and lake.txt was forgotten
echo "--- before amending"
git log --oneline
git add lake.txt
git commit -q --amend -m "Add first two trails"
echo "--- after amending: a new commit has replaced the old one"
git log --oneline
git show --stat --format="%s" HEAD
--- before amending
942a18f Add trials
61da1f1 Add README
--- after amending: a new commit has replaced the old one
f7c47e7 Add first two trails
61da1f1 Add README
Add first two trails
lake.txt | 1 +
trails.txt | 1 +
2 files changed, 2 insertions(+)
Notice the commit ID changed. An amended commit is a new commit, and the old one is no longer on the branch (it stays reachable through the reflog for a while, article 26). That's harmless while the commit exists only on your computer. If you'd already pushed it, other people may have the old commit, and pushing the amended one needs a force push (article 20), which can disrupt their work. Amend freely before pushing, and think twice after.
Useful variations:
| Command | Effect |
|---|---|
git commit --amend | opens the editor with the old message, to edit it |
git commit --amend --no-edit | keeps the message, for example to add a forgotten file |
git commit --amend -m "New message" | replaces the message |
git commit --amend --reset-author --no-edit | sets the author to your current user.name and user.email, and the author date to now |
git commit --amend --author="Ana Diaz <ana@example.com>" | changes the author only |
git commit --amend --date="2026-03-01 18:00" | changes the author date |
Amending only touches the last commit. To change older commits, use an interactive rebase (article 15).
Author and committer
Every commit records two people, each with a name, an email and a date:
- The author wrote the change.
- The committer created this commit object.
They're usually the same person. They differ when someone else's work is applied, for example a patch from an email (article 45), a cherry-pick (article 16), or a rebase, which re-creates commits with you as committer but keeps the original author. --author sets the author explicitly, for crediting someone whose change you're committing:
git init -q trail-guide
cd trail-guide
echo "# Trail guide" > README.md
git add README.md
git commit -q -m "Add README" --author="Ana Diaz <ana@example.com>"
echo "Ridge loop: 8 km" > trails.txt
git add trails.txt
git commit -q -m "Add first trail" --date="2026-01-15 09:30"
echo "Lake path: 5 km" >> trails.txt
git commit -q -am "Add lake path" --author="Ana" # reuse a previous author matching "Ana"
git log --format="%h author: %an, %ad%n committer: %cn, %cd" --date=short
7a6cbfd author: Ana Diaz, 2026-03-02
committer: Sam Lee, 2026-03-02
57a8b7f author: Sam Lee, 2026-01-15
committer: Sam Lee, 2026-03-02
1719425 author: Ana Diaz, 2026-03-02
committer: Sam Lee, 2026-03-02
- The first commit is authored by Ana and committed by Sam, the sandbox's configured user.
--datesets the author date only. The committer date is still the current time, which is fixed in the sandbox. To set the committer date as well, set theGIT_COMMITTER_DATEenvironment variable:GIT_COMMITTER_DATE="2026-01-15 09:30" git commit --date="2026-01-15 09:30" ....--author="Ana", without an email in angle brackets, searches existing commits for an author matching "Ana" and reuses that name and email.
git log shows the author by default, and hosting services show both when they differ. Dates accept many formats, such as 2026-01-15 09:30, 2026-01-15T09:30:00+05:30, RFC 2822 (Thu, 15 Jan 2026 09:30:00 +0000) and relative forms such as yesterday or "3 days ago". A date without a time keeps the current time of day.
Co-authors, sign-offs and other trailers
Trailers are Key: value lines at the very end of a message, and tools read them. The common ones:
Co-authored-by: Name <email>credits other people who worked on the commit, for example in pair programming. GitHub and GitLab show co-authors on the commit.Signed-off-by: Name <email>, added bygit commit -s, certifies that you have the right to submit the change. Some projects require it, under a Developer Certificate of Origin (DCO). It's a statement, not a cryptographic signature (article 39).- Project-specific keys, such as
Refs: TRAIL-142,Reviewed-by:orFixes:.
--trailer adds one without editing the message:
git init -q trail-guide
cd trail-guide
git commit -q --allow-empty -m "Start project"
git commit -q --allow-empty -m "Trigger the deployment pipeline" \
--trailer "Co-authored-by: Ravi Kumar <ravi@example.com>" \
--trailer "Refs: TRAIL-150" -s
git log -1 --format=%B
echo "--- reading one trailer back"
git log -1 --format='%(trailers:key=Refs,valueonly)'
Trigger the deployment pipeline
Signed-off-by: Sam Lee <sam@example.com>
Co-authored-by: Ravi Kumar <ravi@example.com>
Refs: TRAIL-150
--- reading one trailer back
TRAIL-150
The example also uses --allow-empty. Git normally refuses to commit when nothing has changed, because an empty commit is usually a mistake. It's useful deliberately to trigger a CI or deployment pipeline that runs on every commit, or to start a repository with a root commit that contains no files. --allow-empty-message similarly allows a commit with no message, which is almost never a good idea.
Other options worth knowing
| Option | Effect |
|---|---|
-a | stage changes to all tracked files first (article 4) |
-p | choose hunks to commit interactively, like git add -p (article 5) |
--dry-run | show what would be committed, without committing |
-n, --no-verify | skip the pre-commit and commit-msg hooks (article 36) |
-S | sign the commit with GPG or SSH (article 39) |
--fixup=<commit> | make a commit that an interactive rebase will fold into an earlier one (article 15) |
-q | quiet: don't print the summary |
-C <commit> | reuse another commit's message and authorship |
Keeping secrets out
Never commit passwords, tokens or private keys. If you do and the commit is still only local, amend it or reset it (article 24) before pushing. Once pushed, consider the secret leaked: revoke and replace it first. Removing it from history afterwards is possible with tools such as git filter-repo (article 47), but every existing clone still has it. Secret scanners, run as a pre-commit hook or by your hosting service, help catch mistakes before they leave your computer.
What's current
--trailer(Git 2.32) adds trailers from the command line, and%(trailers)ingit logformats reads them back.git interpret-trailersprocesses them in scripts.- Hosting services understand common trailers. GitHub and GitLab show
Co-authored-by:authors, and many issue trackers linkFixes:orCloses:references. - Git 2.54 adds an experimental
git historycommand for rewriting commits, including rewording a commit's message without an interactive rebase. It's still marked experimental, so the stable tools in this course are--amendand interactive rebase.
Common questions
How long should the summary line be?
Aim for 50 characters, and stay under about 72. Hosting services and tools truncate long summaries, and git log --oneline becomes hard to scan. Put the detail in the body.
I committed to the wrong branch. Do I have to start over? No. If it's the last commit and not pushed, create a branch for it, then move the original branch back (articles 11 and 24). Or cherry-pick it to the right branch and remove it from the wrong one.
Can I amend a commit that isn't the last one?
Not with --amend. Use an interactive rebase with edit or reword for that commit (article 15), or git commit --fixup followed by git rebase -i --autosquash.
What's the difference between git commit --amend and git commit --amend --no-edit?
Both replace the last commit with what's staged. Without --no-edit, Git opens the editor so you can change the message. With it, the message stays as it was.
Why do my commits show a different author on GitHub than I expect?
GitHub links commits to accounts by the author email. If user.email doesn't match an address on your account, the commit appears unlinked or under another name. Check with git log --format='%an <%ae>' and fix user.email (article 2).
Summary
- Write a short imperative summary line, a blank line, and a body that explains why. Reference tickets with trailers or a prefix.
-mgives the message, several-moptions make paragraphs,-Freads a file, and no option opens the editor.git commit <paths>commits only those paths, ignoring other staged changes.--amendreplaces the last commit with a new one. It's safe before pushing, and needs a force push after.- Commits record an author and a committer.
--authorcredits someone else, and--datesets the author date. - Trailers such as
Co-authored-by:andSigned-off-by:go at the end of the message.--trailerand-sadd them. --allow-emptycreates a commit with no changes, for triggering pipelines.
Practice questions
Rewrite this commit message to follow the usual conventions: fixed the bug where the map didnt load on mobile because the script was loaded too early.
Answer
Fix map not loading on mobile
The map script ran before the page's map container existed on slow
mobile connections. Load it with defer, so it runs after the HTML
is parsed.
The summary is imperative, capitalized, short and has no full stop. The cause and the fix go in the body. (The fix described here is illustrative: the point is that the body explains why.)
You committed and pushed "Add trail pages", then noticed a typo in one page. A teammate has already pulled. Should you amend?
Answer
Usually not. Amending creates a new commit, so pushing it needs a force push, and your teammate's copy still has the old commit, which causes confusion or duplicated commits when they pull. Make a new commit, "Fix typo in ridge trail page", and push it normally. Amending is for commits that haven't been shared.
What does git log -1 --format='%an / %cn' print after this command, run by a user configured as Sam Lee?
git commit -m "Add lake trail" --author="Mei Chen <mei@example.com>"
Answer
Mei Chen / Sam Lee. --author sets the author, Mei, who wrote the change. The committer is the person who created the commit, taken from the configuration: Sam.
Previous: Staging Changes: add, Patch Mode, Interactive Staging, rm, mv and Unstaging
Next: Viewing Changes With git diff: Unstaged, Staged, Commits, Branches and Word Diffs

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.