Git Stash (git stash)
What Will We Learn?
- How to save unfinished changes and apply it later without losing them.
- What is
git stashand when to use it with common real world usage. - Visualizing the flow of
git stash. - Common pitfalls and best practices using
git stash.
What git stash Does?
Literally, stash means to store or hide something in a safe place for later use. And to keep it simple, this is exactly what git stash is for. More precisely:
It safely stores your unfinished changes and resets your working directory to the clean state matching the last commit.
- To store something means to store changes in your working dir or index
- The safe place is
.git/refs/stashwhich acts as a pointer to the latest stash entry - Later use means when you are ready or done with something that was more necessary
What Stash Entry Consists Of?
git stash creates a new stash entry including:
- Modified files (by default)
- Staged files (by default)
- Untracked files (but only when you tell it so providing
-uoption)
Use Cases: Why use Git Stash?
- Temporarily switch tasks — When you need to quickly move to another branch, fix a bug, or review someone’s code without committing incomplete work.
- Keep the working tree clean — Useful when commands like
git pull, branch switching, or testing require a clean working directory. - Save work-in-progress safely — Stash changes that aren’t ready for a commit yet, then restore them later with
git stash poporgit stash apply. - Avoid unnecessary commits — Instead of creating commits like
WIPortemporary changes, stash the work until it reaches a meaningful checkpoint.
Git Stash Flow
flowchart TD
A[Working Changes] --> B[git stash]
B --> IMP[Something Important Occur]
IMP --> C[Changes saved in Stash]
C --> D{Restore?}
D -->|Yes, keep stash| E[git stash apply]
D -->|Yes, remove stash| F[git stash pop]
D -->|No| G[Keep Stash for Later]
E --> H[Changes Restored]
F --> H
Git Stash Operations
Think of stash as stack. It does have similar operations to the stack data structure like: push, pop, drop, apply, etc.
Saving or Stashing Changes
You basically start here by stashing or saving your changes:
git stash pushYou already know, git stash saves modified and staged files by default. If you also want to save untracked files, provide -u option:
git stash push -uProtip
push operation is default for git stash. This means you can just use git stash for stashing changes.
It creates stash entry like:
stash@{0}: WIP on <branch>: 8d0a053 <commit-message>This message can be confusing. Instead, provide meaningful message.
git stash --message "meaningful message"Deprication Warning
People often uses git stash save instead of push but save is depricated.
Listing Stash Entries
You can list your stash entries with list option:
git stash listThe list:
stash@{0}: On master: WIP Auth feature
stash@{1}: WIP on master: 8d0a053 universeViewing Stash Entries
You can use show operation to show the changes recorded in stash entry. By default, it shows the diffstat between the stashed contents and the commit when the entry was stashed like:
git stash show
file | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)But it accepts git diff options too. Common usage can be:
git stash show -p stash@{1}Applying Stash Changes
Once you are done with whatever important task you were up to, you can apply the stashed changes.
git stash applyBy default, it will apply the most recent list at the top of the stash. Provide specific stash identifier if you wish to apply different stash:
git stash apply stash@{1}Important
apply and pop are subject to merge conflict. Make sure your working tree is clean and be prepared to resolve conflicts when restoring stashed changes.
Important
apply operation does not remove the stash from the list after applying. If you both to apply and remove — use git stash pop operation.
Dropping or Removing Stash Entries
If you no longer need any stash entry, drop it.
git stash drop stash@{5}drop will only remove specified stash entry. To remove all entries, use clear.
git stash clearWarning
Be cautious with drop and clear as they remove your unfinished changes which you may not have applied yet. Be sure that you either don’t need or have applied already.
Final Flow
Each operation defined here is actually the general flow of using git stash.
flowchart LR
push --> list
list --> show
show --> APPLY_POP[apply/pop]
APPLY_POP --> DROP_CLEAR[drop/clear]
Best Practices
Best Practices for Using git stash:
- Use descriptive stash messages — Prefer
git stash push -m "fix login validation"so you know what each stash contains. - Stash only what you need — Avoid blindly stashing everything; use
git stash -pfor selective changes orgit stash -uwhen untracked files are also needed. - Check your stash list regularly — Use
git stash listand remove stale entries withgit stash droporgit stash clear. - Don’t use stash as long-term storage — If the work is important or will take days, create a proper branch and commit it instead.
Avoid These Using git stash
- Forgetting untracked files —
git stashdoes not include untracked files by default; usegit stash -uwhen needed. - Losing track of stashes — Multiple unnamed stashes can become confusing. Use
git stash listand descriptive messages. - Conflicts when restoring —
git stash poporapplycan cause merge conflicts if the branch has changed significantly since the stash was created. - Treating stash as a backup — Stashes are temporary work storage, not a reliable long-term backup. Commit important work to a branch instead.