Merging Branches and Fixing Conflicts
Merge a branch three ways: fast-forward, a merge commit, and a conflict you read, fix and finish with confidence.
In this lesson you will
- Merge a branch into main
- Tell a fast-forward from a merge commit
- Read conflict markers
- Resolve and finish a conflicted merge
About 30 minutes
Bringing the herbs home
Nia’s herb idea worked, so she wants it in main. The command is git merge, and it is always run from the branch that receives the changes. Standing on main, she merges add-herbs. Git has two ways to do it, and which one happens depends on the shape of the history.
Case one: fast-forward
If main has not moved since the branch was created, Git has nothing to combine. It simply slides the main label forward to the branch’s latest commit. The output says Fast-forward, and no new commit is created. The log is a straight line, as if the work had always happened on main.
$ mkdir garden
$ cd garden
$ git init
Initialized empty Git repository in /home/nia/garden/.git/
$ echo "Beans: row 1" > plan.txt
$ echo "Mon: Nia" > rota.txt
$ git add .
$ git commit -m "Add plan and rota"
[main (root-commit) a43202a] Add plan and rota
2 files changed, 2 insertions(+)
create mode 100644 plan.txt
create mode 100644 rota.txt
$ git switch -c add-herbs
Switched to a new branch 'add-herbs'
$ echo "Herbs: pots by the door" >> plan.txt
$ git commit -am "Add herb pots"
[add-herbs 4d3bf95] Add herb pots
1 file changed, 1 insertion(+)
$ git switch main
Switched to branch 'main'
$ git merge add-herbs
Updating a43202a..4d3bf95
Fast-forward
plan.txt | 1 +
1 file changed, 1 insertion(+)
$ git log --oneline
4d3bf95 Add herb pots
a43202a Add plan and rota
Case two: both sides moved
Usually other work lands on main while you are busy on your branch. Next, Nia adds carrots on a new branch while Dev adds a Tuesday to the rota on main. The two changes touch different files, so Git can combine them without help, and it records the combination as a merge commit, a commit with two parents. The graph shows the two lines meeting again.
$ git switch -c add-carrots
Switched to a new branch 'add-carrots'
$ echo "Carrots: row 2" >> plan.txt
$ git commit -am "Add carrots"
[add-carrots 81e0063] Add carrots
1 file changed, 1 insertion(+)
$ git switch main
Switched to branch 'main'
$ echo "Tue: Dev" >> rota.txt
$ git commit -am "Add Tuesday to the rota"
[main 15f1a55] Add Tuesday to the rota
1 file changed, 1 insertion(+)
$ git merge add-carrots --no-edit
Merge made by the 'ort' strategy.
plan.txt | 1 +
1 file changed, 1 insertion(+)
$ git log --oneline --graph
* 1b98503 Merge branch 'add-carrots'
|\
| * 81e0063 Add carrots
* | 15f1a55 Add Tuesday to the rota
|/
* 4d3bf95 Add herb pots
* a43202a Add plan and rota
Read the graph from the bottom. The history splits, then joins at a commit titled Merge branch ‘add-carrots’. Git wrote that message itself, because of the --no-edit option that accepts it unchanged.
Case three: a conflict
Now the hard case. Nia gives Monday’s watering duty to Asha on one branch, while Dev gives Monday to himself on main. Both changed the same line differently. Git cannot know who is right, so it stops and asks. This is a merge conflict, and it is a normal part of teamwork, not a failure. Do not panic. Follow what the output says.
$ git switch -c swap-monday
Switched to a new branch 'swap-monday'
$ sed -i 's/Mon: Nia/Mon: Asha/' rota.txt
$ git commit -am "Give Monday to Asha"
[swap-monday e5978aa] Give Monday to Asha
1 file changed, 1 insertion(+), 1 deletion(-)
$ git switch main
Switched to branch 'main'
$ sed -i 's/Mon: Nia/Mon: Dev/' rota.txt
$ git commit -am "Give Monday to Dev"
[main 8ea1076] Give Monday to Dev
1 file changed, 1 insertion(+), 1 deletion(-)
$ git merge swap-monday
Auto-merging rota.txt
CONFLICT (content): Merge conflict in rota.txt
Automatic merge failed; fix conflicts and then commit the result.
$ git status -s
UU rota.txt
$ cat rota.txt
<<<<<<< HEAD
Mon: Dev
=======
Mon: Asha
>>>>>>> swap-monday
Tue: Dev
The merge command stopped with an error, which is exactly what we expect here. Git listed the conflicted file, and git status -s shows it with the letters UU, meaning both sides changed it. The file itself now holds conflict markers: a line of less-than signs followed by the version from the branch you are on, a line of equals signs, the version from the other branch, and a line of greater-than signs. Everything outside the markers merged cleanly. Here is a short program that finds them:
text = open("/home/nia/garden/rota.txt").read()
lines = text.splitlines()
starts = [i for i, l in enumerate(lines) if l.startswith("<" * 7)]
middles = [i for i, l in enumerate(lines) if l == "=" * 7]
ends = [i for i, l in enumerate(lines) if l.startswith(">" * 7)]
print("conflict blocks:", len(starts))
print("marker lines:", len(starts) + len(middles) + len(ends))
print("our side:", lines[starts[0] + 1])
print("their side:", lines[middles[0] + 1])
conflict blocks: 1
marker lines: 3
our side: Mon: Dev
their side: Mon: Asha
It counted 1 conflict block and 3 marker lines. Your job is to decide what the file should say, then remove the markers. That decision belongs to people, not to Git. Nia decides that Asha takes Monday and Dev keeps Tuesday, so she writes the final text, stages the file, and finishes the merge with a commit.
$ printf "Mon: Asha\nTue: Dev\n" > rota.txt
$ cat rota.txt
Mon: Asha
Tue: Dev
$ git add rota.txt
$ git commit --no-edit
[main 5b7364a] Merge branch 'swap-monday'
$ git log --oneline --graph
* 5b7364a Merge branch 'swap-monday'
|\
| * e5978aa Give Monday to Asha
* | 8ea1076 Give Monday to Dev
|/
* 1b98503 Merge branch 'add-carrots'
|\
| * 81e0063 Add carrots
* | 15f1a55 Add Tuesday to the rota
|/
* 4d3bf95 Add herb pots
* a43202a Add plan and rota
$ git branch -d add-herbs add-carrots swap-monday
Deleted branch add-herbs (was 4d3bf95).
Deleted branch add-carrots (was 81e0063).
Deleted branch swap-monday (was e5978aa).
$ git branch
* main
Four steps fixed it: edit the file to the final text, run git add on it, run git commit, and check the graph. Your editor may help by highlighting both sides and offering buttons, and some projects use merge tools, but under the hood the work is the same. If you ever want to abandon a merge half way, git merge --abort returns everything to how it was before you started.
The last command deleted the three finished branches with git branch -d. The lowercase d is the safe version, because Git refuses to delete a branch that has not been merged. The commits stay in the history, and only the labels disappear.
Committing a file that still contains the marker lines. Search the file for the less-than and greater-than lines before you run git add. Leaving them in breaks the file for everybody.
The names and the rota are made up, and a real conflict can be bigger, with several blocks in several files. The method is the same each time: decide, edit, add, commit. Merge strategies and messages vary a little between Git versions.
Practice for this lesson
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
When does Git do a fast-forward merge?
- When there is a conflict
- When you use –abort
- When the branch is deleted
- When the receiving branch has not moved since the branch was made
main had no new commits, so its label just slid forward.
Which branch do you stand on when you run git merge add-herbs?
- The branch being merged in
- The one that should receive the changes, such as main
- Any branch
- A new remote
You merge into the branch you are on.
What causes a merge conflict?
- A long commit message
- Too many branches
- A slow computer
- Both sides changed the same lines differently
Asha and Dev both changed the Monday line.
What do you do to finish a conflicted merge?
- Edit the file, run git add, then git commit
- Delete the repository
- Run git init again
- Wait for GitHub
Nia wrote the final text, added the file and committed.
Which command abandons a merge in progress?
- git branch -d
- git restore –staged
- git merge –abort
- git log
It returns everything to how it was before the merge began.
Summary
- Merge from the branch that should receive the changes.
- A fast-forward just moves the label; a merge commit joins two lines of work.
- A conflict means both sides changed the same lines, and a person must decide.
- Resolve by editing, adding and committing; use –abort to back out.
Questions
Are conflicts a sign that I did something wrong?
No. They happen whenever two people change the same lines. Keeping branches short and merging often makes them smaller and rarer.
Can Git resolve a conflict for me?
Git merges everything it safely can. When both sides changed the same lines, it needs a person to choose, because only people know the intended result.
What if I resolve it wrongly?
You can revert the merge commit, or abort before committing. Because commits are saved, a wrong choice can be corrected with another commit.
