Tour of Git
Branching & Merging / Lesson 2.4

git merge — Combining Work

The git merge command integrates changes from one branch into another. It's how you bring feature work back into main.

How Merge Works

When you run git merge feature while on main:

  1. Git finds the common ancestor of both branches
  2. It combines the changes from both sides
  3. It creates a new merge commit with two parents
main:     A --- B --- C ------- M
                 \             /
feature:          D --- E ----

The merge commit M has two parents: C (from main) and E (from feature).

Fast-Forward Merge

If main hasn't moved since the branch was created, Git can just move the pointer forward — no merge commit needed:

Before:  main → A --- B    feature → C --- D
After:   main → A --- B --- C --- D ← feature

Try it!

Build a feature branch and merge it:

  1. Create a commit on main: echo "base" > base.txt then git add base.txt then git commit -m "Add base"
  2. Create and switch to feature: git checkout -b feature
  3. Add a feature commit: echo "feature" > feature.txt then git add feature.txt then git commit -m "Add feature"
  4. Switch back to main: git checkout main
  5. Merge: git merge feature

Watch the graph — you should see the merge commit connecting both lines of history.

After Merging

Once merged, the feature branch is no longer needed. Clean up with:

git branch -d feature

The commits from the branch are preserved in main's history through the merge commit.

🎯

Goal

Create a feature branch, commit to it, then merge it back into main

Terminal
$

No commits yet. Run `git init` and create your first commit.