Sub topic 1
What git merge does
Prerequisites
This chapter assumes you can do the following:
- Run
git init,git add,git commit,git statusandgit log. - Create and switch branches with
git switch. - Use a command line and a text editor.
What a merge does
git merge combines the work from another branch into the branch you have checked out. You run it from the branch that should receive the changes. The other branch is not modified.
git switch main
git merge featureAfter this, main contains the commits from feature. Git does not copy files over. It compares three snapshots, works out what each branch changed, and combines those changes.
The three snapshots
Git picks three commits for a merge:
- The tip of your current branch (often called "ours").
- The tip of the branch you are merging (often called "theirs").
- Their most recent common ancestor, called the merge base.
For each file, Git asks what changed between the merge base and each tip. If only one side changed a part of a file, Git takes that change. If both sides changed the same lines in different ways, Git cannot choose and reports a conflict. Later chapters cover the merge base and conflicts in detail.
Two outcomes
What Git does next depends on how the histories relate.
| Situation | Result |
|---|---|
| Your branch has no commits that the other branch lacks | Fast-forward: Git moves the branch pointer forward. No new commit. |
| Both branches have new commits | Merge commit: Git creates a commit with two parents. |
A merge commit records both lines of history. A fast-forward only moves the pointer, so history stays a straight line.
A worked example
This script builds two branches that change different files, so the merge succeeds without conflict.
git init -b main demo && cd demo
printf 'timeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'hello\n' > notes.txt
git add notes.txt && git commit -m "Add notes"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout to 30"
git merge feature
git log --oneline --graphmain and feature each have one commit the other lacks, so Git creates a merge commit. The --graph output shows the two lines of history joining. Git opens your editor for the merge commit message. Save and close it to finish.
If you had run git merge feature before committing on main, Git would have fast-forwarded instead.
When a merge stops
If both branches changed the same lines, git merge stops partway. It leaves the merge unfinished and marks the problem files. Run git status to see which paths need attention. The next chapters show how to read and resolve these cases.
Exercise
You are on main and run git merge feature. main has two commits that feature does not have, and feature has one commit that main does not have. Will Git fast-forward? What will it create?
Show answer
No. Both branches have commits the other lacks, so a fast-forward is impossible. Git does a three-way merge using the merge base and creates a merge commit with two parents, unless the same lines were changed on both sides, in which case it stops with a conflict.