Computing Foundations › Git and GitHub › Day 31

Day 31: Branching and Merging

Day 31 of 365 — Branching and Merging

After this lesson you will be able to create and switch branches, merge them, resolve a merge conflict calmly, and explain when to merge versus rebase — the everyday loop for working on anything in parallel without breaking the version that works.

Course
Computing Foundations
Category
Git and GitHub
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/computing-foundations/day-031-branching-and-merging

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/computing-foundations/day-031-branching-and-merging
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: what people assume a branch is compared with what it actually is


Historical background


What it is — and what it is not

A branch is:

It is not:


Why it was created and what problems it solves


How it works

The model

Diagram: a feature branch diverging from main and merging back at a merge commit

Creating and switching

Diagram: what happens to uncommitted work when switching branches

TaskModernClassic
List branchesgit branchgit branch
Create, stay putgit branch feature-xgit branch feature-x
Create and move onto itgit switch -c feature-xgit checkout -b feature-x
Move to an existing branchgit switch feature-xgit checkout feature-x
Merge into the current branchgit merge feature-xgit merge feature-x
Delete a merged branchgit branch -d feature-xgit branch -d feature-x

Fast-forward and three-way merges

Diagram: a fast-forward merge compared with a three-way merge

Conflicts

Diagram: how to read and resolve Git conflict markers

<<<<<<< HEAD
milk: 1 cup buttermilk
=======
milk: 2 cups
>>>>>>> feature

Flowchart: two edits to the same line become a conflict, then get resolved and committed

Rebase, and the one rule

Diagram: when rebasing is safe and when it is not

AspectMergeRebase
What it doesCombines with a merge commitReplays your commits elsewhere
History shapePreserves the branch structureRewrites into a straight line
Merge commit?Yes, for a three-wayNo
Original commits kept?YesNo — new ones replace them
Safe on shared history?AlwaysNever
Best forFinished work; anything publicTidying your own private branch

Branch hygiene

Diagram: how merge difficulty grows with branch age


An everyday analogy

Diagram: how cherry-pick copies a single commit

Two cooks adapting the same recipe.

The kitchenBranching
The original recipe cardmain
Your copy, being adaptedA feature branch
Their copy, also being adaptedAnother branch
Combining both sets of notesA merge
Both changed the same ingredientA conflict
Deciding which amount to useResolving it
Rewriting your notes as if you had started from theirsA rebase

Examples in practice

Diagram: what a merge conflict means and what it does not

Diagram: merge compared with rebase


Implications: security, privacy, performance, scalability, and cost

Diagram: why merge resolutions are a source of bugs


Alternatives: free, open source, and commercial

ApproachTypeWhere it fits
git mergeFree, built inCombining finished work; anything already shared
git rebaseFree, built inTidying your own private branch before sharing
git rebase -iFree, built inSquashing and reordering your own commits
git cherry-pickFree, built inTaking one commit from another branch
Trunk-based developmentA practiceVery short branches, merged daily; avoids drift entirely
Merge tools (meld, editor UIs)FreeThree-way conflict resolution, side by side

TermWhat it actually is
BranchA movable pointer to a commit
HEADWhich branch, and therefore which commit, you are on
Merge baseThe most recent common ancestor of two branches
Fast-forwardSliding a pointer forward; no merge commit needed
Three-way mergeCombining two tips relative to their base
ConflictBoth sides changed the same lines differently
RebaseReplaying commits elsewhere as new commits

When to use it — and when not to


The AI connection

Diagram: running experiments on one branch compared with one branch each


Knowledge check

quiz:
  question: "A developer rebases a feature branch that they had pushed two days earlier, then force-pushes it. A colleague who had already based work on that branch is now in difficulty. Why?"
  options:
    - "Rebasing replaced the shared commits with new ones, so the colleague's work is based on commits that no longer exist upstream"
    - "Force-pushing deleted the colleague's commits from the remote repository"
    - "Rebasing removed the merge base, so no future merge between the branches is possible"
    - "The colleague's local branch was automatically reset to match the rewritten history"
  answer_index: 0
  explanation: "A rebase does not move commits; it creates new ones with the same changes and different identities. The colleague's work still points at the originals, which are no longer on the branch upstream, so the two histories now disagree about what happened."

Hands-on exercise

Create a conflict deliberately and resolve it. Worked through fully in the Day 31 lab directory.

What you are doingCommand
Create and move onto a branchgit switch -c feature-x
Commit thereedit, git add, git commit
Return to main and commit differentlygit switch main, edit the same line, commit
Merge and hit the conflictgit merge feature-x
See the markersopen the file
Resolve and completeedit, git add <file>, git commit
Read the shapegit log --oneline --graph --all
Tidy upgit branch -d feature-x

Expected output

Auto-merging recipe.md
CONFLICT (content): Merge conflict in recipe.md
Automatic merge failed; fix conflicts and then commit the result.
*   9a3f1c2 Merge branch 'feature-x'
|\
| * 4b2e7d1 Use two cups of milk
* | 7c5a0f3 Use buttermilk
|/
* 1d0c8b4 Add recipe

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. In Git, what is a branch?

  1. A complete copy of every file in the project, stored in a new folder
  2. A permanent record that can never be deleted once created
  3. A backup of the repository kept on a separate server
  4. A movable pointer to a commit, naming the latest commit on a line of work
Show answer

Answer: D. A movable pointer to a commit, naming the latest commit on a line of work

A branch is just a small movable pointer to a commit — a few dozen bytes on disk — which is why creating one is instant and does not copy any files.

Q2. Which command creates a new branch AND moves you onto it in one step?

  1. git switch -c feature-x
  2. git merge feature-x
  3. git branch feature-x
  4. git log --graph feature-x
Show answer

Answer: A. git switch -c feature-x

git switch -c NAME creates the branch and switches to it. Plain git branch NAME creates the branch but leaves you where you are; the classic equivalent of switch -c is git checkout -b NAME.

Q3. When does Git perform a fast-forward merge instead of a three-way merge?

  1. Whenever the two branches changed the same lines
  2. The target has not advanced, so its pointer can slide forward
  3. Only when you explicitly pass the --no-ff option
  4. When the branch being merged has more commits than the target
Show answer

Answer: B. The target has not advanced, so its pointer can slide forward

If the target branch has not moved since the split, there is nothing to combine and Git just slides its pointer forward — a fast-forward, with no merge commit. A three-way merge happens when both branches advanced.

Q4. A merge conflict occurs when:

  1. You forget to write a commit message
  2. A branch has been left un-deleted for too long
  3. Two branches changed the same lines differently
  4. Two branches have the same name
Show answer

Answer: C. Two branches changed the same lines differently

Git combines non-overlapping changes automatically. It only stops and asks for a conflict resolution when both sides changed the same lines differently — it refuses to guess which version you meant.

Q5. In a conflict block, which part is the version from your current branch (HEAD)?

  1. The text below ======= and above >>>>>>>
  2. None of it — HEAD is never shown in a conflict
  3. The ======= line itself
  4. The text between <<<<<<< HEAD and =======
Show answer

Answer: D. The text between <<<<<<< HEAD and =======

The region between <<<<<<< HEAD and ======= is "ours" — the version on your current branch. The region between ======= and >>>>>>> is "theirs", from the branch being merged.

Q6. What must be true of a file before you commit it to resolve a conflict?

  1. No conflict markers may remain, and you must git add the file
  2. It must contain all three marker lines so Git can track the history
  3. It must be renamed to avoid the conflict
  4. It must be identical to the version on main
Show answer

Answer: A. No conflict markers may remain, and you must git add the file

Resolving means editing the file to the version you want, deleting all three marker lines, then staging it with git add and running git commit. Leftover markers would be committed as literal text.

Q7. How does rebasing differ from merging?

  1. Rebasing deletes the branch, while merging keeps it
  2. Rebasing replays your commits as new ones, keeping history linear
  3. Rebasing only works on the main branch
  4. There is no difference; they are two names for the same operation
Show answer

Answer: B. Rebasing replays your commits as new ones, keeping history linear

A merge combines branches with a two-parent merge commit and preserves the branch shape; a rebase replays your commits on top of another branch as new commits, rewriting them into a straight line with no merge commit.

Q8. What is the golden rule of rebasing?

  1. Always rebase before every commit
  2. Only rebase on Fridays
  3. Never rebase commits you have already shared with others
  4. Rebase only branches that have merge conflicts
Show answer

Answer: C. Never rebase commits you have already shared with others

Rebasing rewrites commits into new ones. Doing that to commits others have already based work on makes their history and yours disagree — so you rebase only your own private, un-shared work and merge anything public.

Glossary

branch
A separate line of development, represented in Git as a movable pointer to a commit; creating one is instant because it copies nothing, only writes a small pointer.
HEAD
A special reference marking the branch (and commit) you are currently on — "you are here." It moves to the new commit each time you commit.
checkout
The classic Git command for switching branches (git checkout NAME) or creating and switching (git checkout -b NAME); it also does other jobs, which is why git switch was later split out of it.
switch
The modern Git command (added in version 2.23) for moving between branches: git switch NAME to switch, git switch -c NAME to create and switch.
merge
Combining the commits of one branch into another so the target contains both lines of work; a three-way merge creates a new merge commit with two parents.
fast-forward
A merge in which the target branch has not advanced since the split, so Git simply slides its pointer forward to the other branch tip — no merge commit is created and history stays linear.
three-way merge
A merge that combines two diverged branches using their common ancestor (the merge base) plus both branch tips, producing a merge commit with two parents.
merge conflict
The situation where two branches changed the same lines differently, so Git stops the merge and asks you to choose, marking the disputed region with conflict markers.
rebase
Replaying a branch's commits on top of another branch as new commits, producing a straight linear history with no merge commit; it rewrites the commits, so it must never be used on shared history.
main
The conventional name of the default branch that holds the primary, shippable line of a project's history (older projects often call it master).
feature branch
A short-lived branch created to hold one distinct piece of work — a feature, fix, or experiment — kept isolated from main until it is ready to merge and then deleted.
merge base
The most recent commit two branches share — their common ancestor — which Git uses as the reference point when performing a three-way merge.
conflict markers
The <<<<<<<, =======, and >>>>>>> lines Git inserts around a conflicting region to show both competing versions; a conflict is resolved only once all three are removed.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.