# Merging Basics

> How to safely integrate diverged histories and bring features into the main branch.

Subject: Version Control | Lesson 10 of 29 | 5 min read
Interactive lesson: https://codewithanimation.in/git/git-merge
Previous: [Creating & Switching Branches](https://codewithanimation.in/git/git-branch-and-switch.md) | Next: [Fast-Forward vs 3-Way Merges](https://codewithanimation.in/git/fast-forward-vs-3-way.md)

## Integrating Histories

`git merge` is the command used to join two or more development histories together.

It incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch.

## Why it matters

Web developers constantly build new features, fix bugs, or experiment with UI updates in isolated branches.

`git merge` allows them to safely integrate these parallel lines of work into a central integration branch (like `main` or `develop`) so the application can be deployed with everyone's contributions combined.

## How it works

Git takes two commit pointers (usually the current branch tip and the target branch tip) and finds their common base commit (the merge base).

It then creates a new 'merge commit' that combines the changes from both sequences. If the history is linear, Git performs a 'fast-forward' merge by simply moving the current branch pointer forward without creating a new merge commit.

## Common mistake: Merging into the wrong branch

The #1 common mistake is merging into the wrong receiving branch. Because `git merge` applies changes *into* the currently checked-out branch, developers often forget to switch to the destination branch first.

For example, remaining on `feature-branch` and running `git merge main` incorrectly brings `main` into the feature branch instead of the other way around.

## Tip: Git merge pulls INTO where you stand

Always run `git status` to ensure you are standing on the correct receiving branch (like `main`).

And always `git pull` to get the latest remote changes *before* you execute the merge command.

## Try it: Merge a feature into main

Compare the two histories, then integrate the feature into the selected branch.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-merge

## Check yourself

### Question 1

What does the `git merge` command fundamentally do?

- A. Uploads your local commits to a remote server.
- B. Downloads remote changes without integrating them.
- C. Joins two or more development histories together into the current branch.
- D. Switches your working directory to another branch.

Correct answer: C

`git merge` takes independent lines of development (like separate branches) and brings their histories together into a single, unified branch.

- Why not A: That is what `git push` does.
- Why not B: That is the purpose of `git fetch`.
- Why not D: That is handled by `git checkout` or `git switch`.

### Question 2

You want to merge the `header-fix` branch into the `main` branch. What must you do first before running the merge command?

- A. Checkout the `header-fix` branch.
- B. Delete the `header-fix` branch.
- C. Checkout the `main` branch.
- D. Run `git push header-fix`.

Correct answer: C

`git merge <branch>` incorporates changes from `<branch>` into your *current* branch. Therefore, you must be standing on the receiving branch (`main`) before executing the command.

- Why not A: Merges happen *into* your current branch, so doing this would merge `main` into `header-fix`.
- Why not B: You cannot merge a branch if you delete it first!
- Why not D: Pushing updates the remote repository, but it doesn't perform a local merge between branches.

### Question 3

What happens when Git encounters a 'merge conflict'?

- A. Git automatically chooses the most recent changes and discards the older ones.
- B. Git pauses the merge and asks the user to manually resolve the conflicting changes.
- C. Git deletes both branches and creates a fresh commit.
- D. Git reverts all changes back to the common base commit.

Correct answer: B

A merge conflict occurs when the same part of a file has been modified differently in the two branches being merged. Git safely halts the automatic process and relies on human intervention to choose the correct code.

- Why not A: Git never guesses how to resolve conflicting lines; it prioritizes safety and requires human input.
- Why not C: Git does not delete branches on conflicts; it preserves your work safely.
- Why not D: Git simply halts the merge and waits for you to resolve it; it doesn't wipe your progress.

## Remember this

- Git merge pulls INTO where you currently stand (your active branch).
- A fast-forward merge simply moves the pointer; a 3-way merge creates a new commit.
- Merge conflicts require human resolution when lines overlap.
