# Git Merge Branch

> Merge current branch to a target branch. Use when the user wants to merge their current branch to another branch (e.g., qa, master, develop).

- Skill: `zhaoyis/git-merge-branch` (Agent Skill)
- Install (CLI): `npx skillmds@latest add zhaoyis/git-merge-branch`
- Raw SKILL.md: https://api.skillmd.com/api/skills/zhaoyis/git-merge-branch/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: MIT
- Author: ZhaoYis (https://skillmd.com/u/zhaoyis)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/zhaoyis/git-merge-branch

---


Merge current branch to a target branch.

**Input**: The skill will prompt for the target branch name before executing.

**Steps**

1. **Pre-merge checks**

   Run `git status` to check for:
   - Uncommitted changes (staged or unstaged)
   - Untracked files

   **If there are uncommitted changes:**
   - Warn user and ask how to proceed:
     - Stash changes and continue
     - Commit changes first
     - Cancel merge

   Run `git branch --show-current` to get the current branch name.

   Run `git log origin/<current-branch>..HEAD --oneline` to check if local is ahead of remote.

   **If current branch not pushed to remote:**
   - Warn user: "Current branch has unpushed commits"
   - Ask if they want to push first or proceed anyway

2. **Get available branches**

   Run `git branch -a` to list all available branches (local and remote).

   Show current branch and uncommitted changes status.

3. **Prompt for target branch**

   Use **AskUserQuestion tool** to ask the user which branch they want to merge into.

   Show:
   - Current branch name
   - List of common target branches (filtered from local branches)
   - Allow custom input for other branch names

   Example prompt:
   ```
   当前分支: feature/xxx
   未提交变更: 0 个文件

   请选择目标合并分支:
   - master
   - qa
   - stg
   - 其他 (手动输入)
   ```

4. **Prompt for merge strategy**

   Use **AskUserQuestion tool** to ask for merge strategy:

   **Merge strategies:**
   - **Default merge**: `git merge <branch>` - Standard merge with merge commit
   - **Squash merge**: `git merge --squash <branch>` - Squash all commits into one
   - **No-ff merge**: `git merge --no-ff <branch>` - Always create merge commit

   Default: Standard merge

5. **Fetch and update target branch**

   Before merging, ensure target branch is up to date:
   ```bash
   git fetch origin <target-branch>
   git checkout <target-branch>
   git pull origin <target-branch>
   ```

6. **Perform merge**

   Execute merge with selected strategy:
   ```bash
   git merge <source-branch> -m "Merge branch '<source-branch>' into <target-branch>"
   ```

   Or with squash:
   ```bash
   git merge --squash <source-branch>
   git commit -m "Merge branch '<source-branch>' into <target-branch> (squashed)"
   ```

   **If merge conflicts occur:**
   - Show conflict files: `git diff --name-only --diff-filter=U`
   - Show conflict summary
   - Ask user how to resolve:
     1. Abort merge
     2. Manual resolve (wait for user to resolve)
     3. Use theirs/ours for specific files

   **If abort:**
   ```bash
   git merge --abort
   ```
   Exit and inform user.

7. **Push to remote**

   After successful merge:
   ```bash
   git push origin <target-branch>
   ```

8. **Switch back to source branch**

   **IMPORTANT**: Always switch back to the source branch after merge by default.

   Return to the original branch:
   ```bash
   git checkout <source-branch>
   ```

9. **Post-merge options (optional)**

   After switching back, ask user if they want to delete the source branch:

   **Options (multi-select):**
   - **Keep branch (default)**: No action needed, continue development on source branch
   - **Delete local branch**: `git branch -d <source-branch>` (only if user explicitly selects)
   - **Delete remote branch**: `git push origin --delete <source-branch>` (only if user explicitly selects)

   **Default behavior**: Keep the source branch and continue working on it.

   **If user chooses to delete:**
   - Execute deletion commands
   - Stay on target branch (since source branch no longer exists)

10. **Display summary**

    Show:
    - Source branch
    - Target branch
    - Merge strategy used
    - Merge commit hash
    - Files changed count
    - Push status
    - Branch deletion status (if applicable)

**Output On Success**

```
## Merge Complete

**Source:** <source-branch>
**Target:** <target-branch>
**Strategy:** Standard / Squash / No-ff
**Commit:** <merge-commit-hash>

<source-branch> ──► <target-branch>

**Changes merged:** <count> files (+<additions>, -<deletions>)

**Post-merge:**
- Source branch kept: Yes (default) / No (deleted)
- Switched back to: <source-branch>

**Remote:** <remote-url>
```

**Output On Conflict**

```
## Merge Conflict

**Source:** <source-branch>
**Target:** <target-branch>

**Conflicted files (<count>):**
- <file1>
- <file2>

**Options:**
1. Abort merge
2. Manual resolve (I'll wait for you to resolve)
3. Use "theirs" for all conflicts
4. Use "ours" for all conflicts

What would you like to do?
```

**Merge Strategy Guide**

| Strategy | Use Case | Pros | Cons |
|----------|----------|------|------|
| Standard | Feature branches | Preserves history | Multiple merge commits |
| Squash | Small features, WIP | Clean history | Loses individual commits |
| No-ff | Release branches | Clear merge point | Extra merge commit |

**Guardrails**

- Always check for uncommitted changes before merging
- Always check if source branch is pushed to remote
- Always prompt for target branch name before merging
- Always prompt for merge strategy
- Always fetch and pull target branch before merging
- **Always switch back to source branch after merge (default behavior)**
- **Keep source branch by default, only delete if user explicitly requests**
- Never force push without explicit user request
- Show clear summary of what happened
- Handle merge conflicts gracefully

**Common Target Branches**

When prompting, show these common branches if they exist:
- `master` / `main`
- `qa`
- `stg`
- `develop`

**Error Handling**

- If uncommitted changes: Ask user how to proceed (stash/commit/cancel)
- If source branch not pushed: Warn and ask to proceed or push first
- If target branch doesn't exist: Show error and suggest available branches
- If merge fails with conflicts: Show conflicted files and prompt for resolution
- If push fails: Show error message and suggest resolution

