Show contents
Chapter 5 of 6 · 25 min
Project: a team's contribution
By the end of this chapter, you'll be able to
- Simulate a team with a remote repository and two clones
- Bring a branch up to date with main and resolve a conflict
- Carry a contribution through to the merge and the branch cleanup
- Write a pull request template and a review checklist
Setting up the team
A bare repository is a repository without a working folder: exactly what GitHub or GitLab keep on their servers. We create one in a local folder to play the server, then two folders for Awa and Ibrahim:
git init -q --bare server.git
mkdir awa
cd awa
git init -q
git remote add origin ../server.git
printf "# Sahel Cooperative\n\nOpening hours: 8 am - 5 pm\n" > README.md
git add README.md
git commit -q -m "docs: add the README"
git push -q -u origin main
git clone -q ../server.git ../ibrahim
git -C ../ibrahim log --oneline
f6c7747 docs: add the README
git -C ../ibrahim runs the command in Ibrahim's folder without moving there. His clone does contain Awa's first commit.
Two branches, one line
Each of them creates a branch and pushes their work. Awa first:
cd awa
git switch -q -c docs/weekday-hours
printf "# Sahel Cooperative\n\nOpening hours: 8 am - 6 pm\n" > README.md
git commit -q -am "docs: extend the weekday hours"
git push -q -u origin docs/weekday-hours
Then Ibrahim, whose pull request is approved and merged into main first. Here, we do the merge on the command line, as the hosting service's button would:
cd ibrahim
git switch -q -c docs/saturday
printf "# Sahel Cooperative\n\nOpening hours: 8 am - 5 pm, Saturday 9 am - 12 pm\n" > README.md
git commit -q -am "docs: add the Saturday hours"
git switch -q main
git merge -q docs/saturday
git push -q origin main
The conflict
Awa's pull request now says "this branch has conflicts". Awa fetches the new version of main and merges it into her branch:
cd awa
git fetch -q origin
git merge origin/main
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.
cd awa
cat README.md
git status --short
# Sahel Cooperative
<<<<<<< HEAD
Opening hours: 8 am - 6 pm
=======
Opening hours: 8 am - 5 pm, Saturday 9 am - 12 pm
>>>>>>> origin/main
UU README.md
Between <<<<<<< HEAD and ======= is her branch's version; between ======= and >>>>>>> origin/main, the version from main. UU means the file was changed on both sides and is not resolved yet. Git does not choose: it is up to Awa to decide. Here, both changes are wanted, so the right version keeps both.
The pushed branch contains main: the pull request no longer has a conflict.
Text description of the animation
- We get the server's state without touching our files.
- Both branches changed the same line: Git stops and marks the file.
- We write the final version combining both people's intentions, then remove the markers.
- git add marks the conflict as resolved; the commit completes the merge.
- The pushed branch contains main: the pull request no longer has a conflict.
cd awa
printf "# Sahel Cooperative\n\nOpening hours: 8 am - 6 pm, Saturday 9 am - 12 pm\n" > README.md
git add README.md
git commit -q --no-edit
git push -q
git log --oneline --graph
* 5feb93b Merge remote-tracking branch 'origin/main' into docs/weekday-hours
|\
| * f11455b docs: add the Saturday hours
* | e710255 docs: extend the weekday hours
|/
* f6c7747 docs: add the README
Merging and cleaning up
The pull request is reviewed and approved. We merge it, delete the branch (locally and on the server), and Ibrahim fetches the result:
cd awa
git switch -q main
git pull -q
git merge -q docs/weekday-hours
git push -q origin main
git branch -q -d docs/weekday-hours
git push -q origin --delete docs/weekday-hours
cd ../ibrahim
git pull -q
cat README.md
# Sahel Cooperative
Opening hours: 8 am - 6 pm, Saturday 9 am - 12 pm
Both contributions are in main, and everyone has the same history.
The team's tools
A pull request template pre-fills every new description. On GitHub, it goes in .github/pull_request_template.md (on GitLab, in .gitlab/merge_request_templates/):
## Why
<!-- The problem solved, the linked ticket -->
## What changes
-
## How to check
1.
## Checklist
- [ ] Tests pass and cover the change
- [ ] Documentation is up to date
- [ ] No secret (password, key) in the code
- [ ] Screenshots attached if the interface changes
And a review checklist, shared in the team's README, reminds everyone what to look at: does the code do what the description says? Are edge cases (empty list, missing value) handled? Are names clear? Are errors handled? Could sensitive data leak?
Your turn
Worked exercise
Project: your own team contribution
In the same working folder, replay a complete contribution with a new file, prices.txt:
- Create a new bare repository
shop.gitand two clones,meiandkofi. Mei addsprices.txtcontaining "Mango: 300 F CFA" and pushes it. - Kofi fetches the repository, creates
fix/mango-price, changes the price to 350 F CFA, and his branch is merged intomain. - Meanwhile, Mei creates
feat/pineappleand, on the same line, writes "Mango: 300 F CFA, pineapple: 500 F CFA". - Mei brings her branch up to date with
main, resolves the conflict by keeping the new mango price and the pineapple, then merges. - Write the description of Mei's pull request with the template above.
Hint 1
git init --bare, git init + git remote add, then git clone.Hint 2
git merge origin/main in Mei's folder, after a git fetch.Show the solution and common mistakes
git init -q --bare shop.git
mkdir mei
cd mei
git init -q
git remote add origin ../shop.git
echo "Mango: 300 F CFA" > prices.txt
git add prices.txt
git commit -q -m "feat: add the prices"
git push -q -u origin main
git clone -q ../shop.git ../kofi
cd ../kofi
git switch -q -c fix/mango-price
echo "Mango: 350 F CFA" > prices.txt
git commit -q -am "fix: update the mango price"
git switch -q main
git merge -q fix/mango-price
git push -q origin main
cd ../mei
git switch -q -c feat/pineapple
echo "Mango: 300 F CFA, pineapple: 500 F CFA" > prices.txt
git commit -q -am "feat: add the pineapple price"
git fetch -q origin
git merge -q origin/main || echo "conflict to resolve"
echo "Mango: 350 F CFA, pineapple: 500 F CFA" > prices.txt
git add prices.txt
git commit -q --no-edit
git switch -q main
git pull -q
git merge -q feat/pineapple
git push -q origin main
cat prices.txt
Auto-merging prices.txt
CONFLICT (content): Merge conflict in prices.txt
Automatic merge failed; fix conflicts and then commit the result.
conflict to resolve
Mango: 350 F CFA, pineapple: 500 F CFA
git merge -q stays silent, except to announce a conflict; the || echo lets the script carry on so the resolution can follow. In real life, you would open the file in your editor at that point.
Pull request description:
## Why
Customers are asking for the price of pineapples.
## What changes
- pineapple added (500 F CFA) to prices.txt;
- conflict resolved with fix/mango-price: the new mango price (350 F CFA) is kept.
## How to check
1. Open prices.txt: mango is at 350 F CFA and pineapple at 500 F CFA.
Common mistakes:
- Keeping "Mango: 300 F CFA" during the resolution: Kofi's fix is lost without anyone noticing.
- Forgetting
git addafter editing the conflicting file:git commitrefuses with "Committing is not possible because you have unmerged files". - Merging
feat/pineappleintomainwithout first updating the branch: the conflict then appears inmain, in front of the whole team.
Summary
- 1A bare repository and two clones are enough to simulate a team and its server.
- 2Before merging, bring your branch up to date with main and resolve conflicts on your side.
- 3Resolving a conflict means combining both sides' intentions, then rerunning the tests.
- 4After the merge, delete the branch locally and on the server.
- 5A pull request template and a checklist make reviews regular and thorough.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.