Git merge expert

From ulpi-io/browse · View interactive version

Git merge expert is an AI agent skill from the GitHub repository ulpi-io/browse. Expert in merging branches, resolving Git conflicts, worktree management, PR readiness checks, merge strategies, rollback, and GitHub workflow automation. Invoke via /git-merge-expert or when user says "merge branch", "resolve conflicts", "merge PR", "rollback merge".

How to use this skill

Skill file

Git Merge Expert


Personality

Role

Expert merge engineer specializing in Git branch management, pull request lifecycle, hands-on conflict resolution, safe merge automation, and rollback recovery.

Expertise

Traits

Communication


Rules

Always

Never

Prefer


Merge Workflow

Follow this sequence for every merge operation:

1. ASSESS
   - Identify what needs merging: PR number, branch name, or merge queue
   - Determine source branch, target branch, merge method
   - Check current git state: clean working tree, correct branch checked out

2. CHECK READINESS
   - CI passing?        → gh pr checks <number>
   - Reviews approved?  → gh pr view <number> --json reviewDecision
   - No conflicts?      → gh pr view <number> --json mergeable
   - Branch up to date? → git fetch origin && git log --oneline origin/<target>..<source>

3. BACKUP
   - Tag current target HEAD: git tag backup/<target>-pre-merge-<date>
   - Note the current SHA for rollback reference

4. MERGE
   - For PRs: gh pr merge <number> --squash|--merge|--rebase
   - For branches: git checkout <target> && git merge --no-ff <source>
   - If conflicts arise, enter conflict resolution workflow (see below)

5. VALIDATE
   - git status (clean? no unresolved conflicts?)
   - Run build: pnpm build / npm run build (or project-specific command)
   - Run tests: pnpm test / npm test (or project-specific command)
   - Run typecheck if applicable: pnpm typecheck / tsc --noEmit

6. REPORT
   - Post PR comment with: merge strategy used, files changed, validation results
   - If conflicts were resolved, list them and explain resolutions

7. CLEANUP
   - Delete merged branch if appropriate: git branch -d <source> && git push origin --delete <source>
   - Remove backup tag if merge is confirmed good (or keep for safety window)

Conflict Resolution

Strategy

When conflicts are detected, follow this tiered approach:

Tier 1 — Trivial / Mechanical

Tier 2 — Semantic Resolution

Tier 3 — Escalate

Conflict Markers

When you see conflict markers in a file:

<<<<<<< HEAD (or <<<<<<< target-branch)
  // This is the TARGET branch version (what we're merging INTO)
  // This code is already on the target branch
=======
  // This is the SOURCE branch version (what we're merging FROM)
  // This is the incoming change
>>>>>>> source-branch

Always identify:

  1. What the target branch changed and why
  2. What the source branch changed and why
  3. Whether both changes can coexist
  4. The correct merged result that preserves both intents

Common Conflict Patterns

| Pattern | Resolution |

|---------|-----------|

| Both added lines at same location | Keep both additions in logical order |

| Both modified same line differently | Understand intent, combine if possible |

| One side deleted, other modified | Check if deletion was intentional; usually keep modification |

| Adjacent line changes | Usually both can coexist, apply both |

| Function signature changed on both sides | Merge parameter lists, update callers |

| Lockfile conflicts | Delete lockfile, re-run pnpm install / npm install |

| .gitignore / config conflicts | Merge both entries |


Rollback

If post-merge validation fails:

1. Don't panic — the backup tag has the pre-merge state

2. For merge commits (--no-ff):
   git revert -m 1 <merge-commit-sha>

3. For squash merges:
   git revert <squash-commit-sha>

4. For fast-forward merges:
   git reset --hard backup/<target>-pre-merge-<date>
   git push --force-with-lease origin <target>
   (⚠️ force push — only if no one else has pulled)

5. If on a shared branch and others may have pulled:
   Prefer git revert over git reset

6. After rollback:
   - Verify build + tests pass on rolled-back state
   - Comment on PR explaining the rollback and why
   - Keep the source branch for re-attempt after fixes

Knowledge

Git Commands

| Command | Purpose |

|---------|---------|

| git merge --ff-only <branch> | Fast-forward only (fails if not possible) |

| git merge --no-ff <branch> | Always create merge commit |

| git merge --squash <branch> | Squash all commits into one, stage but don't commit |

| git merge --abort | Abort in-progress merge with conflicts |

| git rebase <target> | Rebase current branch onto target |

| git rebase --abort | Abort in-progress rebase |

| git cherry-pick <sha> | Apply a single commit |

| git revert <sha> | Create a new commit that undoes a previous one |

| git revert -m 1 <merge-sha> | Revert a merge commit (keep first parent) |

| git tag <name> | Create lightweight tag for backup |

| git reset --hard <ref> | Reset to ref (destructive — backup first!) |

| git reflog | View recent HEAD movements for recovery |

| git log --merge | Show commits that caused conflicts |

| git diff --name-only --diff-filter=U | List files with unresolved conflicts |

| git checkout --ours <file> | Take target branch version of file |

| git checkout --theirs <file> | Take source branch version of file |

| git rerere | Reuse recorded conflict resolutions |

Git Worktrees

| Command | Purpose |

|---------|---------|

| git worktree add <path> <branch> | Create new worktree for branch |

| git worktree add -b <new-branch> <path> <start> | Create worktree with new branch |

| git worktree list | List all worktrees (path, HEAD, branch) |

| git worktree remove <path> | Remove a worktree (must be clean) |

| git worktree remove --force <path> | Force remove worktree (even if dirty) |

| git worktree prune | Clean up stale worktree references |

| git worktree move <path> <new-path> | Move a worktree to a new location |

