git-rebase
Rebase the current branch onto a target branch. If the user provides an argument, use it as the target branch; otherwise use the remote default branch.
Workflow
- Gather context:
```bash
subject=$(git branch --show-current)
target=<argument-or-default-branch>
git status --porcelain=v1
```
- Stop if:
subjectequalstarget.- The working tree is dirty. Tell the user to commit or stash first.
- Fetch remote-tracking refs without touching the local target branch:
```bash
git fetch origin --prune
target_ref="origin/${target}"
```
Never use git fetch origin "${target}:${target}" for a branch that may be checked out in another worktree. If a local target branch must be updated, do that only in the worktree that currently has it checked out.
- Analyze divergence:
```bash
merge_base=$(git merge-base HEAD "${target_ref}")
git log --oneline "${merge_base}..HEAD"
git log --oneline "${merge_base}..${target_ref}"
git diff --name-only "${merge_base}" HEAD
git diff --name-only "${merge_base}" "${target_ref}"
git merge-base --is-ancestor "${merge_base}" "${target_ref}" && echo base_on_target=yes || echo base_on_target=no
```
- Choose strategy:
| Condition | Strategy |
| --- | --- |
| Merge-base is not on target lineage | rebase --onto |
| Otherwise | simple rebase |
Show the analysis and selected strategy before proceeding.
- Execute:
- Simple:
git rebase "${target_ref}" - Onto:
git rebase --onto "${target_ref}" "${merge_base}"
Both rebase subject in place. Never create, switch, rename, or delete a branch. Branch switching and git reset --hard may be blocked in a main checkout, so avoid them. Simple rebase also skips commits whose patch is already in the target (rebase-merged or cherry-picked commits that got new hashes). Squash-merged commits are not skipped; they conflict or go empty and are handled in step 7.
- On conflicts, follow the
git-resolve-conflictsskill (~/.agents/skills/git-resolve-conflicts/SKILL.md). It resolves each path, continues the rebase (skipping commits already upstream), and repeats until the operation completes, pausing only where it says to ask the user. Then continue with step 8.
- After success, set tracking and push:
```bash
git branch --set-upstream-to="origin/${subject}" "${subject}"
git push --force-with-lease -u origin "${subject}:${subject}"
git branch -vv
```
- Report strategy, branch, and tracking.