Stashing Changes in Git for Later

Summary: Temporarily save your work using git stash.


When working with Git, it's common to find yourself in the middle of implementing a feature or a fix when another urgent task needs your attention. Or perhaps you want to update your branch but you have local changes that aren’t ready to commit yet. In situations like these, Git’s stash feature comes to the rescue.

In this post, we’ll explain what stashing is, how to use it effectively, and review some advanced tips for managing your stashed work.

What Is Git Stash?

git stash is a command that temporarily shelves (or stashes) changes you've made to your working directory and staging area, giving you a clean working directory. You can later restore the stashed changes when you're ready to continue working on them.

This is extremely useful if you need to:

  • Switch branches quickly
  • Pull the latest changes
  • Make an urgent fix elsewhere
  • Avoid cluttering your commit history with work-in-progress code

Common Use Cases

  • Switching Contexts: You’re halfway through developing Feature A when you’re asked to fix a bug on another branch.
  • Keeping Work Clean: You want to pull new changes from a remote branch, but you have local modifications you aren’t ready to commit.
  • Code Reviews: You want to review code with a clean workspace.

How to Stash Changes

Here’s a step-by-step guide on how to use git stash:

1. Saving Changes

Suppose you’ve made changes to some files but haven’t committed them yet:

$ git status
On branch feature-x
Changes not staged for commit:
  modified:   file1.txt
  modified:   file2.js

To stash your changes:

$ git stash
Saved working directory and index state WIP on feature-x: 0123456 Update README

By default, this saves changes that are both staged (added with git add) and unstaged.

Tip: If you want to add a custom message:

$ git stash save "WIP: working on feature-x"

or, using the newer syntax:

$ git stash push -m "WIP: working on feature-x"

2. Listing Stashes

You can view all your stashed changes:

$ git stash list
stash@{0}: On feature-x: WIP: working on feature-x
stash@{1}: On main: fix typo in docs

Each stash is identified by an index (stash@{0}, stash@{1}, etc.).

3. Applying Stashes

When you’re ready to return to your work, you can apply the latest stash:

$ git stash apply

To apply a specific stash:

$ git stash apply stash@{1}

This applies the stashed changes but leaves them in the stash list (does not remove them).

To apply and remove the stash at the same time:

$ git stash pop

This command applies the most recent stash and deletes it from the stash list.

4. Stashing Part of Your Changes

You can stash just the changes that are staged by running:

$ git stash --keep-index

Or just changes in certain files:

$ git stash push path/to/file1.js path/to/file2.txt

5. Dropping and Clearing Stashes

  • Delete a specific stash:
    $ git stash drop stash@{0}
    
  • Clear all stashes:
    $ git stash clear
    

Advanced Usage

Stashing Untracked and Ignored Files

By default, git stash only stashes tracked files. To also stash untracked files:

$ git stash -u    # or --include-untracked

To also include ignored files:

$ git stash -a    # or --all

Creating a Branch from a Stash

You can create a new branch from your stashed changes:

$ git stash branch my-feature-branch

This checks out the commit where you created the stash, applies the stashed changes, and creates a new branch for you.

Inspecting Stash Contents

To see what’s inside a stash:

$ git stash show

Or for a detailed diff:

$ git stash show -p stash@{0}

Things to Remember

  • Stashes are local to your repository (.git directory) and are not shared with others unless you explicitly commit and push them.
  • Don’t rely solely on stashes for long-term storage; they’re intended as a temporary shelf for work-in-progress.
  • Avoid having too many stashes, as it can get confusing to track their contents and purposes.

Summary

Git’s stashing feature is a life-saver for developers who need to multitask, stay agile, and keep their working directory clean. With git stash, you can quickly save your changes, switch contexts, and return exactly where you left off — all without messy commits or losing any work.

Next time you find yourself needing to switch gears, let git stash help you keep your workflow smooth and efficient.


Happy coding! 🚀