You're halfway through a feature when an urgent production bug arrives. You need to switch branches now. But what happens to your unfinished changes?
Git protects you here. If switching would overwrite changes in your working tree, it blocks the switch rather than quietly throwing your work away. And you shouldn't have to commit half-finished code just to handle an emergency.
You have two options, and the right one depends on the state of your work:
still working on it -> git stash
ready to record -> git commit
Both keep your work safe. Only one goes in the branch history.
This 2-minute explainer walks the whole round trip with a Ruby on Rails payment-api example: stash the unfinished feature, switch to main, fix and commit the production bug, switch back, and pop the stash to land exactly where you left off.
What you'll learn:
- Why Git sometimes blocks a branch switch
- What git stash actually does
- How to save unfinished changes temporarily
- How to restore them with git stash pop
- When to stash and when to commit
- How to inspect what you've saved
- The limitations worth knowing about
The commands:
# set the unfinished work aside
git stash push -m "WIP: payment feature"
# now the switch goes through
git switch main
# fix it, and commit the fix
git add .
git commit -m "Fix production login bug"
# come back and pick up where you left off
git switch feature/payment
git stash pop
# see what you've saved
git stash list
# restore but keep the stash entry
git stash apply
Four things worth remembering:
1. git stash push saves tracked changes by default — staged and unstaged. Untracked files are NOT included unless you add -u, and ignored files need -a.
2. git stash pop removes the stash entry only if the stash applies successfully. If there's a conflict, the entry stays, so you don't lose it.
3. git stash apply restores your changes and leaves the entry in place.
4. Stash is local temporary storage — not a backup. It doesn't replace commits, and it isn't pushed anywhere.
One note on `git add .` — it's shown here as a simplified example. In real work, check what you're staging before you commit it.
Chapters:
00:00 — The Hook: Don't Lose Your Work
00:10 — Why Git Blocks Branch Switching
00:25 — Stash vs Commit
00:40 — How Git Stash Works
01:00 — Practical Rails Example
01:25 — Benefits and Trade-offs
01:45 — The Takeaway
Save your work before you switch branches.
#git #github #versioncontrol #gitstash #developer #programming #softwareengineering #devops #rubyonrails #coding #webdevelopment #gitcommands #codingtips #softwaredevelopment #backend
— #Astmation —
#software #systems, #automation, and #AI-powered #applications.
Build. Automate. Evolve.
https://astmation.com/
Watch on YouTube