Oscar the owl and Vershd logo

Make Git Change a Commit Message

30th March 2020

This is part of a Git Tips series. For the complete set for beginners and intermediate Git users, see our white paper.

The Problem

Do you need to make Git to change a commit message either in the the last commit, or quite a few commits? And what about those you’ve already pushed to the remote repository? And how quickly can this be fixed before someone sees the swear word you forgot to take out?

In these examples, your remote repository is called origin, as is the convention, and the commit you want to change is on a branch called my-branch. Let's dive in.

The Solution to Change the Latest Commit Message

This immediately alters the message, requiring no confirmation:
git commit --amend -m "Added the new feature without no swearing"
Or this opens an editor to write a multi-line/formatted message:
git commit --amend
Optional: To specify an editor for Git to open, type in something like this, always using forward slashes, even on Windows:
git config --global core.editor "'C:/My/Favourite/Editor' -AnEditorParam"
Finally, this pushes all your changes to the remote branch. (Using -f is the same as --force)
Warning: You will lose all commits on the remote branch that aren’t in the local repository, so check first. And the commit you altered will have a new SHA (hash), which could cause problems for colleagues who are using this branch, if you have pushed that commit already.
git push origin my-branch -f

The Solution to Change Multiple Commit Messages

In this example, we’re trying to change the message in 3 of the last 5 commits.

This opens up your selected text editor for interactive rebasing:
git rebase -i HEAD~5
When the editor has opened, as shown in the example below, replace pick with reword next to the commits you wish to amend.
Then close the editor to start the rewriting process.
pick 975de9e feat(World Peace): started dealing with problems
reword 9958258 feat(World Peace): tried to force world peace
reword 5c428a2 feat(World Peace): added in some possible solutions
pick ca34dd6 feat(World Peace): removed 2 solutions
reword fc273f4 feat(World Peace): added the Greta Thunberg solution

# Rebase 1c8f5e0..fc273f4 onto ca34dd6 (5 commands)
# Commands:
# p, pick  = use commit
# r, reword  = use commit, but edit the commit message
# e, edit  = use commit, but stop for amending
# s, squash  = use commit, but meld into previous commit
# f, fixup  = like "squash", but discard this commit's log message
# x, exec  = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop  = remove commit
# l, label 
You can now change the message of each commit in turn, following the process as instructed.

Pushing to the Horizon

Do you want to use Git far more easily, with no commands or parameters to remember, and with a clear overview of your repository, plus the ability to preview changes? Then make your life easier, get a free download of Vershd, the effortless Git GUI.
Designed from the ground up to prevent errors, accidental deletions, and having to write all of this morning's code. Again.

Features include:
  • Rewind is simpler and easier than using Revert or Reset. It winds the clock back on commits, letting you choose whether to keep pending files or not.
  • Advanced Context Menus. Right clicking on a commit lets you merge, cherry pick, or create a branch there and then. Files have many ways to compare them to either historic or working files on your desktop.
  • See everyone's work in the clearest way possible. Branches shows branches and their commits, where you can easily pull, push, cherry pick and more. Pending shows what you can commit or stash. Files shows commits' details. Stashes helps you apply, view and delete your stashes.
Oscar the owl and Vershd logo
 © 2020 Blightysoft
Made in England
St. George's flag of EnglandThe Union Jack, the flag of the United KingdomThe European Union flagThe United Nations flag
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram