Home Coding & Scripting Using Git Stash to Save Work-in-Progress

Using Git Stash to Save Work-in-Progress

281
0

Sometimes you’re in the middle of writing code, and suddenly you need to:

  • switch branches to fix a bug,
  • pull the latest changes, or
  • do something quickly without committing unfinished work.

But your current changes are not ready to commit yet.
This is exactly where Git Stash helps.

Git Stash lets us temporarily save our uncommitted changes and return to a clean working directory—without losing anything.


1. What Is Git Stash?

Git stash is like taking a “snapshot” of our current unfinished work and putting it in a temporary storage area called the stash stack.

Think of it as:

  • a clipboard for code
  • a place to save our messy work-in-progress
  • a way to switch tasks without committing junk

When we’re ready, we can bring those changes back.


2. When Should We Use Git Stash?

Use git stash when:

  • We need to change branches but have uncommitted work.
  • We want to test something without losing what we’re doing.
  • We need a clean working directory (e.g., before pulling).
  • Our work isn’t ready to commit yet.

3. Stashing Our Work

Stash all tracked file changes

git stash

Stash everything (tracked + untracked files)

git stash -u

or

git stash --include-untracked

Add a message (recommended)

git stash save "WIP: login page fixes"

or with newer syntax:

git stash push -m "WIP: login page fixes"

4. Viewing All Stashes

To see everything you’ve saved:

git stash list

You will see entries like:

stash@{0}: WIP on main: login page fixes
stash@{1}: WIP on feature-auth: token update

5. Getting Our Work Back

There are two ways to apply stashes.

Option 1: Apply stash and keep it in the stack

git stash apply stash@{0}

Use this when we want to reuse the stash later.

Option 2: Apply stash and remove it from stack

git stash pop stash@{0}

This is usually what beginners need—apply + delete.


6. Deleting Stashes

Remove a specific stash

git stash drop stash@{1}

Clear ALL stashes

(Be careful!)

git stash clear

7. Stashing Only Specific Files

We don’t have to stash everything.
You can stash just one file or a set of files:

git stash push -m "partial stash" file1.js file2.css

8. Stashing With a “Patch” (Interactive Mode)

Want to stash only part of a file?

git stash push -p

We’ll be asked which chunks to stash.


9. Real-World Example

Scenario

We’re editing the login page, but a teammate asks us to fix a bug on another branch.

Steps

  1. Save our current work: git stash push -m "WIP: login page UI"
  2. Switch to the bug fix branch: git checkout bugfix/user-profile
  3. Fix the bug, commit, and return to our original branch: git checkout main
  4. Restore our stashed login page work: git stash pop

Done! We didn’t lose anything.


10. Summary Table

TaskCommand
Stash tracked changesgit stash
Stash tracked + untrackedgit stash -u
Stash with messagegit stash push -m "message"
View stashesgit stash list
Apply stashgit stash apply
Apply + delete stashgit stash pop
Delete specific stashgit stash drop
Clear all stashesgit stash clear
Stash specific filesgit stash push file1

Final Thoughts

Git stash is one of the most useful tools for real-world development.
It helps us stay organized, avoid messy commits, and switch tasks without stress.

Previous articleUndoing Changes in Git: restore, reset, revert
Next articleHandling Merge Conflicts with Git
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here