Lesson

Git Merge

10 min reading
Free Course

Git Merge: Combining Branch Histories Safely

git merge integrates commits from one branch into your active branch, uniting parallel development back into a single timeline. Before merging, switch to the receiving branch (usually main) so the incoming commits land in the right place.

Fast-Forward vs 3-Way Merge

Git picks its merge strategy automatically based on whether your receiving branch moved forward while you worked on the feature branch:

flowchart TD
    subgraph FastForward ["Fast-Forward Merge"]
        A1["main: C1"] --> A2["main: C2"] --> A3["feature: C3"]
    end
    subgraph ThreeWay ["3-Way Merge Commit"]
        B1["Base: C1"] --> B2["main: C2"]
        B1 --> B3["feature: C3"]
        B2 --> M["Merge Commit: M1"]
        B3 --> M
    end
  • Fast-Forward Merge: If main has no new commits since you branched feature/header-nav, Git simply slides the main pointer forward to the latest feature commit.
  • 3-Way Merge: If main received other commits in the meantime, Git creates a new merge commit with two parents that combines both timelines.

Running a Standard, --no-ff, or --squash Merge

First switch to main, then merge feature/header-nav and delete the finished branch:

git switch main
git merge feature/header-nav
git branch -d feature/header-nav

If you want a permanent merge commit even when fast-forward is possible, pass --no-ff. If you want to condense twenty messy WIP commits from a feature branch into a single staged changeset on main, pass --squash:

git merge --no-ff feature/header-nav
git merge --squash feature/header-nav
git commit -m "Implement header navigation and banner.svg"

Handling Conflicts During a Merge

If both main and feature/header-nav edited the same lines in index.html, Git pauses the merge and marks the conflicting lines with <<<<<<< HEAD, =======, and >>>>>>> feature/header-nav. Edit the file to keep the correct markup, remove the conflict markers, stage the file, and finish with git commit.

If you started a merge at the wrong time and want to back out cleanly, run:

git merge --abort

Common Merging Mistakes

Starting a merge while your working tree has uncommitted edits can cause Git to block the merge or mix unrelated changes into a conflict resolution. Always commit or stash local edits before running git merge.

Frequently Asked Questions

Which branch should I be on before running git merge?

Always check out the target branch that will receive the changes. To bring feature/header-nav into main, switch to main first, then run git merge feature/header-nav.

What is the difference between git merge and git rebase?

git merge preserves exact branch history and joins timelines with a merge commit when branches diverge. git rebase replays your feature commits onto the tip of main to produce a straight, linear history.

When should I use git merge --squash?

Use --squash when a feature branch has many noisy commits like "fix typo" or "wip" and you want one clean commit on main. Remember that --squash stages changes without creating the commit until you run git commit.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
Git Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum