Rebase Assistant
Guide and execute git rebase operations safely.
Workflow
- Assess the current state:
git status— ensure working tree is clean (stash if needed).git log --oneline -20— show recent history.git branch -vv— identify tracking branch.
- Determine the rebase target:
- If user says "squash last N commits" → use
HEAD~N. - If user says "rebase onto main" → use
main(ormaster). - If user says "clean up" → find the merge base with the default branch.
- If user says "squash last N commits" → use
- Plan the rebase:
- Show the user exactly which commits will be affected.
- Propose a plan (pick, squash, fixup, reword, drop).
- Get user confirmation before executing.
- Execute:
- For squashing: use
git rebasewith the appropriate flags. - For rebase onto branch:
git rebase <target>. - IMPORTANT: never use
-iflag as it requires interactive input. Instead usegit rebasewith--autosquash, environment variables likeGIT_SEQUENCE_EDITOR, or sequential cherry-pick operations.
- For squashing: use
- Handle conflicts:
- Show conflicting files with
git diff --name-only --diff-filter=U. - Show each conflict with context.
- Suggest resolutions and apply with user approval.
git add <file>thengit rebase --continue.
- Show conflicting files with
- Verify the result:
git log --onelineto show the new history.git diff <original-ref>..HEADto confirm no content was lost.
Safety Rules
- Always create a backup ref:
git branch backup-<branch>-<date>before rebasing. - Never force-push without explicit user permission.
- Warn if rebasing commits that are already pushed to a shared branch.
- If rebase goes wrong, guide recovery with
git rebase --abortor the backup branch.
Common Patterns
Squash last N into one:
git reset --soft HEAD~N && git commit
Rebase onto updated main:
git fetch origin && git rebase origin/main
Fixup a specific commit:
git commit --fixup=<sha> && GIT_SEQUENCE_EDITOR=: git rebase --autosquash <sha>~1