How to amend the last commit in Git
Modifying the last commit is essential for fixing typos in commit messages or adding forgotten changes before pushing to shared repositories.
As the creator of CoreUI, a widely used open-source UI library, I’ve amended countless commits to maintain clean project history over 25 years of development.
From my expertise, the safest approach is using git commit --amend, which modifies the most recent commit without creating a new one.
This keeps the commit history clean and is safe to use before pushing changes to remote repositories.
Use git commit --amend to modify the most recent commit message or add changes.
git add forgotten-file.js
git commit --amend -m "Add user authentication with proper validation"
Here the first command stages a forgotten file, and the second command amends the last commit to include this file while updating the commit message. The --amend flag modifies the existing commit instead of creating a new one. Use -m to provide a new message, or omit it to edit the existing message in your default editor.
Best Practice Note:
This is the same commit correction workflow we use in CoreUI development for maintaining clean project history. Never amend commits that have already been pushed to shared repositories, as this rewrites history and can cause conflicts for other developers. Only amend local commits before pushing.



