Skip to main content

Git Stash

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/git-stash

In short

Git stash is a command that temporarily saves uncommitted changes and cleans the working directory, so you can switch tasks and restore the work later.

What is git stash?

Git stash is a Git command that sets aside your uncommitted changes without making a commit. Running git stash saves your modified tracked files and staged changes, then resets the working directory to match the last commit. Later, you can bring the saved changes back and continue exactly where you left off.

Each stash is stored as a special commit on a stack, so the most recent one is stash@{0}, the one before it is stash@{1}, and so on. git stash pop reapplies the latest stash and removes it from the stack, while git stash apply reapplies it but keeps a copy. By default, new untracked files are not stashed, so add -u to include them and -m to give the stash a descriptive message. If the stashed changes clash with edits made since, Git reports a merge conflict that you resolve as usual.

Think of it like putting your half-finished paperwork in a drawer when someone urgently asks for help at your desk. A common situation is working on a feature when a critical bug report arrives: you stash the feature work, switch to another branch, fix the bug, then come back and pop your stash.

Stashing is often confused with committing. A commit is a permanent, shareable part of the project history, while a stash is local to your machine, is never pushed to a remote, and is easy to forget about. For work that will take more than a short detour, a work-in-progress commit on its own branch is often safer.

Key takeaways

  • git stash saves uncommitted changes and cleans the working directory.
  • Stashes are kept on a stack, and stash@{0} is always the newest.
  • pop restores and removes a stash, while apply restores it and keeps it.
  • Untracked files are included only when you add -u.
  • Stashes are local and are never pushed to a remote.

Example

Stashing work to fix a bug on another branchbash
# Save current changes with a message, including untracked files
git stash push -u -m "half-done login form"

# Switch away, fix something else, and come back
git switch hotfix
# ...fix the bug and commit...
git switch feature/login

# See what is stashed, then restore the latest stash
git stash list
git stash pop

Readers ask

What is the difference between git stash pop and git stash apply?

git stash pop reapplies the most recent stash and then deletes it from the stash list. git stash apply reapplies it but keeps it in the list, which is useful if you want to apply the same changes to several branches.

Does git stash include untracked files?

Not by default. Use git stash -u to include untracked files, or git stash -a to also include ignored files.

Can I stash only some files?

Yes. Use git stash push <path> to stash specific files, or git stash push -p to choose individual changes interactively.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings