Edit commit

2 snippets in Git

Also written as edit commits

GitGit

Amend the Last Commit

Git · Undoing Changes
Syntax
git commit --amend [-m "new message"]
Example
# Fix the commit message:
git commit --amend -m "Add user auth endpoint with JWT support"

# Add forgotten files to the last commit:
git add forgotten-file.js
git commit --amend --no-edit
Output
[main b2c3d4e] Add user auth endpoint with JWT support

Note WARNING: Amending rewrites the commit hash. Never amend a commit that has already been pushed to a shared branch - other developers who pulled the original commit will have conflicts. Use git revert instead for pushed commits.

Interactive Rebase

Git · Rebasing
Syntax
git rebase -i HEAD~<n>
git rebase -i <commit>
Example
# Rewrite the last 4 commits:
git rebase -i HEAD~4

# Editor opens with:
# pick a1b2c3d Add login page
# pick d4e5f6a Add signup page
# pick b7c8d9e Fix typo in login
# pick c0d1e2f Update styles
#
# Change 'pick' to: squash, reword, edit, drop, etc.

Note Commands: pick (keep as-is), reword (change message), squash (merge into previous commit), fixup (squash but discard message), edit (pause to modify), drop (delete commit). Save and close the editor to execute.

Frequently asked questions

How does Git handle edit commit?
Git covers this with 2 copy-ready snippets on this page. The "Amend the Last Commit" snippet in Git uses `git commit --amend [-m "new message"]`.
Which command does the Git example use?
The "Amend the Last Commit" snippet uses `git commit --amend [-m "new message"]`, from the Undoing Changes section of the Git cheat sheet.
What other Git snippets are shown for "edit commit"?
Besides "Amend the Last Commit", this page also shows "Interactive Rebase".
Is there anything to watch out for?
Yes. For "Amend the Last Commit": WARNING: Amending rewrites the commit hash. Never amend a commit that has already been pushed to a shared branch - other developers who pulled the original commit will have conflicts. Use git revert instead for pushed commits.