| name | git-rebase-patterns |
| description | Advanced git rebase — linear history, stacked PRs, --update-refs, --onto. Use when rebasing branches, cleaning history, managing PR stacks, or fixing merge-heavy branches. |
| user-invocable | false |
| allowed-tools | Bash, Read |
| created | "2026-01-30T00:00:00.000Z" |
| modified | "2026-05-09T00:00:00.000Z" |
| reviewed | "2026-01-30T00:00:00.000Z" |
Git Rebase Patterns
Advanced rebase techniques for maintaining linear history, managing stacked PRs, and cleaning up commit history.
When to Use This Skill
| Use this skill when... | Use something else when... |
|---|
| Rebasing feature branches onto updated main | Creating branches → git-branch-naming |
| Cleaning up commit history before PR | Creating PRs → git-branch-pr-workflow |
| Managing stacked PRs (PR chains) | Simple commits → git-commit-workflow |
| Converting merge-heavy branches to linear | Basic git operations → git-cli-agentic |
Linear History Basics
Trunk-Based Development
git switch main
git pull origin main
git switch -c feat/user-auth
git switch main && git pull
git switch feat/user-auth
git rebase main
git rebase -i main
git push -u origin feat/user-auth
Squash Merge Strategy
Maintain linear main branch history:
git switch main
git merge --squash feat/user-auth
git commit -m "feat: add user authentication system
- Implement JWT token validation
- Add login/logout endpoints
- Create user session management
Closes #123"
Interactive Rebase Workflow
Clean up commits before sharing:
git rebase -i HEAD~3
pick a1b2c3d feat: add login form
fixup d4e5f6g fix typo in login form
squash g7h8i9j add form validation
reword j1k2l3m implement JWT tokens
Advanced Rebase Flags
Reapply Cherry-Picks (--reapply-cherry-picks)
Problem: After merging trunk into your feature branch multiple times (via git merge main), you want to rebase onto fresh trunk. Default rebase behavior may create conflicts or duplicate commits.
Solution: --reapply-cherry-picks detects commits that were already applied via merge and drops the merge commits, keeping only your original changes.
git log --oneline
git fetch origin
git rebase --reapply-cherry-picks origin/main
When to use:
- After merging trunk into your branch to resolve conflicts
- Converting a merge-heavy branch to linear history
- Before creating a PR to clean up integration merges
Update Refs (--update-refs)
Problem: With stacked PRs (PR chains), rebasing one branch requires manually rebasing all dependent branches. Moving commits between branches in the stack is painful.
Solution: --update-refs automatically updates all branches in the chain when you rebase. Combined with interactive rebase, you can reorganize commits across your entire PR stack in one operation.
git switch feat/auth-refresh
git rebase --update-refs main
git rebase -i --update-refs main
Stacked PR workflow with update-refs:
git switch main
git pull origin main
git switch -c feat/auth-base
git push -u origin feat/auth-base
git switch -c feat/auth-oauth
git push -u origin feat/auth-oauth
git switch -c feat/auth-refresh
git push -u origin feat/auth-refresh
git fetch origin
git switch feat/auth-refresh
git rebase --update-refs origin/main
git push --force-with-lease origin feat/auth-base
git push --force-with-lease origin feat/auth-oauth
git push --force-with-lease origin feat/auth-refresh
When to use:
- Managing stacked PRs (PR chains with dependencies)
- Reorganizing commits across multiple branches
- Keeping branch hierarchies in sync during rebase
Explicit Base Control (--onto)
Problem: Git's automatic base detection can be ambiguous or incorrect. You want precise control over where commits are rebased.
Solution: --onto explicitly specifies the new base, bypassing Git's base detection heuristics.
git rebase --onto origin/develop HEAD~5
git switch feat/payment
git rebase --onto main develop feat/payment
git rebase --onto main abc123^ def456
git switch -c feat/auth-ui feat/auth
git rebase --onto main feat/auth~3 feat/auth-ui
Common --onto patterns:
| Pattern | Command | Use Case |
|---|
| Last N commits on trunk | git rebase --onto origin/main HEAD~N | Rebase recent work onto updated trunk |
| Change branch base | git rebase --onto <new-base> <old-base> | Fix branch created from wrong base |
| Extract commit range | git rebase --onto <target> <start>^ <end> | Move specific commits to new location |
| Interactive with onto | git rebase -i --onto <base> HEAD~N | Clean up and rebase last N commits |
When to use:
- When you don't trust Git's automatic base detection
- Rebasing a specific number of recent commits (
HEAD~N pattern)
- Changing the base of a branch after it was created
- Extracting a subset of commits to a new location
Combining Advanced Flags
These flags work together for powerful workflows:
git rebase --reapply-cherry-picks --update-refs origin/main
git rebase -i --onto origin/main HEAD~10 --update-refs
git rebase --reapply-cherry-picks --update-refs --onto origin/develop origin/main
Conflict Resolution
git rebase main
git add resolved-file.txt
git rebase --continue
git rebase --abort
git merge main
Safe Force Pushing
git push --force-with-lease origin feat/branch-name
git config alias.pushf 'push --force-with-lease'
Quick Reference
| Operation | Command |
|---|
| Rebase onto main | git rebase main |
| Interactive rebase | git rebase -i HEAD~N |
| Rebase with cherry-pick cleanup | git rebase --reapply-cherry-picks origin/main |
| Rebase stacked PRs | git rebase --update-refs main |
| Rebase last N commits | git rebase --onto origin/main HEAD~N |
| Change branch base | git rebase --onto <new> <old> |
| Abort failed rebase | git rebase --abort |
| Continue after conflict | git rebase --continue |
| Skip problematic commit | git rebase --skip |
Troubleshooting
Rebase Conflicts Are Too Complex
git rebase --abort
git merge main
Branch Diverged from Remote
git pull --rebase origin feat/branch-name
git fetch origin
git reset --hard origin/feat/branch-name
Lost Commits After Rebase
git reflog
git reset --hard HEAD@{N}