Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Git merge and merge conflicts

After this lesson: I can merge two branches and settle a conflict.

60 min lesson16 commandsGit and Gitea lab: learn clone, commit and push on a Git server of your own

What a Git merge does

A Git merge brings the work of one branch into another. When both branches changed the same line, Git cannot choose between them, and it reports a merge conflict.

Two staff change the shop's opening hours on different branches. One makes the day longer. The other makes it start earlier. Git needs a person to decide.

By the end of this lesson

Note

I can merge two branches and settle a conflict.

Key words

WordWhat it means
Mergeto bring the work of one branch into another
Fast-forwarda merge that only moves a branch name forward
Merge commita commit with two parents that joins two lines of work
Conflicta merge where both branches changed the same lines
Conflict markersthree kinds of line that Git writes into the file

The cells students fill in are left blank in this preview.

A merge is a fast-forward or merge commit

If main has not moved since you made your branch, Git just moves the main name forward to your branch's tip. That is a fast-forward. No new commit is made.

If both branches have new commits, Git makes a merge commit. It has two parents, one from each line of work. Git does this on its own when the changes touch different lines.

Make a shop with two opening hours

You will make a conflict on purpose, in a repository called shop. First save a file with the hours on main. Your name and address are set from lesson 1. If the lab was restarted, set them again.

Make a repository called shop

Command
git init -b main shop

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Make a file with the hours

Command
printf 'Mon to Sat 9 to 5\nSunday closed\n' > shop/hours.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Stage the file

Command
git -C shop add hours.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Save the first commit

Command
git -C shop commit -m "Add the hours"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Both branches change the same line

Now change the first line on a branch, and change the same line a different way on main. Each change is a fair edit. Together they clash.

The command sed -i replaces text in a file. Here it changes 9 to 5 into new hours.

Make a branch called longer-day

Command
git -C shop switch -c longer-day

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Close at 8 on the branch

Command
sed -i 's/9 to 5/9 to 8/' shop/hours.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Commit it on the branch

Command
git -C shop commit -am "Stay open until 8"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Go back to main

Command
git -C shop switch main

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Open at 8 on main

Command
sed -i 's/9 to 5/8 to 5/' shop/hours.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Commit it on main

Command
git -C shop commit -am "Open at 8"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Merge the branch into main

Command
git -C shop merge longer-day

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read what Git wrote in the file

Command
cat shop/hours.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Three marker lines show the two versions

Git wrote both versions into the file. The line <<<<<<< HEAD starts the version from the branch you are on. The line ======= ends it. The line >>>>>>> longer-day ends the other version.

To settle the conflict, edit the file to the text you want and delete the three marker lines. Then run git add on the file and git commit. To give up instead, run git merge --abort.

Write the settled text

Command
printf 'Mon to Sat 8 to 8\nSunday closed\n' > shop/hours.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Tell Git the file is settled

Command
git -C shop add hours.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Finish the merge with its own message

Command
git -C shop commit --no-edit

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Draw the history as a graph

Command
git -C shop --no-pager log --graph --format=%s

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a conflict means the repository is broken, or that someone did something wrong. It means two people changed the same lines. Nothing is lost. Choose the right text, or run git merge --abort to go back to before the merge.

About this lab and its source: Git and Gitea lab: learn clone, commit and push on a Git server of your own

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.