---
name: abort
description: Rollback and recovery operations
---

# Phase: ABORT - Rollback & Recovery

## YOUR TASK:

Provide rollback and recovery options for GitFlow operations.

---

## PRE-CHECK:

```bash
# Load GitFlow config (sets GF_* variables)
read_gitflow_config || { echo "❌ Run /gitflow init first."; exit 1; }
```

## OPTIONS:

```yaml
AskUserQuestion:
  header: "Recovery"
  question: "What do you want to abort/rollback?"
  options:
    - label: "Abort current merge/rebase"
      description: "Cancel in-progress git operation"
    - label: "Abandon current branch"
      description: "Delete branch and worktree, return to develop"
    - label: "Restore from checkpoint"
      description: "Restore to a saved checkpoint (not yet implemented)"
    - label: "Reset branch to remote"
      description: "Discard local changes, match remote"
```

---

## EXECUTION:

### Option 1: Abort Merge/Rebase

```bash
# Check for in-progress operations
if [ -d ".git/rebase-merge" ] || [ -d ".git/rebase-apply" ]; then
  echo "Aborting rebase..."
  git rebase --abort
elif [ -f ".git/MERGE_HEAD" ]; then
  echo "Aborting merge..."
  git merge --abort
elif [ -f ".git/CHERRY_PICK_HEAD" ]; then
  echo "Aborting cherry-pick..."
  git cherry-pick --abort
else
  echo "No operation in progress"
fi
```

### Option 2: Abandon Branch

```bash
CURRENT=$(git rev-parse --abbrev-ref HEAD)
BRANCH_TYPE=$(echo $CURRENT | cut -d'/' -f1)

# Confirm
echo "⚠️ This will delete branch $CURRENT and its worktree"
echo "All uncommitted changes will be LOST"

# Repair cross-platform worktree paths (WSL ↔ Windows)
detect_platform
repair_worktree_paths "$(git rev-parse --git-common-dir 2>/dev/null || echo '.bare')"

# Find worktree
WORKTREE=$(git worktree list | grep "$CURRENT" | awk '{print $1}')

# Create backup tag
BACKUP_TAG="backup/$CURRENT-$(date +%Y%m%d-%H%M%S)"
git tag "$BACKUP_TAG" HEAD
echo "✓ Backup created: $BACKUP_TAG"

# Switch to develop
git checkout $GF_DEVELOP_BRANCH

# Remove worktree
[ -n "$WORKTREE" ] && {
  git worktree remove "$WORKTREE" --force
  git worktree prune
}

# Delete branch
git branch -D "$CURRENT"
git push origin --delete "$CURRENT" 2>/dev/null

echo "✓ Branch $CURRENT abandoned"
echo "  Backup: $BACKUP_TAG (use 'git checkout $BACKUP_TAG' to recover)"
```

### Option 3: Restore from Checkpoint

> **Note:** Checkpoint creation is not yet implemented. This option uses backup tags
> created by abort operations as a recovery mechanism. Full checkpoint support
> (automatic state snapshots during plan execution) is planned for a future release.

```bash
# List available backup tags (created by abandon operations)
BACKUPS=$(git tag -l "backup/*" --sort=-creatordate 2>/dev/null)

if [ -z "$BACKUPS" ]; then
  echo "No backup tags found"
  echo "  Backup tags are created automatically when abandoning branches."
  echo "  Use 'Reset branch to remote' instead."
  exit 1
fi

echo "Available backups:"
for tag in $BACKUPS; do
  COMMIT=$(git rev-parse --short "$tag")
  DATE=$(git log -1 --format=%ci "$tag" 2>/dev/null)
  echo "  - $tag → $COMMIT ($DATE)"
done

# Select backup
# ... user selection ...

# Restore
git checkout "$SELECTED_TAG"
git checkout -b "recovered-$(date +%Y%m%d-%H%M%S)"
echo "✓ Restored from $SELECTED_TAG to new branch"
```

### Option 4: Reset to Remote

```bash
CURRENT=$(git rev-parse --abbrev-ref HEAD)

# Check for local changes
CHANGES=$(git status --porcelain | wc -l)
[ "$CHANGES" -gt 0 ] && {
  echo "⚠️ $CHANGES uncommitted changes will be LOST"
  # Confirm
}

# Stash as backup
git stash push -m "abort-backup-$(date +%s)" --include-untracked

# Reset to remote
git fetch origin
git reset --hard "origin/$CURRENT"

echo "✓ Reset to origin/$CURRENT"
echo "  Backup in stash (use 'git stash list' to see)"
```

---

## SAFETY FEATURES:

1. **Backup tag** created before destructive operations
2. **Stash backup** before reset
3. **Checkpoint restore** for plan execution
4. **Confirmation required** for destructive actions

---

## RECOVERY COMMANDS:

```bash
# List backup tags
git tag | grep "^backup/"

# Recover from backup
git checkout backup/{branch-name}
git checkout -b {new-branch-name}

# List stashes
git stash list

# Recover from stash
git stash pop
```

---

## SUCCESS CRITERIA:

- Operation aborted cleanly
- Backup created before destructive action
- User returned to safe state (develop/main)
