Merging_and_Resolving_Conflicts
Merging and Resolving Conflicts
Combine branches and handle conflicts.
π Basic Merging
Merge Branch into Current
git merge feature-branch
Merges
feature-branchinto current branch.
Merge with Commit Message
git merge feature-branch -m "Merge feature into main"
Merges with custom message.
No Fast-Forward Merge
git merge --no-ff feature-branch
Always creates merge commit, even if fast-forward possible.
Fast-Forward Only
git merge --ff-only feature-branch
Only merges if fast-forward possible. Fails otherwise.
π Merge Types
graph TD
A[Merge Types] --> B[Fast-Forward]
A --> C[Three-Way Merge]
A --> D[Squash Merge]
B --> E[No merge commit]
C --> F[Creates merge commit]
D --> G[Single commit, no history]
π Fast-Forward Merge
When branch is directly ahead:
Before:
main: A---B
\
feature: C---D
After git merge feature:
main: A---B---C---D
π Three-Way Merge
When branches have diverged:
Before:
main: A---B---E
\
feature: C---D
After git merge feature:
main: A---B---E---M
\ /
feature: C---D
β οΈ Handling Conflicts
Check for Conflicts
git status
Shows files with conflicts marked as "both modified".
View Conflict Markers
Conflict in file looks like:
<<<<<<< HEAD
Your changes here
=======
Their changes here
>>>>>>> feature-branch
Steps to Resolve
- Open conflicted file
- Find conflict markers (
<<<<<<<,=======,>>>>>>>) - Choose/combine changes
- Remove conflict markers
- Stage and commit
Stage Resolved File
git add resolved-file.txt
Marks file as resolved.
Complete Merge
git commit
Completes the merge (editor opens for message).
With Message
git commit -m "Resolve merge conflicts"
Completes merge with inline message.
β Abort Merge
Cancel and Return to Pre-merge State
git merge --abort
Aborts merge and returns to state before merge started.
π§ Merge Tools
Open Merge Tool
git mergetool
Opens configured merge tool for conflict resolution.
Configure Merge Tool (VS Code)
git config --global merge.tool vscode
Sets VS Code as merge tool.
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
Configures the command.
Skip Backup Files
git config --global mergetool.keepBackup false
Prevents creation of
.origbackup files.
π Squash Merge
Squash All Commits
git merge --squash feature-branch
Brings all changes but doesn't commit. All commits squashed into one.
Commit Squashed Changes
git commit -m "Add feature (squashed)"
Commits the squashed changes.
π View Merge Commits
Show Merge Commits
git log --merges
Shows only merge commits.
Show Non-Merge Commits
git log --no-merges
Shows only non-merge commits.
π‘ Tips
git merge --no-commit --no-ff feature-branch
git diff --cached
git merge --abort
git merge --no-commit feature-branch
Always ensure your branch is in good state before merging.
π Related
#git #merge #conflict #resolution