Categories:Viewed: 51 - Published at: 4 months ago

Mistakes are common, we all make them. That's why they put erasers on pencils. And this is no different with tools like Git. While some changes can be difficult to undo, there is usually a way. So what if you want to modify an existing commit message? Luckily this is simple to do in Git, but the method may change a bit depending on some factors.

Fix Last Commit, Not Yet Pushed to Remote

This is the simplest case since there is no need to modify a remote repository, you just need to modify your own. In this case, you can use the following command:

$ git commit --amend

Running this command will open up an editor where you can modify the last commit message. Once you're done and save/close the editor, the commit message will be changed. Personally, I prefer to do most things on the command line, which you can do by adding an argument and message to the command above:

$ git commit --amend -m "Added a new file"

Doing it this way won't open the editor, but instead it'll just modify the commit message immediately.

Fix Last Commit, Pushed to Remote

This is another common case, and is a bit more difficult to fix, not only because we need to modify our own repository, but we also need to modify the remote repository, which could be further ahead than your local branch. The first step is to amend the last commit, just like we did in the previous section:

$ git commit --amend -m "Added a new file"

Then, you need to push these changes to the remote repository. However, this must be done using the --force flag.

$ git push <remote> <branch> --force


We need to do it this way in order to overwrite the remote repository with our own local state. Be aware that by forcing the push, you'll also lose any commits on the local branch that you don't already have in your local branch. Proceed with caution! If you can catch the mistake fast enough, then making the change won't be a problem. Otherwise, you may need to find another solution to avoid losing any changes made after the commit message you're wanting to change.

Use Interactive Rebase

Another option, which is more flexible than the previous solutions described, is to use an interactive rebase. Doing this allows you to inspect the last N commits and make changes to them, including picking, squashing, etc. To start the rebase, you can use the following command:

$ git rebase -i HEAD~n

Here, n is the number of commits you want to inspect and edit. You'll then see a list of commits, similar to this:

$ $ git rebase -i HEAD~4
pick 788ebf0 Oops, I forgot to add a new file
pick ced1329 Fixed a super critical bug
pick 5e9cdc5 Added a new file

# Rebase d05209d..5e9cdc5 onto d05209d (4 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
# d, drop = remove commit
# These lines can be re-ordered; they are executed from top to bottom.
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
# Note that empty commits are commented out

As you can see all of the last N commits are listed, and you can modify their messages as needed.