Skip
Arish's avatar

45. How to Merge Changes


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-branch

Types 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 M

Merge Options

No Fast-Forward

Always create merge commit:

bash
1git merge --no-ff feature-branch

Useful 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 --abort

Handling 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 commit

View Conflicts

bash
1git status
2# both modified: file.txt

Conflict Markers

<<<<<<< HEAD Content from current branch (main) ======= Content from merging branch (feature) >>>>>>> feature-branch

Resolve Conflicts

  1. Open the conflicting file
  2. Decide what to keep
  3. Remove conflict markers
  4. 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-branch

Best for most merges.

Ours

Keep our version, ignore theirs:

bash
1git merge -s ours feature-branch

Theirs

Not a built-in strategy, but can use:

bash
1git merge -X theirs feature-branch

Merge 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-auth

View 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 main

Merge 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 rebase

2. Test Before Merging

bash
1# On feature branch
2npm test
3# If tests pass, proceed with merge

3. Review the Diff

bash
1# Preview what will be merged
2git diff main...feature-branch

4. 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-branch

Summary

Merge TypeCommandUse Case
Regulargit merge branchStandard merge
No FFgit merge --no-ffPreserve branch history
Squashgit merge --squashClean single commit
Abortgit merge --abortCancel merge

Next: Learn about rebasing as an alternative to merging!