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
| Word | What it means |
|---|---|
| Merge | to bring the work of one branch into another |
| Fast-forward | a merge that only moves a branch name forward |
| Merge commit | a commit with two parents that joins two lines of work |
| Conflict | a merge where both branches changed the same lines |
| Conflict markers | three 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
git init -b main shopShown 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
printf 'Mon to Sat 9 to 5\nSunday closed\n' > shop/hours.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Stage the file
git -C shop add hours.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Save the first commit
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
git -C shop switch -c longer-dayShown 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
sed -i 's/9 to 5/9 to 8/' shop/hours.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Commit it on the branch
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
git -C shop switch mainShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Open at 8 on main
sed -i 's/9 to 5/8 to 5/' shop/hours.txtShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Commit it on main
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
git -C shop merge longer-dayShown 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
cat shop/hours.txtShown 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
printf 'Mon to Sat 8 to 8\nSunday closed\n' > shop/hours.txtShown 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
git -C shop add hours.txtShown 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
git -C shop commit --no-editShown 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
git -C shop --no-pager log --graph --format=%sShown 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.