What is Git Stash?
Git stash is used to temporarily save your local changes without committing them. This allows you to clean your working directory so you can switch branches, pull updates, or perform other operations without losing your work.
For example, if you are working on a feature and suddenly need to switch to another branch:
git stash
git checkout another-branch👉 Your changes are safely stored and can be restored later.
In simple terms:
Git stash = temporarily store your work without committing
What does git stash do?
When you run git stash, Git:
- Saves your modified tracked files (and optionally staged changes)
- Reverts your working directory back to the last committed state
- Stores your changes in a stack-like structure (called "stash")
You can later:
- View stashed changes
- Apply them back
- Remove them if no longer needed
👉 Think of it as a temporary clipboard for your code changes
When should you use git stash?
Use git stash when:
- You want to switch branches without committing unfinished work
- You need to pull latest changes but have local modifications
- You want to temporarily save work-in-progress changes
- You want a quick way to backup changes without creating commits
Avoid using stash when:
- You need permanent history (use commit instead)
- You are collaborating heavily and changes must be visible
Git Stash – Quick Cheat Sheet
| Description | Command |
|---|---|
| Stash current changes | git stash |
| Stash with message | git stash push -m "message" |
| Stash only staged changes | git stash --staged |
| Stash including untracked files | git stash -u |
| Stash including ignored files | git stash -a |
| Stash specific files | git stash push <file> |
| Stash all local changes | git stash -u |
| List all stashes | git stash list |
| Show stash details | git stash show |
| Show full diff of stash | git stash show -p |
| Apply latest stash | git stash apply |
| Apply specific stash | git stash apply stash@{n} |
| Apply and remove stash | git stash pop |
| Pop specific stash | git stash pop stash@{n} |
| Create branch from stash | git stash branch <branch-name> |
| Drop latest stash | git stash drop |
| Drop specific stash | git stash drop stash@{n} |
| Clear all stashes | git stash clear |
| Check stash status | git stash list |
| Stash before pull | git stash && git pull && git stash pop |
| Recover dropped stash | git reflog |
| Save changes without commit | git stash |
| Restore stashed changes | git stash apply |
| Remove stash after apply | git stash drop |
| Stash changes before switching branch | git stash |
| View stash file list | git stash show --name-only |
Quick Examples
# Save local changes
git stash
# Save with message
git stash push -m "work in progress"
# List stashes
git stash list
# Apply stash
git stash apply
# Apply and remove stash
git stash pop
# Stash including untracked files
git stash -uCommon Git Stash Commands
git stash
The git stash command is used to temporarily save your local changes without committing them.
git stashThis command stores your changes and resets your working directory to the last committed state. It is commonly used when you want to switch branches without committing incomplete work, similar to workflows explained in git checkout command.
git stash list
To view all saved stashes:
git stash listThis displays a list of stashes in the format:
stash@{0}: WIP on branch-name
stash@{1}: WIP on another-branchgit stash show
To view details of a stash:
git stash showFor full changes (diff):
git stash show -pThis helps you inspect what was saved before applying it, similar to inspecting changes using git diff examples.
git stash apply
To restore stashed changes without removing them:
git stash applyTo apply a specific stash:
git stash apply stash@{1}This keeps the stash in the list for future reuse.
git stash pop
To apply and remove the stash at the same time:
git stash popThis is the most commonly used way to restore changes after temporarily saving them.
Stash Local Changes
Stash tracked files
By default, git stash only saves tracked (modified) files:
git stashThis is useful when you want to temporarily save your progress before pulling updates or switching branches.
Stash all changes (including untracked)
To stash both tracked and untracked files:
git stash -uTo include ignored files as well:
git stash -aThis ensures that all local changes are saved, including newly created files.
View and Manage Stashes
List stashes
To see all saved stashes:
git stash listEach stash is indexed and can be referenced later.
Show stash details
To inspect what changes are stored in a stash:
git stash show -pThis gives a detailed diff view, helping you decide whether to apply or discard the stash.
Delete stash
To remove a specific stash:
git stash drop stash@{0}To remove all stashes:
git stash clear👉 Use this carefully, as deleted stashes are not easily recoverable without using advanced tools like git reflog tutorial.
Apply or Restore Stash
Apply or restore stash
To restore the most recent stash without removing it:
git stash applyThis reapplies your changes but keeps the stash saved for reuse. This is useful when you want to test changes without losing the backup.
Apply or restore specific stash
If you have multiple stashes, you can apply a specific one:
git stash apply stash@{1}You can identify the correct stash using git stash list.
Pop stash vs apply
git stash apply→ restores changes but keeps stashgit stash pop→ restores changes and removes stash
git stash pop👉 Use pop when you no longer need the stash, and apply when you may reuse it.
Create a new branch from stash
If you want to apply a stash directly to a new branch, use:
git stash branch <branch-name>Example:
git stash branch feature-loginThis command:
- creates a new branch
- checks out that branch
- applies the latest stash
- removes the stash if applied successfully
👉 This is useful when you started making changes on the wrong branch and later decide those changes should go into a separate branch. In such cases, it also works well with git branch examples and git switch command.
If you have multiple stashes, first identify the correct one using:
git stash listThen create the branch from that stash:
git stash branch <branch-name> stash@{1}Advanced Stash Usage
Stash with message
To add a meaningful message while stashing:
git stash push -m "work in progress"This makes it easier to identify stashes later, similar to writing clear messages in git commit message.
Stash specific files
To stash only selected files:
git stash push file1 file2This is useful when you don’t want to stash all changes in your working directory.
Stash before pull
If you have local changes and want to pull updates safely:
git stash
git pull
git stash popThis prevents conflicts while pulling changes, as explained in git pull command examples.
Common Errors and Fixes
no local changes to save
This error occurs when there are no modified or staged files.
Fix:
- Run
git statusto verify changes - Ensure files are modified or staged
stash not applying
This can happen due to conflicts between stashed changes and current files.
Fix:
- Resolve conflicts manually
- Use:
git stash applyUnderstanding file differences using git diff examples can help resolve conflicts faster.
lost stash recovery
If a stash is accidentally dropped or lost, you can recover it using:
git reflogThen restore using:
git reset --hard <commit-id>👉 Learn more about recovery using git reflog tutorial.
Frequently Asked Questions
1. What is git stash?
Git stash is used to temporarily save local changes without committing them, allowing you to switch branches or pull updates without losing your work.2. How do I stash local changes in Git?
You can stash local changes usinggit stash, which saves your changes and cleans your working directory.3. How do I list stashed changes?
Usegit stash list to view all saved stashes in your repository.4. How do I apply a stash in Git?
You can apply a stash usinggit stash apply or remove it after applying using git stash pop.5. Why do I get 'no local changes to save' in git stash?
This occurs when there are no modified or staged files in your working directory to stash.Summary
Git stash is a powerful feature that allows you to temporarily save changes without committing them.
In this guide, you learned how to:
- Stash and restore changes safely
- Manage multiple stashes
- Apply or remove stashes efficiently
- Handle common errors and recover lost work
Using git stash effectively helps you switch tasks quickly, keep your working directory clean, and avoid unnecessary commits.




![Git Error: Cannot Delete Branch Checked Out or Used by Worktree [SOLVED]](/cannot-delete-branch-checked-out-at/git-cannot-delete-branch_hu_3f86bd25a59d627d.webp)
![[SOLVED] Pulling is not possible because you have unmerged files](/pulling-is-not-possible-because-you-have-unmerged-files/git-pull-conflict_hu_849287d4525bf4c8.webp)




