home / skills / cacr92 / wereply / merge-main-cleanup

merge-main-cleanup skill

/.trae/skills/merge-main-cleanup

This skill performs a full merge into the main branch, then removes all non-base branches locally and remotely.

npx playbooks add skill cacr92/wereply --skill merge-main-cleanup

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.1 KB
---
name: merge-main-cleanup
description: This skill should be used when the user requests merging work into main and then deleting all other local and remote branches, keeping only main.
---

# Merge Main Cleanup

## Overview
Perform a full merge into main and then delete all other branches locally and on the default remote. Remove any worktrees tied to deleted branches.

## When to Use
- User says to merge into main and keep only main.
- User wants a one-step cleanup of branches after merge.

## Workflow

### 1) Validate repository state
- Run `git status --porcelain` and stop if there are uncommitted changes.
- Run `git rev-parse --show-toplevel` and work from that root.
- Run `git fetch --all --prune` to sync branch state.

### 2) Determine base branch
- Prefer `main`; if missing, use `master`.
- Command: `git branch --list main master`

### 3) Merge current branch into base
- Get current branch: `git branch --show-current`.
- If already on base, skip merge.
- Otherwise:
  - `git checkout <base>`
  - `git pull --ff-only`
  - `git merge <current-branch>`
  - Resolve conflicts if any, then complete the merge.

### 4) Push base to remote
- Push base: `git push origin <base>`

### 5) Remove worktrees for non-base branches
- List worktrees: `git worktree list`.
- For each worktree whose branch is not `<base>`, remove it:
  - `git worktree remove <path>`

### 6) Delete all non-base branches (local + remote)
- Local branches (force delete, as requested):
  - List: `git branch --format='%(refname:short)'`
  - For each branch not `<base>`, run: `git branch -D <branch>`
- Remote branches (origin):
  - List: `git branch -r --format='%(refname:short)' | rg '^origin/'`
  - For each remote branch not `origin/<base>`, run: `git push origin --delete <branch>`

### 7) Verify only base remains
- `git branch -vv` should show only `<base>`.
- `git branch -r` should show only `origin/<base>`.

## Notes
- This skill is intentionally destructive: it deletes all branches except base.
- If the remote is not named `origin`, replace it accordingly.
- If deletion of worktree files is blocked by policy, report and ask the user to remove them manually.

Overview

This skill performs a full merge of the current work into the repository base branch (prefer main, fallback master) and then deletes all other local and remote branches, leaving only the base branch. It also removes any git worktrees tied to deleted branches. Use this when you want a destructive one-step cleanup after finishing work and consolidating history into main.

How this skill works

The skill validates the repo is clean, discovers the base branch, and merges the current branch into that base. It pushes the updated base to the default remote, removes worktrees for branches that will be deleted, then force-deletes all non-base local branches and removes their remote counterparts. It finishes by verifying only the base branch remains locally and remotely.

When to use it

  • You want to merge your current branch into main (or master) and then remove every other branch.
  • You need to perform a one-step destructive cleanup after code consolidation.
  • You are certain all branch histories except the base can be deleted.
  • You want to remove all git worktrees tied to branches you plan to delete.
  • You prefer an automated sequence: validate, merge, push, remove worktrees, delete branches.

Best practices

  • Ensure working directory has no uncommitted changes before running this skill.
  • Create a backup or tag the base branch before destructive deletion if you might need recovery.
  • Confirm the remote name (origin assumed) and replace it if your remote is named differently.
  • Resolve merge conflicts manually when they occur; the skill will pause for conflict resolution.
  • Notify collaborators before deleting remote branches to avoid data loss or interrupted workflows.

Example use cases

  • Finish a feature branch, merge into main, and remove all development branches to simplify the repo.
  • Clean up a repository after a rebase/cleanup sprint where only main should remain.
  • Prepare a repository for archiving by consolidating changes into main and deleting other branches.
  • Remove stale branches and associated worktrees after a cleanup review.

FAQ

What if I have uncommitted changes?

The skill stops if there are uncommitted changes. Commit or stash them before proceeding.

Does it support remotes not named origin?

Yes. The commands assume origin by default; replace the remote name with your remote as needed.