Merging Branches
Combine changes from one branch into another using git merge.
Types of Merges
Fast-Forward Merge
When the target branch has no new commits:
Before:
main: A---B
\
feature: C---D
After (fast-forward):
main: A---B---C---D
Three-Way Merge
When both branches have new commits:
Before:
main: A---B---E
\
feature: C---D
After:
main: A---B---E---M (merge commit)
\ /
feature: C---D
Merging Branches
Basic Merge
# Switch to target branch
git checkout main
# Merge source branch
git merge feature
Merge Options
| Option | Description |
|---|---|
--no-ff | Force merge commit (no fast-forward) |
--squash | Combine all commits into one |
--abort | Cancel merge in progress |
No Fast-Forward
git merge --no-ff feature
Creates a merge commit even if fast-forward is possible.
Squash Merge
git merge --squash feature
git commit -m "Add feature"
Combines all feature commits into one.
Merge Conflicts
Conflicts occur when the same lines were changed in both branches.
<<<<<<< HEAD
current branch code
=======
incoming branch code
>>>>>>> feature
To resolve:
- Edit the file to fix conflicts
- Remove conflict markers
git add <file>git commit
Exercise: Merge a Branch
Create a feature branch, switch back to main, and merge:

