How to amend the last commit in Git
Amending the last commit in Git allows you to modify the most recent commit’s message or add forgotten changes without creating an additional commit in the project history. As the creator of CoreUI, a widely used open-source UI library, I’ve used git amend countless times across development workflows to perfect commits before sharing them with the team. From my expertise, the most effective approach is using git commit –amend for message changes or staging additional files before amending. This method provides clean commit history by fixing mistakes in the most recent commit without cluttering the project timeline.
Use git commit –amend to modify the last commit’s message or add additional changes to it.
# Amend commit message only
git commit --amend -m 'New improved commit message'
# Add forgotten files to last commit
git add forgotten-file.js
git commit --amend --no-edit
# Amend and change message interactively
git commit --amend
# Amend with new files and message
git add new-changes.js
git commit --amend -m 'Updated commit with additional changes'
# Force push amended commit (if already pushed)
git push --force-with-lease origin main
The –amend flag modifies the last commit instead of creating a new one. Use –no-edit to keep the existing message when only adding files. Be careful when amending commits that have been pushed to shared repositories, as this rewrites history and requires force pushing.
Best Practice Note:
This is the same git amend workflow we use in CoreUI development to maintain clean commit history and perfect commits before sharing with the team.