Goal: Learn how to undo mistakes in Git — safely and permanently — using VS Code.
Prerequisites: Complete Exercise 1 first.
| Tool | What it does | Safe to share? |
|---|---|---|
| Revert | Creates a new commit that undoes a previous one. History is kept. | ✅ Yes |
| Reset | Moves the branch pointer backwards. History is rewritten. |
Before committing, you can throw away edits to a file with a single click.
-
Open
pancakes.txtand add a bad line at the end:Add dishsoap for extra bubbles.Save the file.
-
In the Source Control panel, the file appears under Changes.
-
Hover over
pancakes.txtand click the ↺ (Discard Changes) icon. -
VS Code asks for confirmation. Click Discard Changes. The file goes back to exactly how it was at the last commit.
This is like
git checkout -- <file>. Use it when you want to throw away edits you haven't committed yet.
git revert creates a new commit that cancels out a previous one. Your history is preserved and it's safe for shared work.
-
Open
pancakes.txtand add:Add pineapple for extra flavor.Save, stage, and commit with the message
Add questionable ingredient. -
Open the history view: Command Palette (
Ctrl+Shift+P) → "Git: View History". -
Find the commit
Add questionable ingredient. Right-click on it and select "Revert Commit..." (or "Undo Commit" depending on VS Code version). -
VS Code creates a new commit that undoes the change. Check the history — you'll see both the original and the revert commit.
-
Open
pancakes.txtto confirm the pineapple line is gone.Revert is always the safe choice when you've already shared your commits with others.
Warning: Only use reset on commits that you haven't pushed or shared with anyone.
-
Add a temporary line to
pancakes.txt, save, stage, and commit with the messageAdd test content. -
Open the Command Palette and search for "Git: Undo Last Commit". VS Code performs a soft reset — the commit disappears but your changes stay staged in the Source Control panel, ready to recommit.
Use this when you committed too early and want to adjust the message or add more changes.
-
Commit the staged changes again (same content is still staged from the soft reset).
-
This time, in the terminal:
git reset HEAD~1
The commit is gone and the file is now in Changes (unstaged). You can edit further before staging again.
Danger: This permanently deletes the commit and all its changes.
-
Re-add the content and commit it one more time.
-
In the terminal:
git reset --hard HEAD~1
The commit and all changes are completely gone.
-
In the Source Control panel, notice the file no longer appears under Changes.
Tomorrow we'll resolve conflicts properly. For now, let's see what one looks like.
-
Create a branch called
spicy-versionfrom the status bar. -
Open
pancakes.txtand change the ingredients line to:Ingredients: flour, eggs, milk, butter, chili flakesStage and commit with the message
Add chili flakes to pancakes. -
Switch back to
mainvia the status bar. -
Edit the same line in
pancakes.txt:Ingredients: flour, eggs, milk, butter, vanilla extractStage and commit with the message
Add vanilla extract to pancakes. -
Command Palette → "Git: Merge Branch..." → select
spicy-version. -
VS Code shows a merge conflict warning and marks the file with a
Cbadge in the Source Control panel. Click the file to open the Merge Editor. -
VS Code shows you both versions side by side. For now, just pick one version by clicking "Accept Current Change" or "Accept Incoming Change".
-
Stage the resolved file and commit with the message
Resolve pancake ingredient conflict. -
Delete the
spicy-versionbranch: Command Palette → "Git: Delete Branch...".
- What is the difference between Discard Changes (Part A) and Revert Commit (Part B)?
- When would you use soft reset vs hard reset?
- Why is
git revertconsidered safe whilegit reset --hardis considered dangerous? - What do you think causes a merge conflict — when does Git get confused?