---
description: Phase 14 - Rebase current branch on develop (or specified base)
agent: gitflow-status
model: haiku
argument-hint: "[base-branch]"
---

# Phase 14: REBASE - Rebase on Base Branch

Rebase la branche courante sur develop (ou une autre branche specifiee).

**Usage:**
- `/gitflow:14-rebase` - Rebase sur develop (default)
- `/gitflow:14-rebase main` - Rebase sur main
- `/gitflow:14-rebase feature/other` - Rebase sur une autre feature

---

## STEP 1: Verification pre-rebase

```bash
BRANCH=$(git branch --show-current)
BASE_BRANCH="${1:-develop}"

echo "Branche courante: $BRANCH"
echo "Branche cible:    $BASE_BRANCH"

# Verifier working directory clean
if [ -n "$(git status --porcelain)" ]; then
  echo "ERROR: Working directory not clean"
  git status --short
  exit 1
fi

# Verifier que la branche cible existe
if ! git rev-parse --verify "$BASE_BRANCH" >/dev/null 2>&1; then
  echo "ERROR: Branch '$BASE_BRANCH' does not exist"
  exit 1
fi
```

---

## STEP 2: Fetch et analyse divergence

```bash
git fetch --quiet 2>/dev/null

# Calculer divergence
AHEAD=$(git rev-list --count "$BASE_BRANCH".."$BRANCH")
BEHIND=$(git rev-list --count "$BRANCH".."$BASE_BRANCH")

echo ""
echo "Divergence vs $BASE_BRANCH:"
echo "  Commits ahead:  $AHEAD"
echo "  Commits behind: $BEHIND"

if [ "$BEHIND" -eq 0 ]; then
  echo ""
  echo "Already up-to-date with $BASE_BRANCH"
fi
```

---

## STEP 3: Confirmation

```javascript
AskUserQuestion({
  questions: [{
    question: "Rebase {branch} on {base_branch}? ({ahead} commits will be replayed)",
    header: "Rebase",
    options: [
      { label: "Yes, rebase", description: "Replay commits on top of {base_branch}" },
      { label: "No, cancel", description: "Keep current state" }
    ],
    multiSelect: false
  }]
})
```

**Si No -> Stop**

---

## STEP 4: Check EF Core migrations (si applicable)

```bash
# Detecter si des migrations sont impliquees
MIGRATIONS_IN_BRANCH=$(git diff "$BASE_BRANCH"..."$BRANCH" --name-only | grep -E "Migrations/.*\.cs$" | grep -v Designer | wc -l)
SNAPSHOT_MODIFIED=$(git diff "$BASE_BRANCH"..."$BRANCH" --name-only | grep "ModelSnapshot.cs" | wc -l)

if [ "$MIGRATIONS_IN_BRANCH" -gt 0 ] || [ "$SNAPSHOT_MODIFIED" -gt 0 ]; then
  echo ""
  echo "WARNING: EF Core migrations detected in this branch!"
  echo "  Migrations: $MIGRATIONS_IN_BRANCH"
  echo "  ModelSnapshot modified: $SNAPSHOT_MODIFIED"
fi
```

Si migrations detectees:

```javascript
AskUserQuestion({
  questions: [{
    question: "EF Core migrations detected. Rebase may cause ModelSnapshot conflicts. Continue?",
    header: "EF Core",
    options: [
      { label: "Continue rebase", description: "I'll handle migration conflicts manually" },
      { label: "Use /efcore:rebase-snapshot", description: "Specialized command for EF Core rebase" },
      { label: "Cancel", description: "Review migrations first" }
    ],
    multiSelect: false
  }]
})
```

**Si "Use /efcore:rebase-snapshot" -> Redirect:**
```
Utilisez la commande specialisee:

  /efcore:rebase-snapshot

Cette commande gere automatiquement:
- Regeneration du ModelSnapshot
- Resolution des conflits de migration
- Validation post-rebase
```

---

## STEP 5: Execute rebase

```bash
echo "Starting rebase..."

git rebase "$BASE_BRANCH"

if [ $? -ne 0 ]; then
  echo ""
  echo "================================================================================
                         REBASE CONFLICTS DETECTED
================================================================================"
  echo ""
  echo "Des conflits ont ete detectes. Pour les resoudre:"
  echo ""
  echo "  1. Editez les fichiers en conflit"
  echo "  2. git add <fichiers resolus>"
  echo "  3. git rebase --continue"
  echo ""
  echo "Pour annuler le rebase:"
  echo "  git rebase --abort"
  echo "  ou /gitflow:6-abort"
  echo ""
  exit 1
fi
```

---

## STEP 6: Push avec force-with-lease

```bash
UPSTREAM=$(git rev-parse --abbrev-ref @{upstream} 2>/dev/null)

if [ -n "$UPSTREAM" ]; then
  echo ""
  echo "La branche a ete rebasee. Un force push est necessaire."
fi
```

```javascript
AskUserQuestion({
  questions: [{
    question: "Push rebased branch to remote? (requires force push)",
    header: "Push",
    options: [
      { label: "Yes, force push", description: "git push --force-with-lease (safe)" },
      { label: "No, later", description: "Push manually when ready" }
    ],
    multiSelect: false
  }]
})
```

**Si Yes:**
```bash
git push --force-with-lease

if [ $? -eq 0 ]; then
  echo "Force push successful"
else
  echo "ERROR: Force push failed (remote may have new commits)"
  echo "Retry with: git push --force-with-lease"
fi
```

---

## STEP 7: Summary

```
================================================================================
                           REBASE COMPLETE
================================================================================

Branche:     {branch}
Rebase sur:  {base_branch}
Commits:     {ahead} replayed
Push:        {done|pending}

DIVERGENCE APRES REBASE:
  vs {base_branch}: +{ahead}/-0

{Si EF Core:}
NEXT: Verifiez les migrations avec /efcore:db-status

================================================================================
Actions: /gitflow:2-status | /gitflow:13-sync | /gitflow:3-commit
================================================================================
```

---

## Codes sortie

| Code | Signification |
|------|---------------|
| 0 | Rebase reussi |
| 1 | Conflits (rebase en cours) |
| 2 | Working directory dirty |
| 3 | Branche cible inexistante |

---

## Recovery si conflits

Si le rebase echoue avec des conflits:

```bash
# Option 1: Resoudre manuellement
# Editez les fichiers, puis:
git add .
git rebase --continue

# Option 2: Annuler completement
git rebase --abort

# Option 3: Via Claude Code
/gitflow:6-abort
```
