</>
Skip to content
Git lessons (14/44)

Git — Merge

What is Merge?

Merging takes changes from one branch and integrates them into another. It's how you bring feature work back into the main codebase.

# Merge feature-branch into current branch
git merge feature-branch

Types of Merges

Fast-Forward Merge

When the target branch has no new commits since the feature was created:

# Before:
# main: A → B → C
# feature: A → B → C → D → E

git switch main
git merge feature

# After:
# main: A → B → C → D → E

No new commit is created — main just moves forward.

Three-Way Merge

When both branches have new commits:

# Before:
# main: A → B → C → F
# feature: A → B → C → D → E

git switch main
git merge feature

# After:
# main: A → B → C → F → M (merge commit)
#                   ↘ D → E ↗

A new merge commit M is created, combining both histories.

Merge Conflicts

When Git can't automatically merge changes, you get a conflict:

# Automatic merge failed
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

What Conflict Looks Like

<<<<<<< HEAD
<p>Welcome to our website!</p>
=======
<p>Welcome to our new website!</p>
>>>>>>> feature-update
  • <<<<<<< HEAD — your current branch's version
  • ======= — separator
  • >>>>>>> feature-update — incoming branch's version

Resolving Conflicts

# 1. Open the conflicted file
# 2. Edit to keep what you want
<p>Welcome to our new website!</p>

# 3. Remove conflict markers (<<<<<<< ======= >>>>>>>)

# 4. Stage the resolved file
git add index.html

# 5. Commit
git commit -m "Resolve merge conflict in index.html"

Abort a Merge

# Start over if conflicts are too complex
git merge --abort

Merge Strategies

--no-ff (No Fast-Forward)

Always creates a merge commit, even for fast-forward:

git merge --no-ff feature-branch -m "Merge feature-branch"

This preserves the branch history, making it easier to see what was a feature branch.

Squash Merge

Combines all commits into one:

git merge --squash feature-branch
git commit -m "Add complete feature"

Useful for cleaning up messy commit histories.

Viewing Merge History

# Show merge commits
git log --merges

# Show graph
git log --graph --oneline --all

# See what was merged
git log --oneline main..feature-branch

Practical Example

# Create feature branch
git switch -c feature/contact-form

# Make changes
echo "contact form" > contact.html
git add contact.html
git commit -m "feat: add contact form"

# Switch to main
git switch main

# Make a change on main
echo "updated readme" >> README.md
git add README.md
git commit -m "docs: update readme"

# Merge feature
git merge feature/contact-form

# Handle any conflicts...
# Push to remote
git push

Best Practices

  • Always pull before merging
  • Use --no-ff to preserve branch history
  • Review changes before merging
  • Resolve conflicts carefully — test after resolving
  • Delete merged branches
  • Use pull requests for team code review

Mini Practice

  1. Create a branch, make commits, and merge into main
  2. Create a merge conflict and resolve it
  3. Use --no-ff to force a merge commit
  4. Use --squash to combine multiple commits
  5. View the merge graph with git log --graph

Up Next

Next: Conflicts →

Related Topics

Frequently Asked Questions about Merge

What is Merge in Git?

Merge is a fundamental concept in Git. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Merge?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Merge.

Why is Merge important in Git?

Merge is essential for Git development. Understanding this concept will help you write better code and solve real-world problems more effectively.