Git without fear · Chapter 19 of 28
Cherry-pick a hotfix
By the end of this chapter you can copy one commit from another branch onto yours, and leave a note in it saying where it came from. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Branches
- 3 tasks
- Git
- In the all labs plan
The first 2 chapters of Git without fear are free. This one comes with the all labs plan.
Why you need this
The till at Tapri runs the release-1.0 branch. This morning it billed a customer 52.5 rupees for a 50 rupee bun maska: GST at 5 percent, never rounded. Ravi has already fixed the rounding, but on develop, between Bunty's half-written monsoon menu and a loyalty card idea that says "maybe 5th??". Neither of those may reach the counter.
Bunty's plan is to merge develop into the release. A merge brings every commit develop has and the release does not: all three of them. Retyping Ravi's fix into bill.js by hand would work once, but the release would lose his name, his message, and any link to the commit it came from.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Copy Ravi's rounding fix to the release
Copy one commit from develop onto release-1.0: Ravi's rounding fix.
1. Run git log --oneline --graph --all. Find "Round the bill with GST to whole rupees" on develop.
2. Switch to release-1.0. A copied commit lands on the branch you are on.
3. Copy that one commit with git cherry-pick -x. Give it the commit's hash. Task 3 needs the -x.
To pass: release-1.0 has the commit "Round the bill with GST to whole rupees", bill.js on release-1.0 rounds the bill to whole rupees, and version.txt on release-1.0 still says 1.0.
- Task 2
Take nothing else from develop
Only the fix goes to the release. Nothing else from develop goes there.
1. Do not merge develop. A merge brings all three new commits.
2. Make the copy while you are on release-1.0. Do not change develop.
3. Run git log --oneline to see the commits on release-1.0.
To pass: release-1.0 has exactly four commits, newest first: the fix, Release 1.0, Add GST to the bill, Add the menu. It has no monsoon.txt and no loyalty.txt. develop keeps its five commits as they are.
- Task 3 · interview question
Interview: record where the fix came from
Interview question: how do you record where a copied commit came from?
1. Make the copy with git cherry-pick -x. The -x adds a line to the commit message.
2. Run git log -1 on release-1.0 and read the full message.
3. Its last line starts with cherry picked from commit.
To pass: the fix is the newest commit on release-1.0, and its message has that line with the full hash of Ravi's commit on develop. Picked without -x? Run lab reset and pick once, with -x.
What you will learn
- git cherry-pick: copies the change one commit made into a new commit on the branch you are on.
- Naming a commit by position: develop~1 is one commit before develop's last.
- -x: adds a line to the copy's message naming the commit it was picked from.
More in Branches
Build "Cherry-pick a hotfix" yourself
Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Git without fear are free. This one comes with the all labs plan.