CS210 · Block 3 · Lesson 24

Git in Depth I: Branching, Merging, Conflict Resolution

Branches · HEAD · Fast-forward vs three-way merge · Conflict markers · Resolve, stage, commit
At a glance: You have used Git as one straight line of commits. Today history learns to fork and rejoin. You will create branches, merge them, and resolve your first merge conflict — the skill the team project depends on. Two guided walkthroughs in Section 5 take you through branching and conflict resolution step by step; the lab has you resolve a real conflict for a grade. No C code today: the graded skill is the Git workflow itself.
Contents
  1. Lesson Objectives
  2. Assigned Readings
  3. Pre-Class Work
  4. Lesson Materials and Overview
  5. In-Class Work
  6. After Class
1. Lesson Objectives

By the end of this lesson, cadets will be able to:

  1. Create, switch between, and delete Git branches. (Outcomes 6, 7)
  2. Distinguish a fast-forward merge from a three-way merge by reading the commit graph. (Outcome 6)
  3. Resolve a merge conflict by editing the conflict markers and completing the merge commit. (Outcome 6)
  4. Use git status and git log to stay oriented, and apply branching strategies appropriate to a project's complexity. (Outcomes 6, 7)
2. Assigned Readings

Complete before class. These set the vocabulary we will use.

Source Sections Why
Beej-Git Ch. 5-6 (Branches and Fast-Forward Merges, Merging and Conflicts) The exact mechanics we practice in class: branches, both kinds of merge, and conflict resolution

If branches and merges are brand new to you, read both chapters closely; they map one-to-one onto the two walkthroughs in Section 5.

3. Pre-Class Work
Do this first
Plan on 30 to 40 minutes for the reading and a quick repo check. Most of the learning happens during the in-class walkthroughs; come ready to type Git commands.

Pre-Class Checklist

1
Read Beej-Git Ch. 5-6
Come in knowing what these words mean, even if loosely:
  • branch and HEAD
  • git switch, git branch, git merge
  • fast-forward vs three-way merge
  • merge conflict and conflict markers (<<<<<<<, =======, >>>>>>>)
2
Confirm Git works in your terminal
Open your terminal and confirm Git responds. You have used it since Lesson 1; this is just a sanity check.
Run:
$ git --version
$ git status
If Git is missing or misconfigured, schedule EI before class so setup problems do not block today's lab.
3
Skim the In-Class section below
You will see two interactive Git walkthrough widgets. No need to use them yet; just know they are there and what they look like.
4. Lesson Materials and Overview

Today Git stops being a straight line. The reference below covers the concepts you will see in lecture and practice in the two walkthroughs. Read it before class or use it as a reference during the lab.

Jump to Branches & HEAD Two kinds of merge Conflict markers Branching strategy

Slide Deck

The Lesson 24 deck covers branches and HEAD, the two kinds of merge, reading conflict markers (including how your editor shows them), the resolution workflow, branching strategy, and a look ahead to Lesson 25. The slides are the spine of the in-class lecture; the two walkthroughs are where you practice hands-on.

Open the Lesson 24 slide deck →

Branches and HEAD

A commit is a snapshot of your files. A branch is nothing more than a lightweight, movable label that points at one commit. HEAD is the special label meaning “where you are right now.” When you are “on main,” that literally means HEAD points at the main label.

Creating a branch does not copy your files. Git just writes a tiny new label. That is why branches are cheap and used freely. The four commands you need today:

$ git branch # list branches; * marks where you are
$ git switch -c my-order # create a branch AND move onto it
$ git switch main # move back to an existing branch
$ git branch -d my-order # delete a merged branch (just the label)

We use git switch, the modern command, rather than the older git checkout. Both do the job; switch states the intent more clearly.

Two kinds of merge

When you merge a branch in, Git does one of two things depending on a single question: did the branch I am merging into move since the two branches split?

  • Fast-forward. The target branch did not move, so Git simply slides its label forward to the branch tip. No new commit, no conflict possible, history stays a straight line.
  • Three-way (a real merge). Both branches moved from a common ancestor, so Git builds a new merge commit with two parents to tie them together. If both sides changed the same region of a file, you get a conflict.

Conflicts only happen in three-way merges, and only when the two sides touched the same lines. A conflict is not an error you caused wrongly; it is Git asking you to make a decision it cannot make for you.

Reading conflict markers

When an auto-merge fails, Git rewrites the contested region of the file with three marker lines. Read them like this:

<<<<<<< HEAD ← start of YOUR version (the branch you are on)
Garcia: vanilla latte, oat milk
======= ← the fence between the two versions
Chen: caramel macchiato, extra shot
>>>>>>> roommate-edits ← end of THEIR version (the incoming branch)

