Git Stash – Save Work-in-Progress Without Committing

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:


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 stash

or with a message:

git stash save "WIP: working on login bug"

Check status after stash

git status

Everything should be clean.


3. Viewing the Stash List

git stash list

Example 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 apply

Apply 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 drop

Remove specific stash

git stash drop stash@{1}

Clear all stashes

git stash clear

7. Stashing Untracked & Ignored Files

By default, stash does NOT stash untracked files.

Stash untracked files too:

git stash -u

Stash untracked + ignored files:

git stash -a

Useful when working with:

  • build artifacts
  • logs
  • generated files

8. Partial Stashing (Selective)

Just like partial staging, you can stash specific hunks.

git stash -p

You 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 show

With full patch:

git stash show -p

View 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-api

12. 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

ActionCommand
Save stashgit stash
Save with msggit stash save "msg"
List stashesgit stash list
Applygit stash apply
Popgit stash pop
Drop stashgit stash drop stash@{n}
Clear allgit stash clear
Stash untrackedgit stash -u
Stash allgit stash -a
Partial stashgit stash -p
Stash into branchgit 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.


Leave a Reply

Your email address will not be published. Required fields are marked *