name: mp-sync-base description: 'Merge target branch into current branch. Use when: "sync with main", "merge dev into branch", "update from main"' allowed-tools: Bash(git *), Agent, Read, Edit, Bash(gh *) metadata: author: MartinoPolo version: "1.1" category: git-workflow
Sync Base Branch
Merge a target branch into the current branch. $ARGUMENTS
Args: [branch]
Workflow
Step 1: Determine Target Branch
If $ARGUMENTS provides a branch → use it.
Otherwise, spawn mp-base-branch-detector sub-agent:
- Explicit base branch: none
- Remote branches: output of
git branch -r
Based on result:
- Branch returned → use it, display to user
- Null with candidates → ask user to pick from candidates
- Null without candidates → ask user to specify manually
Step 2: Pre-merge Checks
2a. Uncommitted changes:
git status --porcelain
If non-empty → ask user: "Uncommitted changes detected. Stash before merging?"
- "Stash and continue" →
git stash push -m "Auto-stash before merge" - "Abort"
2b. Remote sync (current branch):
git branch --show-current
git rev-parse --verify origin/<current> 2>/dev/null
If no tracking branch → skip to Step 3.
Otherwise:
git fetch origin <current>
git rev-list --left-right --count HEAD...origin/<current>
- Behind only → ask user: "Current branch is N behind remote. Pull first?"
- "Pull remote changes (Recommended)" →
git pull origin <current> - "Continue anyway"
- "Pull remote changes (Recommended)" →
- Ahead only → inform user, continue
- Diverged → ask user: "Branch diverged (N ahead, M behind). Pull first?"
- "Pull (Recommended)" →
git pull origin <current> - "Continue anyway"
- "Pull (Recommended)" →
- In sync → continue
Step 3: Fetch and Preview
git fetch origin <target>
git log HEAD..origin/<target> --oneline
Display incoming commits. If none → report "Already up-to-date" and stop.
Step 4: Merge
git merge origin/<target>
Step 5: Resolve Conflicts (if any)
If conflicts occur:
- List conflicted files:
git diff --name-only --diff-filter=U - For each conflicted file:
a. Read the file (use Read tool)
b. Analyze conflict markers (
<<<<<<<,=======,>>>>>>>) c. Simple conflicts (non-overlapping, clear intent) → resolve with Edit tool, thengit add <file>d. Complex conflicts (overlapping logic, ambiguous) → show both sides to user, ask how to resolve - After all resolved:
git commit(accept default merge message) - If new conflicts appear → repeat from step 1
Step 6: Push
If origin/<current> exists (determined in Step 2b) → git push origin <current>
If no remote tracking branch → skip push, inform user.
Output
After completion, display:
- Target branch merged
- Number of incoming commits applied
- Conflicts resolved (if any), with brief description
- Session Activity: list agents dispatched (if any)