Switching Contexts Mid-Work
Halfway through an edit, you need to check something on another branch. Git's real error message is informative — but scary the first time.
While spicy-experiments sat there with its one ghost pepper commit, real work continued on master too — a serving note was added to the cookie recipe. cookies.md now genuinely differs between the two branches, which is completely normal — but it sets up today's real interruption.\n\nYou start editing cookies.md on master to add a note about chili flakes, then realize: this note belongs on spicy-experiments, not here. You try to switch. Git says no — and for good reason. This lesson makes that real error message make sense, and practices the standard way out of it.
Triggering the Real Conflict
# edit cookies.md's Variations line, uncommitted
git checkout spicy-experiments
error: Your local changes to the following files would be overwritten by checkout:
cookies.md
Please commit your changes or stash them before you switch branches.
Aborting
This is real protection, not a bug: spicy-experiments' own committed version of cookies.md differs from your uncommitted edit, and checking out would silently discard one of them. Git refuses rather than guess.
Escaping With git stash
git stash
git checkout spicy-experiments
git stash pop
git stash genuinely removes the uncommitted edit and saves it separately, leaving the working directory clean enough to switch. git stash pop reapplies it — here, cleanly, since the edited line does not overlap with anything spicy-experiments changed independently.
Committing It Where It Belongs
git add cookies.md
git commit -m "Add chili flakes note to variations"
The edit now genuinely lives on the branch it was meant for all along.
Checkout conflict
Git's real refusal to switch branches when doing so would silently overwrite uncommitted changes that genuinely differ from the target branch's own version of the same file. Confirmed directly in this lab: the exact same edit on a file that does NOT differ between branches would not have triggered this.
git stash
Temporarily removes uncommitted changes from the working directory and saves them on a real internal stack, leaving a clean working tree. git stash pop reapplies the most recent stash and removes it from that stack — confirmed directly in this lab applying cleanly onto a different branch than the one it was stashed from.
✏️ Start an Edit on the Wrong Branch
On master, edit cookies.md's Variations line to mention chili flakes — then see it sitting there, uncommitted.
cd ~/recipes && sed -i 's/add walnuts, or swap half the chocolate chips for M&Ms./add walnuts, or swap half the chocolate chips for M\&Ms. For extra heat, add a pinch of chili flakes./' cookies.mdcd ~/recipes && git diff cookies.mdstudent@lab:~$ cd ~/recipes && sed -i 's/add walnuts, or swap half the chocolate chips for M&Ms./add walnuts, or swap half the chocolate chips for M\&Ms. For extra heat, add a pinch of chili flakes./' cookies.md student@lab:~$ cd ~/recipes && git diff cookies.md diff --git a/cookies.md b/cookies.md index 6568d52..22cbf3b 100644 --- a/cookies.md +++ b/cookies.md @@ -14,4 +14,4 @@ Instructions: 4. Drop spoonfuls onto a baking sheet. 5. Bake at 190C for 10-12 minutes. -Variations: add walnuts, or swap half the chocolate chips for M&Ms. +Variations: add walnuts, or swap half the chocolate chips for M&Ms. For extra heat, add a pinch of chili flakes.
🚫 Hit the Real Checkout Conflict
Try to switch to spicy-experiments right now and read git's real refusal carefully.
cd ~/recipes && git checkout spicy-experimentsstudent@lab:~$ cd ~/recipes && git checkout spicy-experiments error: Your local changes to the following files would be overwritten by checkout: cookies.md Please commit your changes or stash them before you switch branches. Aborting
📦 Stash, Switch, and Pop
Set the edit aside with git stash, switch cleanly, then bring it back on the correct branch.
cd ~/recipes && git stashcd ~/recipes && git checkout spicy-experimentscd ~/recipes && git stash popstudent@lab:~$ cd ~/recipes && git stash Saved working directory and index state WIP on master: 5997d5c Add serving note to cookie recipe student@lab:~$ cd ~/recipes && git checkout spicy-experiments Switched to branch 'spicy-experiments' student@lab:~$ cd ~/recipes && git stash pop Auto-merging cookies.md On branch spicy-experiments Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: cookies.md no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (4388c314b8106b52739b354c2344ac26b323a975)
📸 Commit It Where It Belongs
Stage and commit the chili flakes note on spicy-experiments, then return to master and view the full real graph.
cd ~/recipes && git add cookies.mdcd ~/recipes && git commit -m 'Add chili flakes note to variations'cd ~/recipes && git checkout mastercd ~/recipes && git log --oneline --all --decorate --graphstudent@lab:~$ cd ~/recipes && git add cookies.md student@lab:~$ cd ~/recipes && git commit -m 'Add chili flakes note to variations' [spicy-experiments 62d1bac] Add chili flakes note to variations 1 file changed, 1 insertion(+), 1 deletion(-) student@lab:~$ cd ~/recipes && git checkout master Switched to branch 'master' student@lab:~$ cd ~/recipes && git log --oneline --all --decorate --graph * 62d1bac (spicy-experiments) Add chili flakes note to variations * d48142c (manual-branch) Add ghost pepper chicken wings recipe | * 5997d5c (HEAD -> master) Add serving note to cookie recipe |/ * 7451729 Increase butter back to 175g for a richer flavor * 59cee07 Reduce butter to 150g per taste-test feedback * 22779d0 Add butter quantity to cookie ingredients * 9db68e6 Add variations note to cookie recipe * dda949a Credit the recipe source in the cookie title * c7a2617 Add step-by-step instructions to cookie recipe * 8c8aee6 Add Belgian waffles recipe * 5856011 Add basic garden salad recipe * fd509ba asdf * c764cd9 Add prep time estimates to all three recipes * 1aa939c Add salt to taste note in soup recipe * 56c4ee4 fixed stuff * b5dba00 Add chicken noodle soup recipe * cd7e65b Add ingredients list and pasta recipe * 7fb33b3 Add chocolate chip cookie recipe
Lab 1.3.3 complete. A real checkout conflict, understood and escaped cleanly:\n\n\n Conflict genuinely triggered : ✅ real error message read\n git stash used to escape : ✅ working tree cleaned\n Stash popped on right branch : ✅ applied cleanly\n Committed where it belongs : ✅ confirmed via graph\n
Enable JavaScript to run the live terminal and track your progress.