Merging Changes in Git
Merging combines changes from different branches into one.
Basic Merge
bash
1# Switch to target branch
2git checkout main
3
4# Merge source branch
5git merge feature-branchTypes of Merges
1. Fast-Forward Merge
When main hasn't changed since branch creation:
Before:
main: A ─ B ─ C
\
feature: D ─ E
After fast-forward:
main: A ─ B ─ C ─ D ─ E
bash
1git checkout main
2git merge feature-branch
3# Fast-forward (no merge commit)2. Three-Way Merge
When both branches have new commits:
Before:
main: A ─ B ─ C ─ F
\
feature: D ─ E
After merge:
main: A ─ B ─ C ─ F ─ M
\ /
feature: D ─ E
bash
1git checkout main
2git merge feature-branch
3# Creates merge commit MMerge Options
No Fast-Forward
Always create merge commit:
bash
1git merge --no-ff feature-branchUseful for:
- Preserving feature branch history
- Easier to revert features
- Clear branch structure in log
Squash Merge
Combine all commits into one:
bash
1git merge --squash feature-branch
2git commit -m "Add feature"Abort Merge
If something goes wrong:
bash
1git merge --abortHandling Merge Conflicts
When Conflicts Happen
bash
1git merge feature-branch
2# Auto-merging file.txt
3# CONFLICT (content): Merge conflict in file.txt
4# Automatic merge failed; fix conflicts and commitView Conflicts
bash
1git status
2# both modified: file.txtConflict Markers
<<<<<<< HEAD
Content from current branch (main)
=======
Content from merging branch (feature)
>>>>>>> feature-branch
Resolve Conflicts
- Open the conflicting file
- Decide what to keep
- Remove conflict markers
- Save the file
Example Resolution:
javascript
1// Keep both changes combined
2function greet(name) {
3 console.log(`Hello, ${name}!`);
4 console.log('Welcome to the app!');
5}Complete the Merge
bash
1# Stage resolved files
2git add file.txt
3
4# Complete merge
5git commit -m "Merge feature-branch, resolve conflicts"Merge Strategies
Recursive (Default)
bash
1git merge -s recursive feature-branchBest for most merges.
Ours
Keep our version, ignore theirs:
bash
1git merge -s ours feature-branchTheirs
Not a built-in strategy, but can use:
bash
1git merge -X theirs feature-branchMerge Workflow Example
bash
1# 1. Ensure main is up to date
2git checkout main
3git pull origin main
4
5# 2. Merge feature branch
6git merge feature/user-auth
7
8# 3. If conflicts, resolve them
9# Edit conflicting files
10git add .
11
12# 4. If no conflicts, push
13git push origin main
14
15# 5. Delete merged branch
16git branch -d feature/user-authView Merge History
bash
1# See merge commits
2git log --merges
3
4# Graphical view
5git log --oneline --graph
6
7# Show branches merged into main
8git branch --merged mainMerge Best Practices
1. Keep Branches Up to Date
bash
1# Before merging to main, update your branch
2git checkout feature-branch
3git merge main # or rebase2. Test Before Merging
bash
1# On feature branch
2npm test
3# If tests pass, proceed with merge3. Review the Diff
bash
1# Preview what will be merged
2git diff main...feature-branch4. Use Descriptive Merge Messages
bash
1git merge feature-branch -m "Merge feature/user-auth: Add login functionality"5. Delete Merged Branches
bash
1git branch -d feature-branchSummary
| Merge Type | Command | Use Case |
|---|---|---|
| Regular | git merge branch | Standard merge |
| No FF | git merge --no-ff | Preserve branch history |
| Squash | git merge --squash | Clean single commit |
| Abort | git merge --abort | Cancel merge |
Next: Learn about rebasing as an alternative to merging!
