How to use git stash effectively

Here’s a clear, practical guide to git stash — what it is, when to use it, and the most useful commands and patterns.

What is git stash?

git stash lets you temporarily shelve (or stash) changes you’ve made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you’re mid-way through a code change and aren’t quite ready to commit.

The screenshot below shows the single stash in SourceTree app. On the sidebar, you can select any stash in the STASHES section and it is displayed on the right. In this post, we primarily work on the commands using with Terminal instead.

Showing a single stash in SourceTree App
Showing a single stash in SourceTree App

Use it when you:

  • Need to quickly switch branches but have local changes you’re not ready to commit.
  • Want to pull/rebase but your working tree isn’t clean.
  • Want to save a WIP (work-in-progress) snapshot without cluttering history.

The basics

Stash your changes

git stash            # Stashes tracked changes and resets working tree
git stash -m "WIP: fix search bug"   # Add a message for easier recall

Include untracked/ignored files

git stash -u         # Include untracked files
git stash -a         # Include untracked + ignored files

List your stashes

git stash list
# stash@{0}: On feature/login: WIP: fix token refresh
The output of git stash list
The output of git stash list

See what’s inside a stash

git stash show                   # Summary of changes in the latest stash
git stash show -p                # Full patch (diff)
git show stash@{2}               # Show a specific stash

Restore your changes

git stash apply                  # Apply latest stash (keep it in stash list)
git stash apply stash@{2}        # Apply a specific stash

Apply and remove in a once go

git stash pop                    # Apply latest stash and drop it if successful
git stash pop stash@{1}

Delete a stash and all stashes

git stash drop stash@{2}
git stash clear                  # ⚠️ Deletes all stashes

Helpful options & patterns

Keep staged, stash only unstaged

You might want to keep your staged changes (index) and only stash the rest:

git stash -k        # --keep-index

Stash only staged changes

If you want to stash only what is staged:

git stash --staged

It’s handy when you’ve prepared a partial commit, but need to put it aside temporarily.

Interactively select changes to stash

git stash -p        # --patch: choose hunks to stash

Stash by path (specific files/folders)

git stash -- path/to/fileA src/utils/

Only changes under those paths are stashed.

Create a branch from a stash

If you stashed work that really belongs on a new branch:

git stash branch my-feature stash@{0}

This:

  1. Creates and checks out my-feature from the stash’s base commit,
  2. Applies the stash,
  3. Drops it on success.

Practical workflows

1) Quick context switch

git stash -m "WIP: tweak search params"  # save work
git checkout main                        # switch safely
# ...do what you need...
git checkout feature/search
git stash pop                            # resume

2) Pull with a dirty working tree

git stash
git pull --rebase
git stash pop
# resolve conflicts if needed, then continue

3) Partial stashing (keep staged as-is)

git add -p              # stage only what you want
git stash -k            # stash the rest (keeps index)
# Now you can commit the staged bits cleanly, later apply the stashed leftovers

4) Move work to a branch cleanly

git stash -m "WIP: refactor helpers"
git checkout -b refactor/helpers
git stash pop

 

Common pitfalls & tips

  • Untracked files aren’t stashed by default. Use -u or -a when needed.
  • Conflicts can happen when applying or popping a stash, especially if the base moved. Resolve conflicts and continue, or consider git stash branch to recreate the original context.
  • Don’t rely on stash as long-term storage. It’s local-only and not part of your repo’s history. For shared WIP, consider a draft/WIP commit on a temporary branch.
  • Name your stashes. -m "WIP: …" makes git stash list much easier to navigate.
  • Stash is a stack. New stashes are stash@{0}, older ones shift to {1}, {2}, etc.
  • Internals: A stash is basically a couple of commits (for the index and working tree) referenced under refs/stash. Advanced users can inspect with git fsck or git log refs/stash.

Quick reference (cheat sheet)

# Create
git stash
git stash -u | -a
git stash -m "message"
git stash -p
git stash -- path1 path2

# Apply / Pop
git stash apply [stash@{n}]
git stash pop [stash@{n}]

# Inspect
git stash list
git stash show [-p] [stash@{n}]
git show stash@{n}

# Manage
git stash drop [stash@{n}]
git stash clear

# Index/Selection
git stash -k           # keep index (stash only unstaged)
git stash --staged     # stash only staged
git stash branch  [stash@{n}]

References

[1] https://git-scm.com/docs/git-stash, accessed on 26th December, 2025

[2] https://www.git-tower.com/learn/git/faq/git-stash-list, 26th December, 2025

[3] https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-list-and-show-the-git-stash-history, accessed on 26th November, 2025

Nguyen Vu Ngoc Tung

I love making new professional acquaintances. Don't hesitate to contact me via nguyenvungoctung@gmail.com if you want to talk about information technology, education, and research on complex networks analysis (i.e., metabolic networks analysis), data analysis, and applications of graph theory. Specialties: researching and proposing innovative business approaches to organizations, evaluating and consulting about usability engineering, training and employee development, web technologies, software architecture.

https://www.itersdesktop.com/author/nvntung/

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.