Git Cheat Sheet · Changes
Amend the last commit
git commit --amend
git commit --amend replaces the last commit with a new one, which takes the content of the index. This lets you fix its message or add a forgotten file, without creating a second commit.
What it does
Replaces the last commit with a new one: corrected message, files added or removed.
A use case
You just committed and notice a typo in the message, or a forgotten file.
Warning: Rewrites history: if the commit is already pushed, a force push will be needed.
On the command line
git commit --amend -m "New message"- Fixes only the message.
git add <forgotten> && git commit --amend --no-edit- Adds a forgotten file and keeps the message.
git commit --amend --reset-author --no-edit- Updates the commit's author and date.
git push --force-with-lease- Republishes the amended commit if it had already been pushed.
In Merathon
- Stage any forgotten files.
- Check "Amend the last commit": the previous message is reused.
- Correct the message then confirm.
Frequently asked questions
How do I change a commit older than the last one?
With an interactive rebase: git rebase -i <commit>^, then the "edit" or "reword" action on that commit.
How do I undo a git commit --amend?
git reset --soft HEAD@{1} goes back to the commit from before the amend: the reflog keeps track of it.