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
- Check
git --versionand thatgit config user.nameandgit config user.emailare set. - Work in a new folder:
mkdir ~/git-lab1 && cd ~/git-lab1.
Tasks
Tick a task when it is done. Only your browser remembers it.
- Make a repository with the branch
mainand a first commit of the filenginx.confwith two lines:worker_processes 2;andkeepalive_timeout 65;. - Make the branch
tune-workers, change2to4, commit, and merge it intomain. Which kind of merge was it? - Make the branch
tune-timeoutfrommain. On it, change the timeout to30and commit. - Go back to
main, change the same line tokeepalive_timeout 15;and commit (the other admin). - Merge
tune-timeoutintomain. Read the conflict markers and solve the conflict with the value20. - Finish the merge, then show the history as a graph. Delete the merged branches.
- Write down 4 branch protection rules you would set on GitHub for
main.
Check your work
- The graph shows a merge commit with two parents.
nginx.confhasworker_processes 4;andkeepalive_timeout 20;and no<<<<<<<markers.git branchshows onlymain;git statussays nothing to commit, working tree clean.
Hints
Show the hints
git init -b mainmakes the first branchmain.git switch -c NAMEmakes a branch and switches to it.- When
mainhas no new commits, Git only moves the pointer: a fast-forward merge. - In a conflict, the part between
<<<<<<< HEADand=======is yours (main), the part up to>>>>>>>is theirs. - After you fix the file:
git addit andgit commit(orgit merge --continue).
Solution
Show the solution
- First commit.
- 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-forwardIt is a fast-forward:
mainhad no new commits, so Git only movedmainforward. - The first admin.
- The second admin on
main. - 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-timeoutFix the file: keep one line,
keepalive_timeout 20;, and delete the three marker lines. - History and clean-up.
- Protection rules for
mainon 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.