Skip to content

Commit 1333b61

Browse files
committed
ai(claude[commands]) Add /rebase
why: Structured rebase workflow with conflict prediction and resolution what: - Add .claude/commands/rebase.md with 5-phase rebase workflow - Detects trunk, analyzes conflicts, executes rebase, resolves conflicts, verifies quality
1 parent 1a9de91 commit 1333b61

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

.claude/commands/rebase.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
description: Rebase current branch onto trunk (origin/master or origin/main), predict and resolve conflicts
3+
allowed-tools: Bash(git:*), Bash(uv run ruff:*), Bash(uv run mypy:*), Bash(uv run pytest:*), Bash(uv run py.test:*), Read, Grep, Glob, Edit
4+
---
5+
6+
## Context
7+
8+
- Current branch: !`git branch --show-current`
9+
- Trunk branch: !`git remote show origin 2>/dev/null | grep 'HEAD branch' | awk '{print $NF}' || echo "master"`
10+
- Remote refs available: !`git remote -v 2>/dev/null | head -2`
11+
- Commits on current branch not on trunk: !`TRUNK=$(git remote show origin 2>/dev/null | grep 'HEAD branch' | awk '{print $NF}' || echo "master"); git log --oneline "origin/${TRUNK}..HEAD" 2>/dev/null || echo "(could not determine commits ahead)"`
12+
- Diff from trunk (summary): !`TRUNK=$(git remote show origin 2>/dev/null | grep 'HEAD branch' | awk '{print $NF}' || echo "master"); git diff --stat "origin/${TRUNK}" 2>/dev/null || echo "(could not diff against trunk)"`
13+
14+
## Your Task
15+
16+
Rebase the current branch onto the remote trunk branch. Follow these steps carefully, handling each phase before moving to the next.
17+
18+
### Phase 1: Detect trunk branch
19+
20+
Determine the trunk branch name from the context above (the "Trunk branch" value). Store it mentally as `TRUNK`. It will typically be `master` or `main`. If detection failed, try both `origin/master` and `origin/main` to see which exists.
21+
22+
### Phase 2: Fetch latest and analyze
23+
24+
1. Run `git fetch origin` to get the latest remote state.
25+
2. Run `git diff origin/${TRUNK}...HEAD --stat` to see what files the current branch modifies.
26+
3. Run `git diff origin/${TRUNK}...HEAD` to see the full diff of changes on this branch.
27+
4. Run `git log --oneline origin/${TRUNK}..HEAD` to see commits that will be rebased.
28+
5. Run `git diff origin/${TRUNK} -- $(git diff --name-only origin/${TRUNK}...HEAD)` to check if trunk has also modified any of the same files — these are potential conflict zones.
29+
30+
Report a brief summary of:
31+
- How many commits will be rebased
32+
- Which files were changed on this branch
33+
- Which of those files were ALSO changed on trunk (potential conflicts)
34+
- An assessment of conflict likelihood (none expected / minor / significant)
35+
36+
### Phase 3: Execute the rebase
37+
38+
Run:
39+
```
40+
git pull --rebase origin ${TRUNK} --autostash
41+
```
42+
43+
If the rebase completes cleanly (exit code 0), skip to Phase 5.
44+
45+
### Phase 4: Resolve conflicts (if any)
46+
47+
If conflicts are detected:
48+
49+
1. Run `git status` to see which files have conflicts.
50+
2. For each conflicted file:
51+
a. Read the file to see the conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`)
52+
b. Understand both sides of the conflict by examining what the branch intended vs what trunk changed
53+
c. Resolve the conflict by editing the file — preserve the intent of BOTH changes when possible. When in doubt, prefer the branch's changes (our work) but integrate trunk's changes if they're structural (renames, new parameters, etc.)
54+
d. Run `git add <file>` to mark it resolved
55+
3. Before continuing the rebase, run the project's pre-commit quality checks. Look for CLAUDE.md or AGENTS.md in the repo root to find the project's required checks. Common checks include:
56+
- Formatting: `uv run ruff format .` (or whatever the project uses)
57+
- Linting: `uv run ruff check . --fix --show-fixes`
58+
- Type checking: `uv run mypy`
59+
- Tests: `uv run pytest`
60+
If any check fails, fix the issues and re-stage with `git add` before continuing.
61+
4. Run `git rebase --continue` to proceed.
62+
5. If more conflicts appear, repeat from step 1 of this phase.
63+
6. If the rebase becomes unrecoverable, run `git rebase --abort` and report what went wrong.
64+
65+
### Phase 5: Verify final state
66+
67+
After the rebase completes successfully:
68+
69+
1. Run `git log --oneline -10` to confirm the rebased commit history looks correct.
70+
2. Run `git status` to confirm a clean working tree.
71+
3. Run the project's quality checks one final time (format, lint, typecheck, test) as described in Phase 4 step 3.
72+
4. Report the results: how many commits were rebased, whether any conflicts were resolved, and the final state of all quality checks.
73+
74+
Do NOT force-push. Only report the final state and let the user decide on the next step.

0 commit comments

Comments
 (0)