Git Merge and Conflicts
Level 2 · Branches and merges
How Git joins one branch into another. Fast-forward, merge commit and squash merge side by side, and how to fix a merge conflict by hand.
Course contentsLesson 5 of 13
Level 1 · Your first commits
Level 2 · Branches and merges
Level 3 · Undo and set aside
Level 4 · Remotes and GitHub
Level 5 · Working as a team
After this lesson you can
- Tell the three kinds of merge apart
- Pick a merge for your team
- Fix a merge conflict
14 mincovers 4 glossary entries
Worth reading first: Git Branches and HEAD
How Git joins two branches
A merge joins the work from one branch into another. You stand on the branch that should get the work, usually main, and run git merge with the other branch's name. Git picks one of two ways to do it, depending on whether main moved on while you worked. A third way, the squash, is one you ask for.
If main has no new commits since your branch split off, there is nothing to combine. Git just slides the main label forward to your newest commit. That is a fast-forward merge. No new commit is made, and the history stays one straight line.
If both branches have new commits, Git makes a merge commit. It is a commit with two parents, one from each line, and it ties them together. The history keeps every small step from the branch, plus the point where the two lines met.
A squash merge turns all the commits on a branch into one new commit on main. Twelve small steps like fix typo and try again become one tidy commit, such as Add a dark mode switch. Main's history stays short. The cost is that the small steps are not kept on main.
A merge conflict happens when both branches changed the same lines in different ways. Git cannot guess which one is right, so it stops halfway and asks you. Conflicts are normal. They do not mean anything is broken, only that two people touched the same spot.
Git marks the spot inside the file with three marker lines. Your side sits between the line of less-than signs and the line of equals signs. The other branch's side sits below, down to the line of greater-than signs. Edit the file to keep what should stay, delete all three markers, then add the file and commit.
If a conflict looks too big, git merge --abort puts everything back the way it was before you started. Nothing is lost. Pulling in main often, and keeping branches small, is what keeps conflicts rare and short.
The classic mistake is to commit a file with the markers still in it. The code then breaks, often in an odd way far from the conflict. Before you commit, search the file for seven less-than signs in a row. Most code editors also colour the two sides, so a leftover marker is easy to spot.
The three ways to merge
| Merge | When it happens | What main's history shows |
|---|---|---|
| Fast-forward | main has no new commits | Your commits, in one straight line |
| Merge commit | Both branches moved on | Every commit, plus one joining commit |
| Squash merge | You ask for it | One new commit for the whole branch |
Which one to pick
- 1
You do not pick it. Git does it on its own when main has not moved, and it is always safe.
- 2
Merge commit
When you want to keep every step, and see where each branch joined.
- 3
When a branch is full of small fix-up commits, and main should read as one change per feature.
$ git merge red
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
$ cat index.html
<<<<<<< HEAD
<h1>Blue shop</h1>
=======
<h1>Red shop</h1>
>>>>>>> redWorked example
The words for this lesson4
Check yourself
What does a merge conflict mean?
Finished this lesson?
Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.