Sub topic 7
Merging diverged branches
This chapter assumes you can create and switch branches and make commits. It also assumes you know the fast-forward merge from the previous chapter. You need Git 2.28 or later for git init -b. Everything else works on older versions.
What "diverged" means
Two branches have diverged when each has commits the other lacks. Git can't fast-forward in that case, because moving the main pointer to the tip of feature/login would drop the commits that exist only on main.
Here is a typical shape. Both branches share commits A and B. After that, feature/login gained commit C and main gained commit D.
The result is a merge commit. A merge commit is an ordinary commit with two parents, so history stays a graph rather than a straight line.
How a three-way merge works
Git needs three snapshots to merge:
- The merge base. This is the most recent commit reachable from both branches. In the diagram it is B.
- Ours. This is the tip of the branch you are on (D).
- Theirs. This is the tip of the branch you are merging in (C).
Git compares the base to each side. Compared with the base, the changes on each side fall into one of these cases:
| Base to ours | Base to theirs | Result |
|---|---|---|
| Unchanged | Changed | Take theirs |
| Changed | Unchanged | Take ours |
| Changed the same way | Changed the same way | Take either (identical) |
| Changed one way | Changed a different way, in the same lines | Conflict |
| Changed in separate regions of the file | Changed in separate regions of the file | Combine both automatically |
The base is what makes the merge possible. Without it, Git could only see that two files differ. It couldn't tell which side changed them.
You can ask Git for the merge base directly:
git merge-base main feature/loginIt prints the SHA of commit B.
A complete example
This script builds a diverged repository and merges it. It touches different files on each branch, so there is no conflict.
git init -b main merge-demo
cd merge-demo
echo "one" > a.txt
git add a.txt
git commit -m "Add a.txt"
git switch -c feature/login
echo "login" > login.txt
git add login.txt
git commit -m "Add login page"
git switch main
echo "docs" > README.md
git add README.md
git commit -m "Add README"
git merge feature/login -m "Merge feature/login"
git log --oneline --graph --decorateThe graph looks like this. Your hashes will differ.
* 3f9c1ab (HEAD -> main) Merge feature/login
|\
| * 8d2e7c0 (feature/login) Add login page
* | b41a95e Add README
|/
* 5c0d3fa Add a.txtRead the graph from the top. The * at the top is the merge commit. The two lines under it are its two parents. Both lines join again at "Add a.txt", the merge base.
The merge runs on the branch you are standing on. git merge feature/login changes main and leaves feature/login where it was. The merge commit's first parent is the old main tip. Its second parent is the tip of the branch you merged in. Commands such as git log --first-parent rely on this order to follow only the mainline.
The merge commit message
Without -m, Git opens your editor with a default message such as Merge branch 'feature/login'. Accept it or improve it. A useful message says why the work is landing, not only where it came from.
If you change your mind while the editor is open, abort by leaving the message empty. If the merge has already stopped for another reason, run git merge --abort to restore the pre-merge state.
Forcing a merge commit with --no-ff
If main hasn't moved, Git fast-forwards by default and leaves no merge commit. Some teams want a merge commit anyway. It marks where a feature landed and gives you one commit to point at, or to revert. Use --no-ff for that:
git merge --no-ff feature/login
# Git stops on a conflict. Edit the files containing <<<<<<<, =======, >>>>>>> markers, then:
git add path/to/conflicted-file
git commitThe conflict lines in that snippet are for the case where the merge does conflict. The next chapter covers them in detail. For now, know that a conflicted merge stops halfway and waits for you.
The opposite option is --ff-only. It refuses to create a merge commit and fails if a fast-forward isn't possible. It is a safe choice for scripts and for updating a branch that should only move forward.
You can set these as defaults in your configuration:
git config --global merge.ff false # always create a merge commit
git config --global merge.ff only # only allow fast-forwardSet one or the other, not both.
Checking before you merge
Look at what is about to be combined before you commit to it:
git log --oneline main..feature/login # commits only on the feature branch
git log --oneline feature/login..main # commits only on main
git diff main...feature/login # changes on the feature since the merge baseThe two-dot form A..B means "commits reachable from B but not from A". The three-dot form in git diff compares the merge base against the second branch. This is the same set of changes the three-way merge will apply from that side.
To see what state a merge left, use git status. It lists the files that merged cleanly and any that conflict. After a clean merge, git status reports that your branch is ahead of its remote by the number of new commits, which includes the merge commit.
Common mistakes
- Merging in the wrong direction.
git merge mainwhile onfeature/loginbringsmaininto your feature. That is a legitimate way to catch up, but it creates a merge commit on the feature branch. Checkgit branch --show-currentbefore you merge. - Merging with a dirty working tree. Git refuses if uncommitted changes would be overwritten. Commit or stash first.
- Assuming no conflict means correct code. Git merges text, not meaning. Two branches can merge cleanly and still break each other, for example if one renames a function and the other adds a call to the old name. Run your tests after every merge.
- Reverting a merge naively.
git revert <merge-sha>fails unless you say which parent to keep, with-m 1for the mainline. Even after that revert, merging the same branch again will not bring back its changes, because Git considers them already merged.
Choosing a strategy option
The default strategy handles nearly every merge. Two options are worth knowing:
-X oursand-X theirsresolve conflicting hunks in favor of one side. Non-conflicting changes from both sides are still merged. Use them with care, because they discard changes without asking you.-s oursis a different thing. It creates a merge commit but keeps your tree unchanged and ignores the other branch's content entirely. It records "this was merged" for history purposes only.
The names look alike, so check which one you typed.
Exercise
You are on main, which has one commit that feature/login lacks. feature/login has two commits that main lacks. You run git merge feature/login. How many parents does the new commit have, what is the merge base, and which branch pointer moves?
Show answer
The new merge commit has two parents: the old main tip (first parent) and the feature/login tip (second parent). The merge base is the last commit both branches share, from before they diverged. Only main moves, to the new merge commit. feature/login stays where it was.
Exercise
Why can Git merge two branches that edit different parts of the same file without asking you, and what does it need that a plain two-way diff does not provide?
Show answer
Git compares each branch against the merge base. Where only one side changed a region, it takes that change. Where the changes are in separate regions, it combines them. A two-way diff only shows that the files differ, so it can't tell which side made each change. The merge base supplies that information.