Git Stash
Learn how to temporarily save unfinished work with stash, including apply, pop, drop, list, named stashes, and untracked files.
What Git Stash Is For
Git stash lets you temporarily set aside local changes without committing them. It is useful when you are halfway through one task and suddenly need to switch branches to fix something urgent.
Instead of creating a messy “work in progress” commit, you can stash the changes, do the urgent task, and then restore your original work later.
Tip: Stash is great for short-term interruptions. For longer work, a real branch and commit are usually safer.
Creating a Basic Stash
Suppose you modified app.js and README.md but are not ready to commit.
Check the status:
git status
Now stash the tracked changes:
git stash
Git saves the current state and reverts your working directory to match the last commit.
You can confirm with:
git status
The working tree should now be clean.
Listing Stashes
To see saved stashes:
git stash list
Example output:
stash@{0}: WIP on feature/login: a1b2c3d Add login page
stash@{1}: WIP on main: d4e5f6g Update README
Each stash has an identifier like stash@{0}.
Restoring a Stash with apply
Use git stash apply when you want to restore a stash but keep it in the stash list.
git stash apply stash@{0}
This is useful if you want to test the restored changes before deciding whether to remove the stash entry.
If you omit the name, Git applies the latest stash:
git stash apply
Restoring and Removing with pop
Use git stash pop when you want to restore a stash and remove it from the stash list in one step.
git stash pop
This is the most common restore command.
apply vs pop
apply: restore but keep the stash savedpop: restore and delete the stash entry if successful
Naming a Stash
Default stash messages are not always easy to remember. You can create a named stash:
git stash push -m "half-finished navbar styling"
List it later:
git stash list
Named stashes are much easier to identify when you have several saved states.
Stashing Untracked Files
By default, git stash saves changes to tracked files only. If you also want to include untracked files, use -u.
git stash push -u -m "save new config files too"
This is helpful when you created new files but are not ready to commit them yet.
Note: Untracked files are files Git sees but is not yet tracking. Ignored files are a separate category and are not included unless you use additional options.
Dropping and Clearing Stashes
To delete one stash entry:
git stash drop stash@{0}
To remove all stash entries:
git stash clear
Be careful with clear because it removes everything from the stash list.
A Real Workflow Example
Imagine you are on feature/docs editing tutorial content when a production issue appears.
git status
git stash push -m "draft updates for Git basics"
git switch main
git switch -c hotfix/header-bug
# fix the issue
git add .
git commit -m "Fix broken header in production"
git switch feature/docs
git stash pop
This lets you pause one task, fix another, and return without creating a confusing temporary commit.
Handling Stash Conflicts
Applying a stash can sometimes create conflicts, especially if the branch changed since the stash was created.
If that happens:
- run
git status - resolve the conflicts manually
- stage the resolved files with
git add - continue working or commit the result
A stash is not magic. It still depends on how different the files became over time.
When Not to Use Stash
Stash is not ideal for everything.
Avoid relying on stash when:
- you need a durable backup of important work
- the work should be reviewed by others
- you are pausing for a long period
- you are working across multiple machines
In those cases, a normal branch and commit are usually better.
What You Should Remember
Git stash is a temporary shelf for unfinished work. Use stash to save changes quickly, list to inspect saved entries, apply or pop to restore them, and drop or clear to clean them up. Use -u for untracked files and -m for a helpful message.
For quick interruptions, stash is excellent. For meaningful milestones, commit to a branch instead.
Test Your Understanding
Let's see how well you understood the concepts! These exercises will help reinforce what you just learned.
Exercise 1: Pausing work quickly
Scenario: You are halfway through editing app.js and README.md, and an urgent bug forces you to switch tasks immediately.
Question: Which Git feature from this lesson is designed for that short-term interruption without making a work-in-progress commit?
Exercise 1: Pausing work quickly
Choose the Git feature intended for temporarily shelving unfinished local changes.
Exercise 2: Saving untracked files too
Scenario: Your unfinished work includes brand-new config files that are still untracked, and you want a descriptive stash entry.
Question: Which command best matches the tutorial?
Exercise 2: Saving untracked files too
Choose the stash command that includes untracked files and adds a helpful message.
Exercise 3: Keeping the stash after restore
Scenario: You want to restore a stash to test something, but you do not want the stash entry removed yet.
Question: Which command should you prefer?
Exercise 3: Keeping the stash after restore
Pick the restore command that leaves the stash entry in the list.