Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Staging and Committing

git commit --amend command

Reference for git commit --amend: change the last commit's message, content or author, use --no-edit and --reset-author, and know why the hash changes.

git commit --amend replaces the commit at the tip of the current branch with a new one. The new commit starts from the same parent, takes its tree from the current index, and by default reuses the old message and author. Use it to fix a typo in the message, add a forgotten file, or drop a stray change.

Git never edits a commit in place: a commit's ID is a hash of its content, so any change produces a different commit. The old commit stays in the object database (reachable through the reflog) until garbage collection removes it.

Terminal
git commit --amend                 # edit the message in your editor
git commit --amend -m "New text"   # replace the message directly
git commit --amend --no-edit       # keep the message, update the content

Try it

Bash Add a forgotten file and fix the message

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Amend without touching the message

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git commit --amend [-m <msg> | -F <file> | -C <commit> | --no-edit]
                   [-a] [--reset-author] [--date=<date>] [--allow-empty]
                   [-S] [--no-verify] [--] [<pathspec>...]
Name Description
--amend Replace the tip commit instead of adding a new one.
-m <msg>, -F <file> Use a new message instead of opening the editor.
--no-edit Keep the existing message and do not open the editor.
-a, --all Stage all modified tracked files first.
--reset-author Make the committer (from your config or environment) the author as well, and refresh the author date.
--author=<name <email>> Set a specific author.
--date=<date> Override the author date.
-C <commit> Reuse the message, author and date of another commit.
--allow-empty Allow the result to have the same tree as its parent.
--no-verify Skip the pre-commit and commit-msg hooks.
-S Sign the new commit.

Returns: exit status 0 and a new commit at the branch tip.

Errors and edge cases: amending with nothing staged and an unchanged message is allowed (it creates a new commit with a new committer date); amending in a repository with no commits fails with "You have nothing to amend."; amending during a merge fails with "You are in the middle of a merge -- cannot amend."

Usage notes

  • The hash always changes. Even --no-edit with no staged changes updates the committer timestamp, so the ID differs from the original.
  • The author date is kept unless you pass --reset-author or --date. The committer date always moves to now.
  • Never amend commits you have already pushed and others have pulled. Everyone who has the old commit now has a divergent history. If you must, use git push --force-with-lease and tell your team.
  • Only the tip. To change an older commit use git rebase -i with edit or reword, or a fixup! commit and --autosquash.
  • Undo an amend by looking up the previous tip with git reflog and running git reset --soft HEAD@{1}, which keeps your work staged.
  • Hooks run again. pre-commit and commit-msg see the amended commit. The post-rewrite hook is also called with the argument amend.
  • Removing a file from the last commit: git rm --cached file, then git commit --amend --no-edit.
  • Amending a merge commit works if you are not in the middle of a merge; the parents are kept.