---
description: Phase 13 - Sync local branch with remote (push/pull)
agent: gitflow-status
model: haiku
---

# Phase 13: SYNC - Synchronize with Remote

Synchronise la branche locale avec sa branche remote tracking.

---

## STEP 1: Fetch et analyse

```bash
# Fetch pour avoir les infos a jour
git fetch --quiet 2>/dev/null

BRANCH=$(git branch --show-current)
UPSTREAM=$(git rev-parse --abbrev-ref @{upstream} 2>/dev/null)

echo "Branche locale: $BRANCH"
echo "Remote tracking: $UPSTREAM"
```

---

## STEP 2: Determiner l'action necessaire

```bash
if [ -z "$UPSTREAM" ]; then
  # Pas de remote tracking - proposer push initial
  ACTION="PUSH_NEW"
  echo "Status: NO REMOTE - branche non pushee"
else
  LOCAL=$(git rev-parse @)
  REMOTE=$(git rev-parse @{upstream})
  BASE=$(git merge-base @ @{upstream})

  if [ "$LOCAL" = "$REMOTE" ]; then
    ACTION="UP_TO_DATE"
    echo "Status: UP-TO-DATE - rien a faire"
  elif [ "$LOCAL" = "$BASE" ]; then
    BEHIND=$(git rev-list --count @..@{upstream})
    ACTION="PULL"
    echo "Status: BEHIND $BEHIND commits - pull needed"
  elif [ "$REMOTE" = "$BASE" ]; then
    AHEAD=$(git rev-list --count @{upstream}..@)
    ACTION="PUSH"
    echo "Status: AHEAD $AHEAD commits - push needed"
  else
    AHEAD=$(git rev-list --count @{upstream}..@)
    BEHIND=$(git rev-list --count @..@{upstream})
    ACTION="DIVERGED"
    echo "Status: DIVERGED +$AHEAD/-$BEHIND - pull --rebase needed"
  fi
fi
```

---

## STEP 3: Executer l'action

### Si UP_TO_DATE

```
================================================================================
                           ALREADY SYNCHRONIZED
================================================================================

Branche: {branch}
Remote:  {upstream}
Status:  UP-TO-DATE

Rien a faire.

================================================================================
```

### Si PUSH_NEW (pas de remote)

```javascript
AskUserQuestion({
  questions: [{
    question: "Push this branch to remote for the first time?",
    header: "Push new",
    options: [
      { label: "Yes, push", description: "git push -u origin {branch}" },
      { label: "No, cancel", description: "Keep local only" }
    ],
    multiSelect: false
  }]
})
```

**Si Yes:**
```bash
git push -u origin "$BRANCH"
echo "Branche pushee et tracking configure"
```

### Si PULL

```bash
echo "Pulling $BEHIND commits from remote..."
git pull --ff-only

if [ $? -ne 0 ]; then
  echo "WARNING: Fast-forward not possible, trying rebase..."
  git pull --rebase
fi
```

### Si PUSH

```bash
echo "Pushing $AHEAD commits to remote..."
git push
```

### Si DIVERGED

```javascript
AskUserQuestion({
  questions: [{
    question: "Branch has diverged from remote. How to resolve?",
    header: "Diverged",
    options: [
      { label: "Pull --rebase (recommended)", description: "Rebase local commits on top of remote" },
      { label: "Force push", description: "Overwrite remote with local (DANGEROUS)" },
      { label: "Cancel", description: "Review manually first" }
    ],
    multiSelect: false
  }]
})
```

**Si Pull --rebase:**
```bash
git pull --rebase
if [ $? -ne 0 ]; then
  echo "CONFLITS DETECTES - resolvez puis: git rebase --continue"
  exit 1
fi
git push
```

**Si Force push:**
```bash
# Double confirmation pour force push
echo "WARNING: Force push va ecraser les commits distants!"
```

```javascript
AskUserQuestion({
  questions: [{
    question: "CONFIRM: Force push will overwrite remote commits. Are you sure?",
    header: "Force push",
    options: [
      { label: "Yes, force push", description: "I understand the risk" },
      { label: "No, cancel", description: "Abort operation" }
    ],
    multiSelect: false
  }]
})
```

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

---

## STEP 4: Verification

```bash
echo ""
echo "Verification post-sync..."

LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse @{upstream} 2>/dev/null)

if [ "$LOCAL" = "$REMOTE" ]; then
  echo "SYNCHRONIZED"
else
  echo "WARNING: Still not synchronized"
  git status
fi
```

---

## Summary

```
================================================================================
                           SYNC COMPLETE
================================================================================

Branche: {branch}
Remote:  origin/{branch}
Action:  {PULL|PUSH|PUSH_NEW|REBASE}
Status:  SYNCHRONIZED

================================================================================
```

---

## Codes sortie

| Code | Signification |
|------|---------------|
| 0 | Sync reussie |
| 1 | Conflits (rebase en cours) |
| 2 | Erreur reseau ou permission |
