How to change last commit message

isabelcmdcosta

Isabel Costa

Posted on November 21, 2020

How to change last commit message

Often when making changes to code, I commit them and then have to change to commit I just made, either because I think the commit message could be better or I had to make a last-minute change to the code I wish to include in the commit.

Change last commit message

So consider I committed changes with the following command (-m to write one line commit message):

git commit -m "fix: big feature X"
Enter fullscreen mode Exit fullscreen mode

Here’s how I fix the commit, using the --amend option.

git commit --amend -m "fix: small feature X"
Enter fullscreen mode Exit fullscreen mode

In case I want to add additional code to the commit, I run git add <file> before running git commit --amend.

Verify new commit message

To check that it worked and I fixed the right commit I check the latest commit with:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

I use --oneline because I just want to see the commit messages, instead of also seeing the name and email of the author of the commit. You’ll an output similar to this, where you can see the last commit has the correct message:

1792519 (HEAD -> master, origin/master, origin/HEAD) fix: small feature X
701aa33  docs: add instructions to README
(END)
Enter fullscreen mode Exit fullscreen mode

In my environment, this will open vim, which I close by typing :q.

Fix pushed commit

In case I already pushed to the remote repository the commit I want to fix, I force the push to remote to update the commit. Preferably, you are working on a feature specific branch, which does not affect the main one (in case this messes the git history for some reason). So this is what I would run, where <branch> is the branch you are working on (e.g.: master).

git push -f origin <branch>
Enter fullscreen mode Exit fullscreen mode

Hope this helps you fix the last commit message.

💖 💪 🙅 🚩
isabelcmdcosta
Isabel Costa

Posted on November 21, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related