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 d4e5f6g Add signup page
# pick h7i8j9k Fix typo in login
# pick l0m1n2o 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.