Change the Last Commit Message
For your latest local commit, use --amend to replace its message. The --only option leaves any currently staged changes out of the replacement commit; those changes remain staged for later.
git commit --amend --only -m "Corrected commit message" Add a Forgotten File or Correction
To include a missing file or correct a file in the last commit, stage the desired changes and amend the commit's contents.
To include only some edits from a file, stage selected lines instead of staging the entire file.
- Stage the File: This stages its current contents, including any edits you made since the last commit.
git add -- <file> - Review the Staged Changes: Everything currently staged will be included in the amendment. Unstage any unrelated changes first.
git diff --cached - Amend the Commit: Use
--no-editto keep its existing message.
git commit --amend --no-edit Check the Result
Review the replacement commit's message and complete diff, then check which changes remain uncommitted.
git show HEAD
git status --short Considerations
- Amending creates a new commit ID and replaces the latest commit on the current branch. Earlier commits are unchanged.
- The previous commit can usually be found in
git reflogif you need to recover from a mistaken amendment. - If you already pushed the commit to a shared branch, put content corrections in a new commit. Changing a published message also rewrites history and requires coordination with collaborators.
- To undo the latest commit while keeping all of its changes staged, see Undo Last Commits.