--- source_keys: - org-commit-conventions - context7-git-htmldocs --- # Rewriting existing commits Every flow on this page rewrites history. None of them runs before the caller has explicitly approved it, and none is followed by a force-push to `main`/`master` — refuse that and explain why instead. ## Amend the last commit 1. Stage the new changes, or the changes that undo something. 2. Run `rtk git commit --amend`, adding `--no-edit` when the message stays as it is. 3. If the message should change, show the current one and prompt for the replacement. 4. The branch has now diverged from its remote. Amending is safe only on a branch nobody else has based work on; on `main`/`master`, refuse the force-push and explain, rather than warning and proceeding. ## Fold a commit into an earlier one (autosquash — preferred) Prefer this whenever a commit is written to be folded, because git does the marking: 1. `rtk git commit --fixup=` keeps the target's message; `rtk git commit --squash=` lets you edit the combined message later. Both prefix the message with `fixup!`/`squash!` and name the target commit. 2. Get explicit approval — the rebase still rewrites history. 3. Run `rtk git rebase --autosquash HEAD~N`, or `-i --autosquash` to review the plan first. Git reorders the tagged commits against their targets automatically. ## Squash by hand (interactive rebase) Use this when the commits were not tagged at commit time. **Interactive rebase has no undo once `rebase -i` starts — `git reflog` is the recovery path.** 1. Identify the commits to squash — typically the last N on the current branch. 2. Get explicit approval. 3. Run `rtk git rebase -i HEAD~N`, marking the older commits `squash` to keep their messages for editing, or `fixup` to discard them. 4. Compose the combined message when the rebase stops to ask. For a non-trivial combined message, follow the structure in `references/commit-template.md`. ## When a rebase halts on a conflict Offer conflict resolution or `rtk git rebase --abort`. Do not resolve conflicts automatically without confirmation.