Introduction
Picture this: you’re on a coding spree, rushing to fulfill a project deadline. You’ve just committed your changes to Git. All of a sudden, you notice a typo in your last commit message! But don’t worry, you have the ability to correct it in the realm of Git.
Modify the last commit message
To update the last commit message in Git, you can use the --amend
option with the git commit
command. The following command illustrates it.
git commit --amend -m "New and improved commit message"
You should replace "New and improved commit message"
with the updated commit message you want to use.
Pushing the changes
You are safe if you did not push your previous commit, which contained a typo in the commit message. You can now push your changes.
If you have already pushed the commit to a remote repository, you have to do a force push to update the repository. You should be extremely careful when using git push --force
because it rewrites the commit history, and it can potentially cause issues for collaborators. If you’re working on a shared branch, it’s a good practice to coordinate with your team before force pushing.
It’s better to use --force-with-lease
, which is a safer alternative to --force
. With this option, a force push will be done, only if the remote branch has not been updated by someone else.
git push --force-with-lease <remote> <branch>