It happens to every engineer: you hit Enter on git commit -m "feat: implement payment gateway", and three seconds later you realize you forgot to stage config.stripe.js, or you made a glaring spelling mistake in your commit title. The --amend flag is Git's built-in eraser for the latest commit.
If you don't need to change any files and only want to correct the commit message, run:
# Open your default editor to rewrite the message:
$ git commit --amend
# Or supply the corrected message directly in terminal:
$ git commit --amend -m "feat(payment): implement Stripe payment gateway"
If you forgot to include a file (or made a quick two-line bugfix that belongs in the previous commit):
git add.git commit --amend --no-edit.# Stage the forgotten file:
$ git add src/config/stripe.js
# Merge it into the last commit without prompting for a new message:
$ git commit --amend --no-edit
The --no-edit flag tells Git to keep the existing commit message while absorbing the newly staged changes directly into the previous commit.
It is vital to understand that Git does NOT physically edit the existing commit object. Commits in Git are immutable. When you run --amend, Git creates a brand-new commit object with a new SHA-1 hash and points the branch reference to it, abandoning the old commit.
Because amending generates a new commit hash, if you already pushed the original commit to a shared GitHub repository, pushing the amended commit will be rejected with an [error: failed to push some refs] message. Forcing the push with --force will overwrite your teammates' commit history and cause merge chaos. Only amend local, private commits!
Strengthen your version control mastery by studying these connected classes:
Review best practices for commit message formatting and atomic snapshots.
Modify older commits beyond just the most recent HEAD commit.
Reset HEAD to restructure multiple commits rather than amending just one.
See previous versions of amended commits stored in .git/logs/HEAD.
Advance your software engineering skills with the next topic in our synchronized curriculum.
Continue to File Management (mv & rm) → Previous: ← git reset vs git revert | Full Syllabus