The Situation
You are Asha, working on a café's menu with your teammate Ravi through a shared repository. Before you started work this morning:
- You had pushed a branch,
add-cake, that adds cake to the menu. It is not onmainyet. - Ravi pushed two commits to
main. One, "Add hot chocolate", is wanted. The other, "Update tea price", set tea to 20.00 by mistake; the owner confirms it should stay at 2.00.
Your job is to deliver the cake without losing anyone's work.
This challenge uses skills from across the course: inspecting history, reverting a shared commit, resolving merge conflicts, pulling and pushing, and cleaning up branches. Try it without looking back first; the hints and the explained solution are below.
Exercise: Deliver the Fixed Feature
Starting state: Git configured as in the configuration lesson.
-
Run the setup. It creates the shared repository, your clone
youand Ravi's cloneteammate, and replays the morning so far. You end in your own clone:mkdir -p ~/git-practice/final-challenge cd ~/git-practice/final-challenge git init --bare -b main shared.git git clone shared.git you git clone shared.git teammate cd you git config user.name "Asha Rao" git config user.email "asha@example.com" cat > menu.md <<'EOF' # Menu Tea: 2.00 Coffee: 2.50 Juice: 3.00 EOF git add menu.md git commit -m "Add menu" git push -u origin main git switch -c add-cake cat >> menu.md <<'EOF' Cake: 3.50 EOF git commit -am "Add cake to the menu" git push -u origin add-cake git switch main cd ../teammate git config user.name "Ravi Kumar" git config user.email "ravi@example.com" git pull cat > menu.md <<'EOF' # Menu Tea: 20.00 Coffee: 2.50 Juice: 3.00 EOF git commit -am "Update tea price" cat >> menu.md <<'EOF' Hot chocolate: 3.00 EOF git commit -am "Add hot chocolate" git push cd ../you -
Complete the task. In your clone,
you:- Undo the tea price change on the shared
main, keeping the hot chocolate. - Bring
add-cakeup to date withmain. Where the cake and the hot chocolate conflict, keep both, with hot chocolate first. - Put the cake on the shared
main. - Delete
add-cakelocally and in the shared repository. - Never use
--forceor--force-with-lease: these branches are shared.
- Undo the tea price change on the shared
-
Check your result with these acceptance checks, run in
you:git log --oneline --graph git status cat menu.md git ls-remote --heads originAll of these must hold:
-
The log contains
Revert "Update tea price"and the merge that broughtmainintoadd-cake, and the cake commit is onmain. -
git statussays your branch is up to date withorigin/mainand the working tree is clean. -
menu.mdreads exactly:# Menu Tea: 2.00 Coffee: 2.50 Juice: 3.00 Hot chocolate: 3.00 Cake: 3.50 -
git ls-remote --heads originlists onlyrefs/heads/main. -
The shared history still contains Ravi's original "Update tea price" and "Add hot chocolate" commits, which proves nothing was rewritten.
-
-
Write down, in a sentence each, why you chose each step. The Check Your Understanding questions below ask for the same reasoning.
Hints, from gentle to specific. Read one at a time, only if you are stuck:
- Hint 1: Your
mainis behind. Start by getting Ravi's commits, then look at what the bad commit changed. - Hint 2: The bad commit is shared, so it must stay in the history. Which undo command adds a commit instead of removing one?
- Hint 3:
add-cakeis shared too. To bringmaininto it, merge rather than rebase. Expect a conflict at the end ofmenu.md. - Hint 4: Once
add-cakecontainsmain, merging it intomainis a fast-forward. Then delete the branch withgit branch -dandgit push origin --delete.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
The revert reports a CONFLICT | You reverted the wrong commit, or main was not up to date | git revert --abort, git pull, then check the commit with git show |
[rejected] when pushing main | The shared main has commits you lack | git pull, then push again |
menu.md has no hot chocolate after the merge | Ravi's line was dropped while resolving | Fix the file, then commit the correction |
git branch -d add-cake says is not fully merged | The cake is not on main yet | Merge add-cake into main first |
| You want to start again | Any reason | Remove ~/git-practice/final-challenge and rerun the setup |
-
Clean up when you have finished:
cd ~/git-practice ls rm -rf final-challenge ls
Check Your Understanding
Explain your decisions:
- Why revert "Update tea price" instead of resetting
mainback past it? - Why merge
mainintoadd-cakeinstead of rebasingadd-cakeontomain? - Ravi pulls after you finish. Will he need to fix anything? Why or why not?
Answer and Reasoning
Solution
In your clone, you:
git switch main
git pull
git log --oneline
git show HEAD~1
git revert --no-edit HEAD~1
git push
git switch add-cake
git merge main
cat > menu.md <<'EOF'
# Menu
Tea: 2.00
Coffee: 2.50
Juice: 3.00
Hot chocolate: 3.00
Cake: 3.50
EOF
git diff --check
git add menu.md
git merge --continue
git push
git switch main
git merge add-cake
git push
git branch -d add-cake
git push origin --delete add-cake
Step by step:
git pullfast-forwardsmainwith Ravi's two commits.git logandgit show HEAD~1confirm that "Update tea price" changedTea: 2.00toTea: 20.00.git revert --no-edit HEAD~1addsRevert "Update tea price", setting tea back to 2.00 while keeping "Add hot chocolate". Pushing it fixes the sharedmain.- Merging
mainintoadd-cakeconflicts at the end ofmenu.md: one side adds hot chocolate, the other cake. The resolution keeps both.git merge --continueopens your editor withMerge branch 'main' into add-cake; save and close it. Pushing updates the sharedadd-cake. - On
main,git merge add-cakefast-forwards, becauseadd-cakenow contains everything onmain. Pushing shares the cake, and the last two commands remove the finished branch everywhere.
Understanding check
- "Update tea price" is on the shared
main, and Ravi has it. Resettingmainpast it would rewrite shared history: the push would be rejected, and forcing it would also have thrown away "Add hot chocolate" or left Ravi's clone disagreeing with the shared repository. A revert undoes the change with a new commit, so everyone's history still agrees. add-cakewas already pushed. Rebasing would replace its commit with a new one, so pushing it would need a force. Merging adds to the branch, so a normal push works.- No. Every change you shared added to the history he already has, so his
git pullsimply fast-forwards to the newmain.
References
This challenge introduces no new commands. The sources for each command are listed in the lessons that teach it.