Git Stash Explained With Real Workflows

Abstract generated cover for Git Stash Explained With Real Workflows.

`git stash` temporarily saves uncommitted changes so you can get them out of the working tree. That is useful when you are halfway through work and need to switch tasks. For example:

  • you need to pull changes but have local edits
  • you need to switch branches quickly
  • you started work in the wrong branch
  • you want to test something from a clean state Stash is useful, but it can also become a junk drawer. Use it carefully.

The Mental Model

Your working tree is the current state of files on disk. When you edit files but have not committed them, Git sees local changes. `git stash` takes those local changes and stores them away. Then your working tree becomes clean again. Basic command:

git stash

Check stashes:

git stash list

Restore the latest stash:

git stash apply

Restore and remove it from the stash list:

git stash pop

Stash With a Message

A named stash is much easier to understand later.

git stash push -m "wip homepage image cleanup"

Then:

git stash list

You will see a message instead of a vague stash entry. This matters because unnamed stashes become confusing quickly. If you cannot tell what a stash contains, you are less likely to apply it safely.

Apply vs Pop

`apply` restores the stash but keeps it in the stash list.

git stash apply

`pop` restores the stash and removes it if the apply succeeds.

git stash pop

If you are unsure, use `apply`. You can manually drop the stash after confirming everything is right:

git stash drop stash@{0}

This is a safer habit when the stash is important.

Inspect a Stash

Before applying a stash, inspect it.

git stash show

For the patch:

git stash show -p

This helps you avoid applying old changes blindly. If you have several stashes, specify one:

git stash show -p stash@{2}

Use `git stash list` to see the available entries.

When Stash Is Useful

Stash is good for short interruptions. Example:

git status
git stash push -m "wip article layout"
git switch master
git pull
git switch feature-branch
git stash apply

That is a reasonable flow. The stash exists for minutes or hours, not weeks. It solves a temporary problem.

When a Branch Is Better

If the work matters, a temporary branch may be safer.

git switch -c wip/article-layout
git add .
git commit -m "WIP article layout"

A WIP commit on a branch is visible and easier to recover. You can clean it up later. Stashes are more hidden. They do not show up in normal branch history. That makes them easy to forget.

Common Mistakes

Mistake 1: Using stash as long-term storage

Stash is not a project management system. If the work matters, use a branch.

Mistake 2: Popping without checking

`git stash pop` is convenient, but inspect important stashes first. Use `apply` when you want to be cautious.

Mistake 3: Forgetting untracked files

By default, stash may not include untracked files. To include them:

git stash push -u -m "wip with new files"

Know whether your new files are included before assuming the working tree is saved.

Where This Shows Up in Real Projects

Stash is useful when moving between machines, branches, and urgent fixes. For example, if Git refuses to pull because you have local changes, stash can temporarily move those changes aside. But if those local changes are meaningful, it may be better to commit them on a branch first. The goal is to avoid losing work while keeping the repository understandable.

Key Takeaways

  • `git stash` temporarily saves uncommitted changes.
  • Use stash for short interruptions.
  • Add messages to stashes.
  • Inspect stashes before applying old changes.
  • Use `apply` when you want caution and `pop` when you are confident.
  • Use a branch for important longer-lived work.

    Related Articles

  • Git Branching Explained: Feature Branch to Master

  • Git Rebase vs Merge Explained
  • Debugging Broken Local Dev Environments

← Back to Blog Index