my-notes

Merging_and_Resolving_Conflicts

1 min read

Merging and Resolving Conflicts

Combine branches and handle conflicts.


πŸ”€ Basic Merging

Merge Branch into Current

git merge feature-branch

Merges feature-branch into 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

  1. Open conflicted file
  2. Find conflict markers (<<<<<<<, =======, >>>>>>>)
  3. Choose/combine changes
  4. Remove conflict markers
  5. 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 .orig backup 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

Preview Merge
git merge --no-commit --no-ff feature-branch
git diff --cached
git merge --abort
Check What Would Conflict
git merge --no-commit feature-branch
Never Merge Unfinished Work

Always ensure your branch is in good state before merging.



#git #merge #conflict #resolution

Built with mdgarden