Edit an earlier Git commit
Pause an interactive rebase at a selected commit, amend its content or message, and understand how rewriting affects descendants and collaborators.
Use an interactive rebase with edit to change an earlier commit. Git pauses at that commit so you can amend it, then replays the remaining commits on the revised history.
Prepare a private branch
Check that the working tree is clean and create a backup ref:
git status --short
git branch backup-before-edit
git rebase -i a1b2c3d^
Replace the ID with the commit you want to change. The ^ starts the rebase at its parent, so the target appears in the todo list. Use git rebase -i --root if the target is the root commit. Inspect the selected history before proceeding, especially if it contains merges.
Change the target line from pick to edit:
edit a1b2c3d Fix request validation
pick e4f5a6b Add validation tests
Amend and continue
When Git pauses, edit the relevant files:
git add -- src/validation.js
git diff --cached
git commit --amend --no-edit
git rebase --continue
If later commits conflict, resolve each conflict, stage the resolution, and continue again. While the rebase is active, git rebase --abort restores its starting history. Preserve any new work you want to keep before abandoning the operation.
An amended commit has a new identity. Descendants rebuilt on that parent also change identity, even if their visible file edits stay the same. Test the final branch rather than assuming a successful replay proves correctness.
Change only the message
Use reword instead of edit when only the target's message needs changing. For the current tip, git commit --amend is simpler; remember that ordinary amend includes staged content. To edit only the tip's message without incorporating unrelated staged changes, use git commit --amend --only.
Handle merge topology deliberately
Ordinary rebase omits merge commits from its todo list and normally produces a linear replay. --rebase-merges attempts to recreate the branch structure and uses additional todo commands such as label, reset, and merge. Existing merge resolutions may need to be reapplied. Editing a merge itself requires understanding that todo structure; a simple edit recipe is not a complete merge-preservation procedure.
Share rewritten history carefully
Prefer a corrective commit on a shared branch. For an agreed rewrite of a personal published branch, inspect the remote and use an explicit --force-with-lease expectation when updating it. The lease protects against an unexpected remote ref value, not every collaborator's unpublished work.
The backup branch keeps the original commits reachable. After a completed rebase, inspect that backup before any reset; reset --hard would discard tracked local changes. Coordinate recovery with teammates instead of instructing them to discard their work blindly.
See git-rebase, git-commit, and git-push.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan