Skip to content
KALANBLON
Show contents

Chapter 6 of 7 · 25 min

Project: a bakery's website, as a pair

By the end of this chapter, you'll be able to

  • Organise a team's work with one branch per task
  • Update a branch with git fetch and git merge
  • Resolve a conflict between the work of two people
  • Clean up merged branches, locally and on the server

The starting point

Awa creates the repository, the shared server, and pushes a first version of the site:

git init
echo "<h1>Kofi's Bakery</h1>" > index.html
echo "Home | Contact" > nav.txt
git add .
git commit -m "Create the website"
git init --bare ../server.git
git remote add origin ../server.git
git push -u origin main

git add . stages every file of the current folder. Kofi gets the project and introduces himself:

git clone ../server.git ../kofi
cd ../kofi
git config user.name "Kofi Mensah"
git config user.email "kofi@example.com"

The team's rule: nobody works directly on main. Every task has its branch, and a branch is merged only after review.

The branch is merged into main, then deleted locally and on the server.

Step 5 of 5
Text description of the animation
  1. Kofi creates a branch for his task, from an up-to-date main.
  2. He works and commits on his branch, without getting in anyone's way.
  3. Before asking for a review, he brings in the latest main. It is the right time to settle conflicts, on his side.
  4. He pushes his branch and opens a merge request. Awa reviews it.
  5. The branch is merged into main, then deleted locally and on the server.

Step 1: a branch each

Awa adds the product menu and puts it in the navigation bar:

git switch -c menu
echo "<h2>Breads, pastries and cakes</h2>" > menu.html
echo "Home | Menu | Contact" > nav.txt
git add .
git commit -m "Add the menu"
git push -u origin menu

At the same time, Kofi adds the opening hours, and changes the navigation bar too:

cd ../kofi
git switch -c hours
echo "<p>Monday to Saturday, 7am to 7pm</p>" > hours.html
echo "Home | Hours | Contact" > nav.txt
git add .
git commit -m "Add the opening hours page"
git push -u origin hours

Step 2: the menu is merged

Kofi reviews Awa's work on the platform and approves it. The menu branch is merged into main. Without a platform, we do the same thing by hand:

git switch main
git merge menu
git push
git log --format="%s" -1
Add the menu

Step 3: Kofi updates his branch

Before asking for a review, Kofi brings the latest main into his branch. git fetch downloads the server's commits without merging anything, then git merge origin/main merges the server's version:

cd ../kofi
git fetch
git merge origin/main
Auto-merging nav.txt
CONFLICT (content): Merge conflict in nav.txt
Automatic merge failed; fix conflicts and then commit the result.

Both changed the same line of the navigation bar. Kofi looks at the file:

cd ../kofi
cat nav.txt
<<<<<<< HEAD
Home | Hours | Contact
=======
Home | Menu | Contact
>>>>>>> origin/main

Neither version is right: we need both links. Kofi writes the final line, finishes the merge and pushes:

cd ../kofi
echo "Home | Menu | Hours | Contact" > nav.txt
git add nav.txt
git commit -m "Merge main into hours"
git push

That is the whole point of updating your branch: the conflict is resolved by the person who knows their change best, before the review.

Step 4: the opening hours are merged

Awa reviews the hours branch, which now contains the full navigation bar, and merges it:

git fetch
git merge origin/hours
git push
cat nav.txt
ls
Home | Menu | Hours | Contact
hours.html
index.html
menu.html
nav.txt

Kofi's branch already contained the whole of main: the merge is a fast-forward.

Step 5: clean-up

Merged branches are no longer needed. We delete them locally and on the server:

git branch -d menu
git push origin --delete menu hours
git log --format="%an: %s" -1
Kofi Mensah: Merge main into hours

Your turn

Mini-lab: two branches, one navigation barGit simulator

Terminal

Click “Run”: the commands run in a fresh repository. Type “help” for the list of commands.

Teaching simulator: it reproduces the essential Git commands in your browser, with nothing to install · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Replay the project without a server: the second merge fails on a conflict. Resolve it to get the four links, then check the history with git log --oneline.

Worked exercise

Worked exercise: the delivery page

Kofi must add a delivery.html page containing “Free delivery from 5,000 FCFA”. Replay his work in his repository following the team's rules: update main, create a delivery branch, commit, then (the review being done) merge into main and push. Finally, check that Awa gets the page.

Hint 1
All of Kofi's commands start with cd ../kofi in the same block: each block of this course starts again from Awa's folder.
Hint 2
Before creating the branch, git switch main then git pull get the up-to-date main from the server.
Show the solution and common mistakes
cd ../kofi
git switch main
git pull
git switch -c delivery
echo "Free delivery from 5,000 FCFA" > delivery.html
git add delivery.html
git commit -m "Add the delivery page"
git switch main
git merge delivery
git push
git branch -d delivery

Back in Awa's repository:

git pull
cat delivery.html
Free delivery from 5,000 FCFA

Common mistakes:

  • Creating the branch from an out-of-date main: the branch starts from an old version, and conflicts come later, at the worst moment.
  • Forgetting git push after the merge: the page only exists on Kofi's computer, and Awa gets nothing.

Summary

  1. 1In a team, every task has its branch, and main only receives reviewed work.
  2. 2git fetch downloads without merging; git merge origin/main then brings in the server's version.
  3. 3Updating your branch before the review lets you resolve conflicts on your side.
  4. 4A conflict is resolved by writing the final version, then git add and git commit.
  5. 5Merged branches are deleted locally (git branch -d) and on the server (git push origin --delete).

Your progress is saved on this device. Create a free account to keep your progress on all your devices.