Building it · lesson 4 of 10
Ideas in this lesson
Agit switch -c namemake a branch and move onto it
Bgit merge namebring a branch’s commits into this one
C<<<<<<< ======= >>>>>>>a conflict’s two sides, marked in the file

Branches and merging

Last updated

Marco is adding the CANCEL word on a branch. You're adding the priority lane's limit on another. Both of you add a line in the same place in queue_rules.py. In this lesson you'll make a branch, merge it, and resolve the conflict that two people's changes to the same lines cause, keeping both people's work.

By the end of this lesson you can

  • Explain what a branch is, and why teams build features on branches
  • Use git switch, branch and merge
  • Tell fast-forward and three-way merges apart
  • Resolve a merge conflict, keeping both sides' intent

A branch is a label

A branch is a separate line of work. In Git it's just a movable label on a commit. main is a branch too. When you commit on a branch, its label moves to the new commit; the other branches stay where they were.

Teams use feature branches: each task is built on its own branch, and merged into main when it's done and reviewed. main stays working while half-finished work lives elsewhere.

Branch commands
CommandWhat it does
git branchLists the branches; * marks the one you’re on
git switch -c priority-runMakes a branch here and moves onto it
git switch mainMoves back to main; the folder changes to match
git merge priority-runBrings priority-run’s commits into the branch you’re on

Keep branches short-lived, a day or two. The longer a branch lives apart from main, the more both change, and the harder the merge. Simple Git's What a branch is, Making and switching and Branching models go further.

Fast-forward and three-way merges

There are two kinds of merge:

  • Fast-forward. If main hasn't moved since the branch started, there's nothing to combine: Git just moves the main label forward to the branch's last commit.
  • Three-way merge. If both have moved on, Git compares three snapshots: the commit where they split (the merge base) and the two tips. Lines changed on only one side are taken from that side. The result is a new merge commit with two parents.

If both sides changed the same lines differently, Git can't know which is right. That's a merge conflict, and a person decides.

Branch, merge, and a conflict

Marco's branch cancel-word adds a line right under DAILY_LIMIT = 60. You'll add the priority lane's limit on your own branch, in the same place: PRIORITY_RUN = 2, the most priority calls in a row (lesson 3.P). Type edit queue_rules.py to change the file.

Git sandbox
  1. Make a branch priority-run and move onto it
  2. Add PRIORITY_RUN = 2 on the line under DAILY_LIMIT = 60, and commit it
  3. Switch to main and merge priority-run (a fast-forward)
  4. Now merge Marco’s cancel-word, and see the conflict
  5. Resolve it so both lines stay, with no markers left, and finish the merge

Working folder

no changes

Staging area

nothing staged

Repository

2 commits on main

You are in ~/pila on main. Marco’s branch cancel-word adds a line under DAILY_LIMIT in queue_rules.py; it isn’t merged yet.

Commit graph

  1. fd2ae02 cancel-word Name the word that cancels a number
  2. 6c3d6f9 HEAD →main Give out numbers, one per phone, up to 60 a day
  3. 389ecdd Start Pila with a README

To finish, save the file, git add queue_rules.py to mark it settled, then git commit -m "Merge cancel-word" (or git commit --no-edit).

Reading the markers

In a conflict, Git writes both versions into the file between markers:

DAILY_LIMIT = 60
<<<<<<< HEAD
PRIORITY_RUN = 2
=======
CANCEL_WORD = "CANCEL"
>>>>>>> cancel-word
  • Between <<<<<<< HEAD and =======: your side, the branch you're on.
  • Between ======= and >>>>>>> cancel-word: the side being merged in.

Resolving means editing the file to what it should say, which is often both, and deleting the three marker lines. Here both constants are needed, so both lines stay. Sometimes the two changes really disagree (Marco set the limit to 50, you to 60); then you talk to the person, or check the requirement, before choosing. Simple Git's Merging and Merge conflicts show more ways to resolve, abort and check a merge.

Spot the trap: deleting the other half

Spot the trapMarco

It’s late, and you hit a conflict in queue_rules.py between your branch and Marco’s. His half is twelve lines you don’t understand.

What happens if…

Check what you learned

main hasn’t changed since you made your branch. What kind of merge brings your branch in?
When does Git report a merge conflict?
In the conflict, what goes between ======= and >>>>>>> cancel-word?

Sources

Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.

All sources for Simple Software Development