Merging Branches
Merging Branches

Introduction
Merging combines changes from one branch into another. This lesson covers the merge process and common scenarios.
Basic Merge
Standard Merge
To merge a branch into your current branch:
git checkout main
git merge feature-branch
This merges feature-branch into main.
Merge with Message
git merge feature-branch -m "Merge feature-branch into main"
Understanding Merge Commits
When merging branches that have diverged, Git creates a merge commit:
main: A---B---M
\ /
feature: C---D---
- A, B - commits on main
- C, D - commits on feature
- M - merge commit combining both
Fast-Forward Merge
When the target branch has not been modified since creating the feature branch:
Before: main: A---B
\
feature: C---DAfter (fast-forward):
feature: A---B---C---D
To enable:
git merge feature-branch
Git automatically does fast-forward if possible.
Disable Fast-Forward
To always create a merge commit:
git merge --no-ff feature-branch
This preserves branch history even with fast-forward possible.
Types of Merges
Auto-Merge
Git automatically combines changes when no conflicts exist.
Manual Merge
When conflicts occur, you must resolve them manually (covered in next lesson).
Merge Options
Squash Merge
Combines all commits into one:
git merge --squash feature-branch
git commit -m "Add feature"
Useful for keeping main history clean.
Rebase Merge
Equivalent to rebasing before merge:
git merge --rebase feature-branch
Handling Multiple Branches
Merge Multiple Branches
git checkout main
git merge feature-a
git merge feature-b
git merge hotfix
Merge with Strategy
git merge -s recursive feature-branch
git merge -s ours feature-branch # Keep current, ignore other
git merge -s subtree branch-name # For subtrees
Aborting a Merge
During Conflict
git merge --abort
Returns to state before merge attempt.
After Partial Commit
git reset --hard ORIG_HEAD
Viewing Merges
Show Merged Branches
git branch --merged
Show Unmerged Branches
git branch --no-merged
Show Merge Commit Details
git show HEAD
Or:
git log --merges
Practical Examples
Example 1: Feature Completion
# Ensure main is up to date
git checkout main
git pull origin mainMerge feature branch
git merge feature-user-loginDelete feature branch
git branch -d feature-user-loginPush changes
git push origin main
Example 2: Squash and Merge
git checkout main
git merge --squash feature-branch
git commit -m "Complete user login feature"
git push origin main
Example 3: Hotfix Merge
# Merge hotfix into main
git checkout main
git merge hotfix-critical-fixAlso merge into current feature
git checkout feature-branch
git merge hotfix-critical-fixPush all
git push origin main feature-branch
Merge Best Practices
1. Always update main before merging 2. Review changes before merging 3. Use --no-ff for important features 4. Delete branches after merging 5. Test after merging
Summary
Key merge commands:
git merge branch- Merge branch into currentgit merge --no-ff- Always create merge commitgit merge --squash- Squash commitsgit merge --abort- Cancel mergegit branch --merged- Show merged branches
Next Lesson
Learn how to resolve merge conflicts when they occur.
Quiz - Quiz - Merging Branches
1. What is a fast-forward merge?
2. What command merges a branch into the current branch?
3. What flag prevents fast-forward merge, creating a merge commit?
4. What does --squash do in a merge?
5. What command cancels a merge in progress?