--- name: git-merge description: Merge and rebase git branches — conflict resolution strategies, merge vs rebase vs squash, interactive rebase, history cleanup. license: MIT --- ## What I do Handle branch merging, rebasing, conflict resolution. Detect main branch dynamically. ## Detect Main Branch ```bash MAIN=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@') ``` ## Merge Workflow ### 1. Prepare ```bash git fetch origin git checkout $MAIN git pull origin $MAIN ``` ### 2. Merge or Rebase **Merge** (preserves history): ```bash git merge feature-branch git push origin $MAIN ``` **Rebase** (cleaner history): ```bash git checkout feature-branch git rebase $MAIN git checkout $MAIN git merge feature-branch git push origin $MAIN ``` ### 3. Cleanup ```bash git branch -d feature-branch git push origin --delete feature-branch ``` ## Conflict Resolution ### Conflict Markers ``` <<<<<<< HEAD current changes ======= incoming changes >>>>>>> feature-branch ``` ### Resolve ```bash # See conflicted files git status git diff --name-only --diff-filter=U # Manual resolution: edit file, remove markers # Or accept one side git checkout --ours file.rs # Accept HEAD git checkout --theirs file.rs # Accept incoming # Stage and complete git add . git commit # or git rebase --continue ``` ## Conflict Types - Same line changed: Choose or combine - File deleted/modified: Keep or restore - Renamed differently: Choose name ## Best Practices - [ ] Pull latest before merging - [ ] Run tests locally - [ ] Understand both sides before choosing - [ ] Remove all conflict markers - [ ] Test after resolving ## Abort ```bash git merge --abort git rebase --abort ``` --- Use this skill for confident conflict resolution.