Sometimes we wish to rewrite history. Unlike in the real-world, Git provides various options to rewrite git history.
In this post, we will cover how to rewrite the latest commit in Git.
Rewrite latest git commit message
Git provides the option to rewrite the most recent commit message.
git commit --amend
This command will open the editor with the latest commit message. You can then change the commit message and push it.
In the above example, you can see that the latest commit has a typo error in the message. You can fix that typo with amend
The amend command will open an editor. Now you can edit the commit message here and save it.
Now let’s see the git log
The typo is fixed. And also note that the latest commit hash is also changed.
So whenever you are doing amend Git will basically rewrite the entire commit and generates a new hash for it. This is very important to understand.
There is also a shorthand for amend
git commit --amend -m "Your rewrite message"
You can pass the message directly with -m attribute. And this command will directly commit the change and won’t open the editor to edit.
Now the tricky part. Assume that you have committed some changes with wrong commit message and have pushed to the remote. After realizing the mistake you have changed the commit message with amend and you are trying to push the changes to remote.
Git won’t allow you to push the changes and you will see the error message as below.
Git is rejecting to push and gives warning that the local branch is behind the remote.
It means that there are new changes in the remote and those changes are not present in the local. So you should probably fix it by pulling the changes from local.
This is happening because we have used amend to change the latest commit and the remote is not aware of that.
So you should tell Git that the local changes are meant to be the updated changes and have to push it anyway.
You can achieve this by
git push origin develop --force
The above command will force the Git to push the local changes to remote.
Beware of —force
It is recommended not to use amend if you have already pushed the changes to remote and other developers have already started using those changes.
Because —force is basically rewriting the git history in remote itself. So any changes that were pushed to remote meanwhile will be erased from the history and will be gone permanently.
So it is always advisable to use amend when you haven’t pushed the changes to remote or if you are pretty confident that no other developers have started using those changes or no others have pushed any new changes to the current branch.
Adding more changes to the latest commit using amend
It is also possible to add more changes to the recent commit using amend. So that you don’t need to add a new commit for missed out changes.
Assume that you have missed adding some changes that have to be the part of the last commit you have done. You can try
git add file_one.txt git commit --amend
First, you have to add the files and then you can use —amend to add those changes to the last commit. This command will open the editor and you can add your updated commit message. Now the changes are added to the recent commit itself.
You can also use
git commit --amend -m "Updated commit message"
As already discussed, this is a shorthand and will accept the commit message.
Sometimes, you don’t need to update the commit message itself. You just need to add more changes to the latest commit.
This is possible by
git commit --amend --no-edit
This command will add the staged changes and leave the commit message unchanged.
But remember, amend will always rewrite the entire commit and so it generates a new commit hash for it.
As discussed earlier, it’s not wise to add changes to the latest commit if the changes are already pushed to the remote.
Make sure that you won’t affect other’s work by using amend. Be careful about using it.
There are also ways of rewriting old commits and also rewriting multiple commits in Git. Those topics will be covered in upcoming posts.
That’s it for today :)
If you got any queries, feedback you can reply to this email or leave a comment.
Thank you and see you next week :) :)