---
name: git-rebase-workflow-7f018f663bfb
description: >
  Guide Claude through safe git rebase workflows: interactive rebase, conflict resolution,
  squash strategies, and force-push safety. Use when the user asks "rebase my branch",
  "clean up commits", "squash commits before PR", "rebase onto main", "resolve rebase conflicts",
  or wants help with git history cleanup. 安全的 Git Rebase 工作流指南：交互式变基、冲突解决、
  提交压缩策略和安全强制推送。
allowed-tools: Bash Read Grep
---

# Git Rebase Workflow

Guide the user through safe, team-friendly git rebase operations — from syncing with main to interactive commit cleanup and safe force-pushing.

## When to Use

- User wants to rebase a feature branch onto main/develop
- User needs to squash or clean up commits before a PR
- User sees "branch has diverged" and needs guidance
- User asks about interactive rebase (`rebase -i`)
- User wants to set up rebase aliases or hooks

## When NOT to Use

- User is on `main` or a shared branch — warn them and suggest merge instead
- User wants to rewrite history that has already been merged — this is destructive
- User is resolving a complex merge and wants to preserve conflict resolution history

## Prerequisites

- Git installed (>= 2.0)
- Working in a git repository with a remote

## Step-by-Step Workflow

### 1. Assess the Situation

```bash
git status
git log --oneline -10
git rev-parse --abbrev-ref HEAD
```

Confirm the user is on a feature branch, not main. If on main, STOP and warn.

### 2. Sync with Upstream

```bash
git fetch origin
git rebase origin/main
```

If conflicts occur:
1. Show the conflicted files: `git diff --name-only --diff-filter=U`
2. Help resolve each conflict
3. Stage resolved files: `git add <file>`
4. Continue: `git rebase --continue`
5. If unrecoverable: `git rebase --abort`

### 3. Interactive Rebase (Commit Cleanup)

```bash
git rebase -i origin/main
```

Apply these strategies:
- **squash** WIP/typo/fix commits into their parent logical commit
- **reword** vague messages like "updates" into descriptive ones
- **drop** accidental commits (debug logs, temp files)
- Keep logical boundaries: separate refactors from features from tests

Ordering: refactors first → features → tests → docs.

### 4. Force Push Safely

```bash
git push --force-with-lease
```

**Never use `--force`** — it can overwrite teammates' work. `--force-with-lease` fails safely if the remote has new commits you haven't seen.

If the push is rejected, fetch and rebase again before retrying.

### 5. Verify

```bash
git log --oneline origin/main..HEAD
```

Confirm:
- Commits are in logical order
- No WIP/fixup commits remain
- Commit messages follow the team's convention

## Input/Output Spec

- **Input**: A git repository with a feature branch that needs rebasing or cleanup
- **Output**: Clean, rebased branch with logical commits ready for PR review

## Edge Cases & Error Handling

| Scenario | Action |
|---|---|
| On main branch | STOP — warn user, never rebase main |
| Rebase conflicts in every commit | Consider `git rebase --abort` and doing a merge instead |
| "diverged from origin" after rebase | Expected — verify with `git log`, then `--force-with-lease` |
| Coworker pushed to same branch | Fetch first, discuss with coworker before force pushing |
| Pre-push hook blocks push | Check if `--force-with-lease` is being blocked by policy |

## Examples

### Example 1: Clean up before PR

User: "I have 8 messy commits on my feature branch, help me clean up before opening a PR"

```bash
git fetch origin
git rebase -i origin/main
# In editor: squash WIP commits, reword vague ones
# Result: 3 clean commits — "add API endpoint", "add tests", "update docs"
git push --force-with-lease
```

### Example 2: Sync stale branch

User: "My branch is 47 commits behind main"

```bash
git fetch origin
git rebase origin/main
# Resolve any conflicts one by one
git push --force-with-lease
# Branch is now up-to-date with clean linear history
```

### Example 3: Recover from bad rebase

User: "I messed up a rebase and my branch looks wrong"

```bash
git reflog  # find the commit before rebase started
git reset --hard HEAD@{N}  # reset to pre-rebase state
# Now try again more carefully, or use merge instead
```


---
Source attribution: skillforgeai-dev
Original project: https://github.com/skillforgeai-dev/content-to-skill/blob/a5dd693952b6e74a48b3d5af37296007c63577c9/examples/git-rebase-workflow/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 content-to-skill contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: skillforgeai-dev
Original project: https://github.com/skillforgeai-dev/content-to-skill/blob/a5dd693952b6e74a48b3d5af37296007c63577c9/examples/git-rebase-workflow/SKILL.md
Attribution basis: Source repository record

Fields of work: Content & Media, Development & Engineering
Roles: Backend Developer, DevOps / SRE, Frontend Developer, Full-Stack Developer, Technical Writer
Specialties: Cross Platform, Git
Tools: Bash
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 72829658a456505f4b20318efb2d4aff9329ce3fc4e7252cc4dd4b6ba9ac8cab
