How do you change the message or contents of your last commit?
Use git commit --amend. It replaces the last commit with a new one, letting you edit the message and include newly staged changes. Because it rewrites history, avoid it on commits you have already pushed and shared.
Explain it simply
It is like fixing a typo on a form just before you hand it in. You do not file a second form; you correct the first one.
git commit --amend creates a brand-new commit with a new hash that takes the place of the old one. Stage a forgotten file first and it joins the commit. Add --no-edit to keep the message as it was.
The old commit is not edited, it is replaced. If teammates already pulled it, their history and yours now disagree, which is why amending shared commits causes trouble.
Example
git commit -m "Add search bar"
# oops, forgot a file and misspelt the message
git add search.css
git commit --amend -m "Add search bar with styles"
# add the file but keep the message
git commit --amend --no-editWhat to say in the interview
"git commit --amend rewrites the most recent commit, so I can fix the message or add forgotten changes. It produces a new hash, so I only do it before pushing. If it is already pushed to a shared branch I make a new commit instead, or use force-with-lease only on my own branch."
Common mistakes
- Amending a commit that is already on a shared branch and then force-pushing over a teammate's work.