Git without fear · Chapter 10 of 28
Revert, don't reset
By the end of this chapter you can undo a commit that is already on GitHub without rewriting anyone's history, and tell when reset is the better tool. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Undo
- 4 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
Ravi calls from the counter: "The app says masala chai costs zero. Customers are very happy. Meera is not." This morning Bunty tested a discount idea, committed "Make every chai free" and pushed it. Meera pushed her monsoon special on top at lunch, and Ravi has already pulled both. Bunty also has one more commit, "Add console.log everywhere", that never left his laptop.
Last chapter's tool, git reset, takes commits off a branch. Bunty's plan is git reset --hard HEAD~3 and a push. That throws away Meera's monsoon special with his own commits, GitHub refuses the push, and getting past that needs a force push that leaves Ravi with a history GitHub says never happened. A shared commit needs an undo that adds to the history instead.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Bring the real prices back on GitHub's main
Bunty's "Make every chai free" set all prices to 0, and it is on GitHub. Bring the real prices back on GitHub.
1. Run git log --oneline and git log --oneline origin/main. See which commits GitHub has.
2. Undo the free chai commit with a new commit (task 2).
3. Push it to GitHub with a plain git push.
To pass: prices.csv on GitHub's main (origin) has masala-chai,40, cutting-chai,25 and bun-maska,50 again.
- Task 2
Undo the free chai commit with a revert
Undo "Make every chai free" with git revert. A revert makes a new commit, and the history stays as it is.
1. First remove the unpushed console.log commit (task 4).
2. Run git revert on "Make every chai free". Give it the commit's hash, or its place below HEAD.
3. Keep the message Git writes. --no-edit keeps it without opening the editor.
4. Push with a plain git push.
To pass: GitHub's main has a commit made by git revert, with Git's own message Revert "Make every chai free". GitHub's main still has every old commit, including "Add the monsoon special".
- Task 3
Never force-push main
main is shared, so never force it. A revert only adds a commit, so a plain push works.
1. Push with git push and nothing more.
2. If the push is rejected, do not force it. Run lab reset and start again.
To pass: none of your pushes used --force, -f, --force-with-lease or a + before the branch. This counts every push you typed, even one that failed. The newest commit on GitHub's main is your revert.
- Task 4 · interview question
Interview: reset away the unpushed commit
Interview question: when do you use reset, not revert? "Add console.log everywhere" was never pushed. So remove it with a reset.
1. Run git status. Your main is 1 commit ahead of origin/main.
2. Move main back to origin/main with git reset. Use the mode that also changes your files.
3. Do this first, before you revert or push anything.
To pass: "Add console.log everywhere" is not on your main and not on GitHub. orders.js in the chapter folder has no console.log. A revert is not enough: it keeps the commit in the history.
What you will learn
- git revert: a new commit that does the exact opposite of an old one.
- Shared or only yours: reading git status and origin/main to tell which commits other people already have.
- Revert what is shared, reset what is only yours: the interview rule, used on the same branch.
More in Undo
Build "Revert, don't reset" 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.