The top section (between <<<<<<< HEAD and =======) is yours. The bottom section (between ======= and >>>>>>>) is theirs. Your editor shows the same thing with colored “Current Change” and “Incoming Change” buttons; those buttons just edit these exact marker lines for you.

The single most common mistake: leaving marker lines in the file. The markers are not data. To resolve a conflict you delete all three marker lines and leave the file reading the way it should. Then the four-step workflow:

$ git merge roommate-edits # 1. triggers the conflict
(edit the file: fix it, delete the markers) # 2.
$ git add coffee_run.txt # 3. mark it resolved (most-forgotten step!)
$ git commit # 4. seal the merge commit

Lost in the middle of a merge? git merge --abort resets everything to before the merge. Nothing is permanent until step 4.

When should you branch?

Branches are cheap, but a tangle of them is not free. Match the strategy to the work: commit small solo changes straight to main; branch anything risky or experimental so a failure leaves main untouched; and on a team, use one branch per feature or per person and merge back often. The biggest lesson from past team projects: small, frequent merges keep every conflict tiny, while saving one giant merge for the end guarantees pain. You will live this in the team project soon.

5. In-Class Work

Three things to do in class. Use the checklist to track your progress.

  • Work through the Branch & Merge walkthrough below. Fourteen guided tasks: create a branch, commit on it, watch the graph fork, fast-forward merge, and clean up. About 15 minutes. Screenshot the completion code for the reflection.
  • Work through the Merge Conflict walkthrough below. Thirteen guided tasks: trigger a conflict, read the markers, edit the file, stage, and commit. Take it slowly. About 15 minutes. Screenshot this completion code too.
  • Complete Lab 24: Resolve a Merge Conflict. Auto-graded through Gradescope. Resolve a real conflict in a two-branch repo. Full instructions in the lab repo.

Widget 1: Branch & Merge Walkthrough

Fourteen tasks across five sections. Type each Git command at the prompt. The left pane is your terminal; the right pane draws the commit graph as it changes.

Before you begin

Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 24 reflection.

CS210 Lesson 24 · Branch & Merge Walkthrough

A guided tour through branching, switching, committing on a branch, and a fast-forward merge. Type each git command on the input line. The left pane is your terminal; the right pane is the commit graph, the same picture git log --oneline --graph --all draws.
Task 0 of 14
Section 1: Orient yourself
Current task
Loading...
Commit graph
on branch main
commit branch label HEAD
~/coffee-run (main)$
How this works: This is not a real shell. It is a teaching tool that simulates the git commands in this lesson so the commit graph on the right stays predictable. The task card tells you exactly what to type next; only the expected command advances the walkthrough. Mistype twice and the hint button pulses. The prompt on the input line shows your current branch in parentheses, exactly like a real configured shell.

Widget 2: Merge Conflict Walkthrough

Thirteen tasks across five sections. You will trigger a real conflict, read the markers in the middle pane, fix the file, and finish the merge. This is the part that trips people up the first time, so the widget narrates every step.

Before you begin

Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Lesson 24 reflection.

CS210 Lesson 24 · Merge Conflict Walkthrough

This is the part that trips everyone up the first time. Take it slowly. You and a teammate both edited the same line of coffee_run.txt. You will trigger the conflict on purpose, read what git wrote into the file, fix it by hand, and finish the merge. The middle pane is the actual file; watch it change as you go.
Task 0 of 13
Section 1: See the setup
Current task
Loading...
coffee_run.txt
Branches
Edit coffee_run.txt to resolve the conflict
~/coffee-run (main)$
If you ever feel lost: a half-finished merge is not dangerous. Running git merge --abort in your real terminal throws the whole merge away and puts the file back exactly as it was, so you can start over. Nothing you do during a conflict is permanent until you commit. This simulated shell understands the commands the task asks for; follow the card and you cannot break anything.
6. After Class
  • Finish Lab 24 if you did not complete it in class. Submit through Gradescope.
  • Submit both widget completion codes on the Lesson 24 reflection, with a screenshot of each completion screen.
  • Read Beej-Git Ch. 7 and beyond, plus the code-review notes, for Lesson 25 (collaboration, remotes, pull requests, code review).
  • Looking ahead: Lesson 25 takes today's solo-machine branching and merging to a whole team. The conflict skills you built today carry over unchanged.
Need help? Schedule EI with your instructor. Bring the exact Git message or output you do not understand. For common Git problems (rejected pushes, detached HEAD, undoing a merge), see the one-page Git troubleshooting card.