Interview Q&A / Git & GitHub

BeginnerQuick answer1 min readUpdated 3 Oct 2026

How do you change the message or contents of your last commit?

SHORT ANSWER

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

bash
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-edit

What 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.

#git#amend#commit

Related questions

Practise more Git & GitHub questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Git & GitHub quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.