If you are experienced with Git, then you should aware of how important to create commits for your project. If a commit message includes unclear, incorrect, or sensitive information, you can amend it locally and push a new commit with a new message to GitHub.
In this tutorial, we are going to talk completely about how to Amend Git Commit Message easily. As it can be possible in multiple different cases, also by using various suitable Git commands so make sure to pick the one who suits your needs the most.
- The Git Commit Amend Command
- Commit has not been pushed online
- How to Amend the latest Git Commit Message?
- Amend Older or Multiple Git Commit Message using rebase
- Amend Last Git Commit Message
You can also check Google Sheets Tips – New Google Spreadsheet Hacks, Tricks with Examples
This command will allow you to change files in your last commit or your commit message. Your old commit is replaced with a new commit that has its own ID.
The following syntax is for the amend command:
git commit --amend
Amending a commit does not simply change a commit. It substitutes it with a new commit which will have its own ID.
In case the commit only exists in your local repository which has not been pushed to GitHub, you can amend the commit message with the
git commit --amendcommand:
- Navigate to the repository that includes the commit you need to amend on the command line.
git commit --amendand click on Enter
- Later, Edit the commit message and save the commit in your text editor.
- You can add a co-author by adding a trailer to the commit.
- You can create commits on behalf of your organization by adding a trailer to the commit.
The new commit and message will seem on GitHub the next time you push.
Also Check: How To Undo Last Git Commit
Are you looking for the process of amending the latest Git commit message? This section will explain you clearly. In case the message to be amended is for the latest commit to the repository, then the following commands are to be performed:
git commit --amend -m "New message" git push --force repository-name branch-name
Remember that using –force is not supported, as this changes the history of your repository. If you force push, people who have already cloned your repository will have to manually fix their local history.
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 the 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 an 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)
That’s it! You successfully amended the message of one of your Git commits in your repository.
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.
To amend the message of your last Git commit, you can simply execute the “git commit” command with the “–amend” option. You can also add the “-m” option and specify the new commit message directly.
$ 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 learned how you can easily amend a Git commit message whether it has already been pushed or not.
You learned 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!