Advanced
Searching inLabs
How to search
  • Search a task or a tool (lvm, selinux, @podman); filter by level and by book or course.
  • With an empty search box, a filter alone lists the labs — for example all hard labs of one course.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Lab 3.1Git, Chapter 3

Branches, a real merge conflict and a protected main

Level
Easy
30 min
Machines
any computer with Git 2.30 or newer
Status
Not started

Goal

Two admins change the same config file at the same time. You play both of them: make a feature branch, make a fast-forward merge and a real three-way merge, cause a merge conflict on purpose, solve it by hand and finish the merge with a clean history. At the end you write the rules that would protect main on GitHub.

Before you start

Tasks

  1. Make a repository with the branch main and a first commit of the file nginx.conf with two lines: worker_processes 2; and keepalive_timeout 65;.
  2. Make the branch tune-workers, change 2 to 4, commit, and merge it into main. Which kind of merge was it?
  3. Make the branch tune-timeout from main. On it, change the timeout to 30 and commit.
  4. Go back to main, change the same line to keepalive_timeout 15; and commit (the other admin).
  5. Merge tune-timeout into main. Read the conflict markers and solve the conflict with the value 20.
  6. Finish the merge, then show the history as a graph. Delete the merged branches.
  7. Write down 4 branch protection rules you would set on GitHub for main.

Check your work

git log --oneline --graph --all
cat nginx.conf
git branch
git status
  • The graph shows a merge commit with two parents.
  • nginx.conf has worker_processes 4; and keepalive_timeout 20; and no <<<<<<< markers.
  • git branch shows only main; git status says nothing to commit, working tree clean.

Hints

Show the hints
  • git init -b main makes the first branch main.
  • git switch -c NAME makes a branch and switches to it.
  • When main has no new commits, Git only moves the pointer: a fast-forward merge.
  • In a conflict, the part between <<<<<<< HEAD and ======= is yours (main), the part up to >>>>>>> is theirs.
  • After you fix the file: git add it and git commit (or git merge --continue).

Solution

Show the solution
  1. First commit.
    git init -b main
    printf 'worker_processes 2;\nkeepalive_timeout 65;\n' > nginx.conf
    git add nginx.conf
    git commit -m "Add nginx config"
  2. A fast-forward merge.
    git switch -c tune-workers
    sed -i 's/worker_processes 2;/worker_processes 4;/' nginx.conf
    git commit -am "Use 4 workers"
    git switch main
    git merge tune-workers
    Updating 1a2b3c4..5d6e7f8
    Fast-forward

    It is a fast-forward: main had no new commits, so Git only moved main forward.

  3. The first admin.
    git switch -c tune-timeout
    sed -i 's/keepalive_timeout 65;/keepalive_timeout 30;/' nginx.conf
    git commit -am "Timeout 30s"
  4. The second admin on main.
    git switch main
    sed -i 's/keepalive_timeout 65;/keepalive_timeout 15;/' nginx.conf
    git commit -am "Timeout 15s"
  5. Merge and see the conflict.
    git merge tune-timeout
    CONFLICT (content): Merge conflict in nginx.conf
    Automatic merge failed; fix conflicts and then commit the result.
    cat nginx.conf
    worker_processes 4;
    <<<<<<< HEAD
    keepalive_timeout 15;
    =======
    keepalive_timeout 30;
    >>>>>>> tune-timeout

    Fix the file: keep one line, keepalive_timeout 20;, and delete the three marker lines.

    printf 'worker_processes 4;\nkeepalive_timeout 20;\n' > nginx.conf
    git add nginx.conf
    git commit -m "Merge tune-timeout: agree on 20s"
  6. History and clean-up.
    git log --oneline --graph --all
    git branch -d tune-workers tune-timeout
  7. Protection rules for main on GitHub (Settings → Rules → Rulesets): require a pull request before merging with at least 1 approval; require status checks (CI) to pass; block force pushes and deletion; require signed commits (and optionally a linear history).

My notes

No notes yet.

Lessons for this lab

Commands in this lab