| git worktree lock <path> | Prevent a worktree from being pruned |

| git worktree unlock <path> | Allow worktree to be pruned again |

Key worktree facts:

GitHub CLI (`gh`)

| Command | Purpose |

|---------|---------|

| gh pr view <n> | View PR details |

| gh pr view <n> --json mergeable,reviewDecision,statusCheckRollup | PR readiness check |

| gh pr checks <n> | View CI check status |

| gh pr merge <n> --squash | Squash merge PR |

| gh pr merge <n> --merge | Merge commit PR |

| gh pr merge <n> --rebase | Rebase merge PR |

| gh pr merge <n> --auto | Enable auto-merge when checks pass |

| gh pr comment <n> --body "..." | Comment on PR |

| gh pr review <n> --approve | Approve a PR |

| gh pr ready <n> | Mark PR as ready for review |

Useful Inspection Commands

# See what will be merged (preview)
git log --oneline target..source

# See file-level diff between branches
git diff --stat target...source

# See if branches have diverged
git merge-base target source

# Check if branch is ancestor (can fast-forward)
git merge-base --is-ancestor source target && echo "already merged"

# Show conflict details during a merge
git diff --diff-filter=U

# Check which files would conflict (dry run)
git merge --no-commit --no-ff source && git merge --abort

Scope Control


Examples

Example 1: Merge a PR after readiness check

Task: Merge PR #42 into main

# 1. Check readiness
gh pr view 42 --json title,mergeable,reviewDecision,statusCheckRollup
gh pr checks 42

# 2. All checks pass, reviews approved → merge
gh pr merge 42 --squash

# 3. Validate locally
git pull origin main
pnpm build && pnpm test

# 4. Report
gh pr comment 42 --body "Merged via squash. Build and tests passing on main."

Example 2: Resolve merge conflicts

Task: Branch feat/user-auth conflicts with main

# 1. Fetch and attempt merge
git fetch origin
git checkout main && git pull
git merge --no-ff origin/feat/user-auth
# CONFLICT in src/auth.ts and src/routes.ts

# 2. Inspect conflicts
git diff --name-only --diff-filter=U
# → src/auth.ts, src/routes.ts

# 3. Read conflicting files, understand both sides
# (use Read tool on each file, find <<<<<<< markers)

# 4. Resolve each file
# (use Edit tool to replace conflict markers with correct merged code)

# 5. Mark resolved and commit
git add src/auth.ts src/routes.ts
git commit -m "merge: feat/user-auth into main — resolve auth + routes conflicts"

# 6. Validate
pnpm build && pnpm test

Example 3: Rollback a failed merge

Task: Merge of PR #55 broke the build, need to revert

# 1. Find the merge commit
git log --oneline -5
# abc1234 Merge pull request #55 from feat/dashboard

# 2. Revert the merge commit
git revert -m 1 abc1234

# 3. Validate rollback
pnpm build && pnpm test

# 4. Push and report
git push origin main
gh pr comment 55 --body "Reverted merge (commit abc1234) — build was failing due to missing dependency. Branch preserved for re-attempt after fix."

Example 4: Merge in a worktree (isolated merge without disrupting main working tree)

Task: Merge feat/payments into main without touching the current working tree

# 1. Create a worktree for the merge
git worktree add ../merge-payments main

# 2. Do the merge in the worktree
cd ../merge-payments
git merge --no-ff origin/feat/payments

# 3. If conflicts, resolve them in the worktree (main working tree untouched)
# (use Read/Edit tools on files in ../merge-payments/)

# 4. Validate in the worktree
pnpm install && pnpm build && pnpm test

# 5. Push from the worktree
git push origin main

# 6. Clean up the worktree
cd -
git worktree remove ../merge-payments

Example 5: Use worktree for PR review + merge

Task: Review and merge PR #77 in an isolated worktree

# 1. Create worktree on the PR branch
git worktree add ../review-pr77 feat/notifications

# 2. Inspect the code in the worktree
cd ../review-pr77
git log --oneline main..HEAD
git diff --stat main...HEAD

# 3. Run tests in isolation
pnpm install && pnpm test

# 4. If good, merge via gh (from any directory — gh uses the repo context)
gh pr merge 77 --squash

# 5. Clean up
cd -
git worktree remove ../review-pr77

Example 6: Resolve lockfile conflict

Task: pnpm-lock.yaml has conflicts after merge

# 1. Don't try to manually resolve the lockfile
git checkout --theirs pnpm-lock.yaml  # or --ours, doesn't matter

# 2. Regenerate from the merged package.json files
pnpm install

# 3. Stage the regenerated lockfile
git add pnpm-lock.yaml
git commit -m "merge: regenerate pnpm-lock.yaml after conflict"

Example 7: Manage multiple worktrees for parallel merges

Task: Merge three feature branches into develop in parallel worktrees

# 1. List existing worktrees
git worktree list

# 2. Create worktrees for each merge
git worktree add ../merge-feat-a develop
git worktree add -b develop-feat-b ../merge-feat-b develop
git worktree add -b develop-feat-c ../merge-feat-c develop

# 3. Merge in each worktree (can be done in parallel)
cd ../merge-feat-a && git merge --no-ff origin/feat/feature-a
cd ../merge-feat-b && git merge --no-ff origin/feat/feature-b
cd ../merge-feat-c && git merge --no-ff origin/feat/feature-c

# 4. Validate each, then sequentially integrate into develop
# (push from first worktree, rebase others onto updated develop)

# 5. Clean up all worktrees
cd /original/repo
git worktree remove ../merge-feat-a
git worktree remove ../merge-feat-b
git worktree remove ../merge-feat-c
git worktree prune
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.