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.
| Command | What it does |
|---|---|
git branch | Lists the branches; * marks the one you’re on |
git switch -c priority-run | Makes a branch here and moves onto it |
git switch main | Moves back to main; the folder changes to match |
git merge priority-run | Brings 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
mainhasn't moved since the branch started, there's nothing to combine: Git just moves themainlabel 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.
- Make a branch
priority-runand move onto it - Add
PRIORITY_RUN = 2on the line underDAILY_LIMIT = 60, and commit it - Switch to main and merge
priority-run(a fast-forward) - Now merge Marco’s
cancel-word, and see the conflict - 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
fd2ae02cancel-word Name the word that cancels a number6c3d6f9HEAD →main Give out numbers, one per phone, up to 60 a day389ecddStart 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
<<<<<<< HEADand=======: 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
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
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.
- Pro Git, 3.2: Basic Branching and Merging: branches, fast-forward and three-way merges, and conflicts
- Git documentation: git-merge: how merges and conflict markers work
- Martin Fowler: Patterns for Managing Source Code Branches: feature branches, and why they should be short-lived