DevOps · Infrastructure as Code (IaC) · Git Series – Part 8
Sometimes you’re in the middle of changes—files modified, half-finished code, tests failing—but suddenly need to:
✔ Switch branches
✔ Pull latest changes
✔ Apply a hotfix
✔ Run deployment scripts
✔ Test something cleanly
But your working directory isn’t clean.
That’s when Git Stash becomes your lifesaver.
In this chapter, you’ll learn:
Table of Contents
1. What Is Git Stash?
git stash temporarily saves your changes without committing them and restores your working directory to a clean state.
Git moves your:
- modified files
- staged changes
into a hidden storage area called stash.
Your working directory becomes clean without losing your work.
2. Basic Stash Usage
Save current changes
git stashor with a message:
git stash save "WIP: working on login bug"Check status after stash
git statusEverything should be clean.
3. Viewing the Stash List
git stash listExample output:
stash@{0}: WIP on feature/login
stash@{1}: WIP: refactor modules
stash@{2}: WIP: before deployment
4. Applying Stashes
Apply the latest stash
git stash applyApply a specific stash
git stash apply stash@{2}
Important: Applying does NOT remove the stash.
You must manually drop it when done.
5. Apply & Remove in One Command
git stash pop
This:
- applies stash
- removes stash@{0}
Use with caution—pop can cause conflicts.
6. Removing Stashes
Remove latest stash
git stash dropRemove specific stash
git stash drop stash@{1}Clear all stashes
git stash clear7. Stashing Untracked & Ignored Files
By default, stash does NOT stash untracked files.
Stash untracked files too:
git stash -uStash untracked + ignored files:
git stash -aUseful when working with:
- build artifacts
- logs
- generated files
8. Partial Stashing (Selective)
Just like partial staging, you can stash specific hunks.
git stash -pYou will be prompted hunk by hunk:
Stash this hunk [y,n,q,a,d,?]?This is extremely powerful when you want to stash some changes but commit/stage others.
9. Create a Branch from a Stash
Ideal for long-running features.
git stash branch feature/new-login stash@{0}This will:
- create new branch
- apply the stash
- delete it automatically
10. Inspect Stash Contents
View diff of latest stash:
git stash showWith full patch:
git stash show -pView specific stash:
git stash show -p stash@{2}11. Real DevOps Use Cases
📌 1. Hotfix in production branch
You’re coding on feature/ui-redesign, and suddenly need to fix an urgent issue in main.
git stash save "WIP ui redesign"
git checkout main
# fix issue
git checkout feature/ui-redesign
git stash pop
📌 2. Pull latest changes from remote
You can’t pull until your directory is clean:
git stash
git pull
git stash pop📌 3. Testing deployments / Ansible playbooks
Temporary changes might break IaC testing.
git stash -u
ansible-playbook test.yml
git stash pop📌 4. Switching Features Quickly
git stash
git switch feature/new-api12. Common Stash Problems & Solutions
❗ Stash merge conflicts
If applying a stash causes conflicts:
git status
# resolve conflicts
git add .
git commit -m "Resolve stash conflicts"❗ Stash accidentally popped
Recover stash using reflog:
git reflog
git stash apply stash@{<hash>}Git keeps stash internals for a while.
13. Git Stash Summary Table
| Action | Command |
|---|---|
| Save stash | git stash |
| Save with msg | git stash save "msg" |
| List stashes | git stash list |
| Apply | git stash apply |
| Pop | git stash pop |
| Drop stash | git stash drop stash@{n} |
| Clear all | git stash clear |
| Stash untracked | git stash -u |
| Stash all | git stash -a |
| Partial stash | git stash -p |
| Stash into branch | git stash branch <name> |
14. What’s Next?
You now understand:
✔ Stash basics
✔ Applying, popping & dropping
✔ Partial stashing
✔ Untracked/ignored stashing
✔ Creating stash branches
✔ DevOps/IaC workflows
Next chapter:
➡️ Part 9 – Git History: Logs, Graphs, Blame, Diffs & Time Travel
You’ll learn powerful history commands like git log, git blame, git diff, git reflog, and how to navigate or repair history like a pro.