How does git stash work
Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.
Last updated: April 8, 2026
Key Facts
- Git stash was introduced in Git version 1.5.3 released in 2007
- The stash command can save both staged and unstaged changes by default
- Git maintains a stash stack where multiple stashes can be stored with 'git stash save'
- The 'git stash list' command shows all stored stashes with unique identifiers
- Stashes are stored locally in the .git/refs/stash directory and are not pushed to remote repositories
Overview
Git stash is a version control feature that allows developers to temporarily save uncommitted changes in their working directory. Introduced in Git version 1.5.3 in 2007 by Junio Hamano and the Git development team, it addresses a common workflow problem: developers often need to switch branches while working on incomplete features or bug fixes. Before stash existed, developers had to either commit incomplete work (creating messy commit histories) or manually save changes elsewhere. The stash feature was inspired by similar functionality in other version control systems like Mercurial's 'shelve' command. Git's implementation creates a special type of commit that stores both staged and unstaged changes, allowing developers to clean their working directory while preserving their work-in-progress. This feature has become essential for modern Git workflows, particularly in team environments where developers frequently switch between feature branches and main development lines.
How It Works
When you execute 'git stash', Git performs several operations: first, it saves your staged changes (those added with 'git add') and unstaged changes (modified files not yet staged) to a new stash entry. By default, it doesn't stash untracked files or ignored files unless you use the '-u' or '-a' flags. The command creates a special commit object that contains the saved changes, which gets stored in Git's object database. This stash commit has two parents: the current HEAD commit and the index state. Git then resets your working directory to match the HEAD commit, effectively cleaning your workspace. The stash is stored in a stack-like structure called the stash reflog, where you can have multiple stashes. You can view all stashes with 'git stash list', which shows them with identifiers like 'stash@{0}' for the most recent. To restore changes, 'git stash apply' reapplies the most recent stash while keeping it in the stack, while 'git stash pop' applies and removes it. You can also apply specific stashes using their identifiers.
Why It Matters
Git stash significantly improves developer productivity by enabling clean context switching between branches without committing incomplete work. This is particularly valuable in agile development environments where developers frequently work on multiple features or bug fixes simultaneously. According to Stack Overflow's 2023 Developer Survey, Git is used by over 93% of professional developers, making stash functionality crucial for daily workflows. It prevents 'work-in-progress' commits that clutter repository history and makes code reviews cleaner. Stash also facilitates emergency hotfixes: when a critical bug appears, developers can stash their current work, switch to the production branch, fix the issue, and then return to their original work. This feature has become so fundamental that Git GUI tools like GitHub Desktop, GitKraken, and SourceTree all include visual stash interfaces. Without stash, developers would waste significant time manually saving and restoring changes or creating temporary commits.
More How Does in Daily Life
Also in Daily Life
More "How Does" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- Git (software)CC-BY-SA-4.0
- Git Stash DocumentationGPL-2.0
Missing an answer?
Suggest a question and we'll generate an answer for it.