Git Merge & Conflict Resolution
Handle complex git operations: merges, cherry-picks, rebases, and conflict resolution.
Parse the user's input to determine the action:
$git-mergeor$git-merge resolve— detect and resolve current conflicts$git-merge branch <name>— merge a branch into current, handle conflicts$git-merge cherry-pick <commit>...— cherry-pick with conflict handling$git-merge rebase <base>— non-interactive rebase onto base$git-merge abort— abort in-progress merge/rebase/cherry-pick$git-merge status— show current merge/rebase/cherry-pick state
Prerequisites
- Verify we're in a git repository (
git rev-parse --is-inside-work-tree) - If not in a git repo, tell the user and stop
Action: status
- Run
git statusto show branch and state - Check for in-progress operations:
.git/MERGE_HEAD→ merge in progress.git/rebase-merge/or.git/rebase-apply/→ rebase in progress.git/CHERRY_PICK_HEAD→ cherry-pick in progress
- If conflicts exist, list conflicted files with
git status --porcelain(look forUU,AA,DD,AU,UA) - Report current branch, upstream tracking, and any in-progress operation
Action: resolve (default)
- Run
git status --porcelainto find conflicted files (UU,AA,DD,AU,UAprefixes) - If no conflicts found, report clean state and stop
- For each conflicted file:
a. Read the file to find
<<<<<<<,=======,>>>>>>>conflict markers b. Read surrounding context and related tests to understand intent of both sides c. If the resolution is clear (e.g., both sides made non-overlapping changes within the same hunk), resolve by editing the file — remove all conflict markers and produce the correct merged result d. If the resolution is ambiguous, show both sides to the user and ask which approach to take — never silently pick a side e. For binary file conflicts, ask the user to choose--oursor--theirsf. After resolving each file, rungit add <file> - After all files are resolved:
- If merge:
git commit(use git's default merge message, do NOT use conventional commit style) - If rebase:
git rebase --continue - If cherry-pick:
git cherry-pick --continue
- If merge:
Action: branch
- Verify the branch exists (
git rev-parse --verify <name>) - Show what will be merged:
git log --oneline HEAD..<name>(commits coming in) - Run
git merge <name> - If merge succeeds cleanly, report success with merge commit
- If merge has conflicts, follow the resolve workflow above
- Use git's default merge commit message — do NOT override with conventional commit style
Action: cherry-pick ...
- Validate each commit exists (
git rev-parse --verify <commit>) - Show what will be cherry-picked:
git log --oneline -1 <commit>for each - Run
git cherry-pick <commit>... - If clean, report success
- If conflicts, follow the resolve workflow above
Action: rebase
- Identify current branch name
- Protected branch warning: If current branch is
main,master, ordevelop, warn the user that rebasing a protected branch is dangerous and ask for explicit confirmation before proceeding - Show what will be replayed:
git log --oneline <base>..HEAD - Warn the user: after rebase, they'll need
git push --force-with-leaseto update the remote — never force-push automatically - Run
git rebase <base>(non-interactive only —git rebase -irequires an interactive terminal) - If clean, report success
- If conflicts, follow the resolve workflow for each step, using
git rebase --continueafter each resolution
Action: abort
- Detect what's in progress:
.git/MERGE_HEAD→git merge --abort.git/rebase-merge/or.git/rebase-apply/→git rebase --abort.git/CHERRY_PICK_HEAD→git cherry-pick --abort
- If nothing is in progress, tell the user
- Run the appropriate abort command
- Show
git statusto confirm clean state
Safety Rules
- Never force-push — warn the user they'll need
--force-with-leaseafter rebase - Never use
git rebase -i— interactive mode requires a terminal that cannot be provided in this context - Ask on ambiguous conflicts — never silently choose a side
- Warn on protected branches —
main,master,developget explicit warnings before rebase - Merge commits use git's default message — only manual commits follow conventional commit style
- Check for uncommitted changes before starting operations — warn if working tree is dirty