This commit is contained in:
Miguel Palhas
2026-02-07 18:34:44 +00:00
parent 69007bcf02
commit 685f414839
5 changed files with 333 additions and 19 deletions
+90 -13
View File
@@ -1,23 +1,100 @@
---
description: Merge a worktree branch back into the base branch
description: Merge worktree changes and clean up safely
---
Merge a branch from the worktree.
If you're currently working on a worktree, assume that's the one I'm referring to.
Start by committing any staged and unstaged changes. I may have made changes to the worktree manually.
Then merge back the new main branch, since I may have made changes to it.
# Merge Worktree and Cleanup
# Language specific:
Merge changes from the current worktree into a target branch and clean up the worktree.
## Rust
**Usage:** `/merge [target-branch]`
Ensure cargo check is happy.
Ensure clippy is happy. clean up any new warnings.
**Arguments:**
- `$1`: Target branch to merge into (default: auto-detect main/master)
## Typescript
## Process
Ensure tsc is happy.
### 1. Pre-flight Checks
# Finally
```bash
CURRENT_BRANCH=$(git branch --show-current)
WORKTREE_DIR=$(git rev-parse --git-common-dir)
Squash the commits into a single one, merge into the base branch, and delete the worktree.
if [[ ! "$WORKTREE_DIR" == *".git/worktrees"* ]]; then
echo "❌ Error: You're not in a worktree"
exit 1
fi
if ! git diff-index --quiet HEAD --; then
echo "❌ Error: You have uncommitted changes"
git status --short
exit 1
fi
```
### 2. Determine Target Branch
```bash
TARGET_BRANCH="${1:-}"
if [ -z "$TARGET_BRANCH" ]; then
if git show-ref --verify --quiet refs/heads/main; then
TARGET_BRANCH="main"
elif git show-ref --verify --quiet refs/heads/master; then
TARGET_BRANCH="master"
else
echo "❌ Error: Cannot auto-detect main branch"
exit 1
fi
fi
```
### 3. Update Target Branch
```bash
WORKTREE_PATH=$(pwd)
COMMON_DIR=$(git rev-parse --git-common-dir)
REPO_ROOT=$(echo "$COMMON_DIR" | sed 's/\.git.*//' | sed 's/\/$//')
if [ "$COMMON_DIR" = ".git" ]; then
REPO_ROOT=$(git rev-parse --show-toplevel)
fi
cd "$REPO_ROOT"
git checkout "$TARGET_BRANCH"
git pull origin "$TARGET_BRANCH"
```
### 4. Merge
```bash
git merge --no-ff "$CURRENT_BRANCH" -m "Merge branch '$CURRENT_BRANCH'"
```
If you prefer a squash merge, do this instead:
```bash
git merge --squash "$CURRENT_BRANCH"
git commit -m "feat: <summary>"
```
### 5. Push and Clean Up
```bash
git push origin "$TARGET_BRANCH"
git worktree remove "$WORKTREE_PATH" --force || rm -rf "$WORKTREE_PATH"
git branch -d "$CURRENT_BRANCH" || git branch -D "$CURRENT_BRANCH"
```
### 6. Optional: Delete Remote Branch
```bash
git push origin --delete "$CURRENT_BRANCH"
```
## Language-specific checks
### Rust
- `cargo check`
- `cargo clippy` (clean up new warnings)
### TypeScript
- `tsc`