If you are familiar with Git, you are probably creating a lot of commits everyday for your projects.
If you followed our previous articles on writing Git commit messages, you know how important it can be to write insightful and descriptive commit messages.
However, in some cases, you probably created your commits already and you will need to amend your Git commit message.
In this tutorial, you will learn how you can amend your Git commit message easily.
There are multiple different cases, so make sure to choose the one who suits your needs the most.
Amend Git Commit Message using rebase
The easiest way to amend a Git commit message is to use the “git rebase” command with the “-i” option and the SHA of the commit before the one to be amended.
You can also choose to amend a commit message based on its position compared to HEAD.
$ git rebase -i <sha_commit> $ git rebase -i HEAD~1 (to amend the top commit) $ git rebase -i HEAD~2 (to amend one commit before HEAD)
As an example, let’s say that you have a commit in your history that you want to amend.
The first thing you would have to do is to identify the SHA of the commit to be amended
$ git log --oneline --graph 7a9ad7f version 2 commit 98a14be Version 2 commit 53a7dcf Version 1.0 commit 0a9e448 added files bd6903f first commit
In this case, we want to modify the message for second commit, located right after the first commit of the repository.
Note : in Git, you don’t need to specify the complete SHA for a commit, Git is smart enough to find the commit based on a small version of it.
First, run the “git rebase” command and make sure to specify the SHA for the commit located right before the one to be amended.
In this case, this is the first commit of the repository, having a SHA of bd6903f
$ git rebase -i bd6903f
From there, you should be presented with an interactive window showing the different commits of your history.
As you can see, every line is prefixed with the keyword “pick”.
Identify the commit to be modified and replace the pick keyword with the “reword” keyword.
Save the file and exit the current editor : by writing the “reword” option, a new editor will open for you to rename the commit message of the commit selected.
Write an insightful and descriptive commit message and save your changes again.
Save your changes again and your Git commit message should now be amended locally.
$ git log --oneline --graph * 0a658ea version 2 commit * 0085d37 Version 2 commit * 40630e3 Version 1.0 commit * 0d07197 This is a new commit message. * bd6903f first commit
In order for the changes to be saved on the Git repository, you have to push your changes using “git push” with the “-f” option for force.
$ git push -f + 7a9ad7f...0a658ea master -> master (forced update)
You successfully amended the message of one of your Git commits in your repository.
Amend Last Git Commit Message
If you only want to amend the last Git commit message of your repository, there is a quicker way than having to rebase your Git history.
$ git commit --amend (will open your default editor) $ git commit --amend -m <message>
As an example, let’s say that you want to amend the message of your last Git commit.
$ git log --oneline --graph * 0a658ea Last commit message * 0085d37 Version 2 commit * 40630e3 Version 1.0 commit * 0d07197 This is a new commit message. * bd6903f first commit
Execute the “git commit” command and make sure to specify the “–amend” option.
$ git commit --amend
Amend the commit message, save and exit the editor for the changes to be applied.
[master f711e51] Amending the last commit of the history. Date: Fri Nov 29 06:33:00 2019 -0500 1 file changed, 1 insertion(+) $ git log --oneline --graph * f711e51 (HEAD -> master) Amending the last commit of the history. * 0085d37 Version 2 commit * 40630e3 Version 1.0 commit * 0d07197 This is a new commit message. * bd6903f first commit
Again, you will need to push your changes to the remote in order for other developers to get your changes. You will need to specify the “force” option as you did in the first section.
$ git push --force + 0a658ea...f711e51 master -> master (forced update)
Your Git commit message should now be amended on the remote repository.
In this tutorial, you learnt how you can easily amend a Git commit message whether it has already been pushed or not.
You learnt that you can either modify the last Git commit with the “–amend” option, or you can modify older commits with the “rebase” command.
If changes were already pushed, you will have to update them using the “git push” command and the force option.
If you are interested in Software Engineering or in Git, we have a complete section dedicated to it on the website, so make sure to check it out!