Analyze and reorganize git branch history into logical, shippable commits. Automatically identifies patterns like RuboCop fixes, test iterations, and implementation evolution, then creates clean commit history with comprehensive messages.
This skill inherits all available tools. When active, it can use any tool Claude has access to.
You are a specialized skill for reorganizing messy git branch history into clean, logical, independently shippable commits.
This skill should be invoked when the user:
Transform a branch with messy commit history (RuboCop fixes, iterations, failed attempts) into clean, logical commits where each commit:
Step 1: Gather branch information
# Get current branch name
git rev-parse --abbrev-ref HEAD
# Get main/base branch
git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'
# Count commits
git log main..HEAD --oneline | wc -l
# Get commit list
git log main..HEAD --oneline
# Get detailed commit info
git log main..HEAD --format="%H|%s|%an|%ad" --date=iso
Step 2: Analyze each commit
For each commit, examine:
git show --stat <commit-hash>
Identify:
Step 3: Identify patterns
Common patterns to look for:
Step 1: Create logical groupings
Group commits into shippable units. Ask yourself:
Example grouping logic:
Original commits:
1. Add feature X
2. Fix RuboCop in feature X
3. Add tests for feature X
4. Fix test typo
Grouped:
1. Add feature X (includes all 4 commits)
Step 2: Write comprehensive commit messages
For each grouped commit, write a message that includes:
Follow the project's commit message conventions (check recent commits with git log --oneline -5).
Step 3: Present plan to user
Show the user:
## Current State
- 16 commits with multiple iterations and fixes
## Proposed Reorganization
- 5 logical commits, each independently shippable
### Commit 1: [Subject]
**Combines:** [list of original commits]
**Why together:** [rationale]
**Summary:** [what this commit achieves]
**Status:** Shippable - [why app works at this point]
[Repeat for each commit]
## Summary
- Reduces from X to Y commits
- Eliminates: RuboCop fix commits, test iterations, failed attempts
- Each commit is shippable and tells clear story
Step 4: Ask for confirmation
Use AskUserQuestion tool:
Step 1: Safety measures
# Get repo and branch info
repo_name=$(basename "$(git rev-parse --show-toplevel)")
branch_name=$(git rev-parse --abbrev-ref HEAD)
# Create backup branch
git branch ${branch_name}-backup
# Confirm backup created
echo "✓ Backup branch created: ${branch_name}-backup"
Step 2: Create rebase script
Generate a script with pick/squash commands:
cat > /tmp/rebase-script-$$.sh << 'EOF'
#!/bin/bash
cat > "$1" << 'REBASE_EOF'
pick abc1234 First commit to keep
squash def5678 Second commit to squash into first
squash ghi9012 Third commit to squash into first
pick jkl3456 Second commit to keep
squash mno7890 Commit to squash into second
REBASE_EOF
EOF
chmod +x /tmp/rebase-script-$$.sh
Step 3: Create commit message files
For each final commit, create a message file:
cat > /tmp/commit-msg-1-$$.txt << 'EOF'
Subject line of first commit
Detailed body explaining what changed and why.
- Bullet points of key changes
- Implementation details
- Rationale for decisions
TICKET-123
EOF
Step 4: Create commit message editor
cat > /tmp/commit-msg-editor-$$.sh << 'EOF'
#!/bin/bash
COMMIT_MSG_FILE="$1"
CURRENT_MSG=$(head -1 "$COMMIT_MSG_FILE")
# Match commit and apply appropriate message
if echo "$CURRENT_MSG" | grep -q "pattern1"; then
cat /tmp/commit-msg-1-$$.txt > "$COMMIT_MSG_FILE"
elif echo "$CURRENT_MSG" | grep -q "pattern2"; then
cat /tmp/commit-msg-2-$$.txt > "$COMMIT_MSG_FILE"
fi
EOF
chmod +x /tmp/commit-msg-editor-$$.sh
Step 5: Execute rebase
# Get base branch (usually main)
base_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
# Execute interactive rebase
GIT_SEQUENCE_EDITOR=/tmp/rebase-script-$$.sh \
GIT_EDITOR=/tmp/commit-msg-editor-$$.sh \
git rebase -i $base_branch
Step 6: Handle conflicts
If rebase fails:
git statusgit rebase --abortgit reset --hard ${branch_name}-backupStep 1: Compare diffs
# Get diff of current branch
git diff main...HEAD > /tmp/local-diff-$$.txt
# Get diff of backup branch
git diff main...${branch_name}-backup > /tmp/backup-diff-$$.txt
# Compare (should be identical except for git metadata)
diff /tmp/local-diff-$$.txt /tmp/backup-diff-$$.txt
If diffs match (except for index lines), verification passes ✓
Step 2: Show final history
# Show new commit history
git log main..HEAD --oneline
# Show full commit messages
git log main..HEAD --format="========== %h ==========%n%B%n"
Step 1: Create output directory
# Get repo and branch info
repo_name=$(basename "$(git rev-parse --show-toplevel)")
branch_name=$(git rev-parse --abbrev-ref HEAD)
# Create directory
mkdir -p ~/.claude/repos/${repo_name}/${branch_name}/git-rewrite
Step 2: Save all artifacts
Save to ~/.claude/repos/{repo}/{branch}/git-rewrite/:
git-history-reorganization-plan.md - Detailed plan with rationalereorganization-summary.md - Summary with before/after, next stepsrebase-script.sh - The rebase sequence scriptcommit-msg-N.txt - Each commit message (N = 1, 2, 3...)commit-msg-editor.sh - The automated message editorlocal-branch-diff.txt - Diff of reorganized branchbackup-branch-diff.txt - Diff of original branchStep 3: Create summary document
Generate reorganization-summary.md with:
Step 1: Report success
## ✓ Git History Successfully Reorganized!
**Before:** 16 commits with iterations and fixes
**After:** 5 clean, logical commits
**Backup created:** {branch-name}-backup
**Verification:** ✓ Code changes are identical (diffs match)
**Documentation saved to:**
~/.claude/repos/{repo}/{branch}/git-rewrite/
**Next steps:**
1. Review the new history: `git log main..HEAD`
2. When ready to push: `git push --force-with-lease origin {branch}`
**If you need to restore:**
`git reset --hard {branch-name}-backup`
Look for and handle:
~/.claude/repos/{repo}/{branch}/git-rewrite/User might say:
If rebase fails:
git rebase --abortgit reset --hard {branch}-backupIf conflicts occur:
If verification fails:
A successful rewrite means: