merge-worktree
npx skills add https://github.com/yulonglin/dotfiles --skill merge-worktree
Agent 安装分布
Skill 文档
Merge Worktree
Merge the current worktree’s branch into the original (parent) branch. Resolves merge conflicts intelligently. Marks the worktree for cleanup after successful merge.
Instructions
1. Detect Context
Run these commands to understand the current state:
# Are we in a worktree?
git rev-parse --git-common-dir
git rev-parse --git-dir
git rev-parse --show-toplevel
# Current branch (should be worktree-<name>)
git rev-parse --abbrev-ref HEAD
# Find main worktree path
git worktree list --porcelain
Determine:
WORKTREE_NAME: extracted from current branch (stripworktree-prefix) or directory nameWORKTREE_BRANCH: current branch (e.g.,worktree-bold-fox-gjac)MAIN_TREE_PATH: path of the main worktree (first entry ingit worktree list)PARENT_BRANCH: branch checked out in the main worktree
If NOT in a worktree, tell the user and exit. This skill is designed to run from inside a worktree session.
2. Pre-merge Check
# Ensure all changes are committed in the worktree
git status --porcelain
If there are uncommitted changes, commit them first using the /commit skill or ask the user.
# Check how many commits to merge
git rev-list --count <PARENT_BRANCH>..<WORKTREE_BRANCH>
If 0 commits ahead, report “Already up to date” and exit.
3. Attempt Merge
Run the merge from the main tree:
git -C <MAIN_TREE_PATH> merge --no-edit <WORKTREE_BRANCH>
If merge succeeds: Report success with commit count, skip to step 5.
If merge fails (conflicts): Continue to step 4.
4. Resolve Conflicts
Do NOT abort the merge. Instead:
-
List conflicting files:
git -C <MAIN_TREE_PATH> diff --name-only --diff-filter=U -
For each conflicting file:
- Read the file (it has conflict markers
<<<<<<<,=======,>>>>>>>) - Read the worktree’s version:
git show <WORKTREE_BRANCH>:<file> - Read the parent branch’s version:
git show <PARENT_BRANCH>:<file> - Resolve the conflict by understanding both sides’ intent
- Write the resolved file
- Stage it:
git -C <MAIN_TREE_PATH> add <file>
- Read the file (it has conflict markers
-
After all conflicts resolved:
git -C <MAIN_TREE_PATH> commit --no-edit -
If you cannot confidently resolve a conflict, leave it and tell the user which files need manual attention.
5. Mark for Cleanup
After successful merge, tell the user:
Merged <N> commit(s) from <WORKTREE_BRANCH> into <PARENT_BRANCH>.
This worktree is now safe to remove:
cwrm --no-merge <WORKTREE_NAME>
Or continue working â run /merge-worktree again later to sync new commits.
Important
- Never force-push or rebase the parent branch
- Never delete the worktree branch â
cwrmhandles that - Prefer the worktree’s version when both sides changed the same thing and intent is unclear (the worktree has the newer work)
- If the main tree has uncommitted changes, warn the user and ask them to commit or stash first before merging