How do you stash an untracked file?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
By default, git stash saves tracked changes and ignores untracked files. If your working tree contains new files that are not yet in Git and you want to shelve them temporarily, you need to opt in explicitly. The important distinction is between stashing tracked plus untracked files and stashing absolutely everything, including ignored files.
Understand What Untracked Means
An untracked file exists in your working directory but is not part of the index and not part of the latest commit.
Check the state with:
You might see output like:
Here, notes.txt is untracked. Plain git stash will not include it.
Use -u or --include-untracked
To stash tracked changes together with untracked files, use:
The -u flag tells Git to include untracked files as part of the stash entry.
Afterward, confirm what is stored:
This is the standard answer when the question is specifically about new files that are not yet tracked.
Know When -a Is Different
If you also want ignored files, use -a or --all:
This is more aggressive than -u. It includes ignored files such as build output, cache directories, or generated artifacts.
Use it carefully. Large ignored directories can make stash operations slower and heavier than expected.
Restore the Stash Safely
There are two common ways to restore a stash:
- '
git stash applyrestores the changes but keeps the stash entry' - '
git stash poprestores the changes and removes the entry if the apply succeeds'
If you expect conflicts or want a safer first pass, apply is often better than pop because the stash entry remains available until you decide to drop it.
Stash Only Part of the Working Tree
You can also limit the stash to a path when only one part of the repository needs to be shelved.
This is useful when you want to isolate a local experiment without disturbing unrelated work in the same repository.
You can combine that with --keep-index when you want to preserve staged changes and stash only the rest:
That workflow is handy when you want to commit one clean part of the work while parking the unfinished files temporarily.
Use git stash branch for Older or Risky Stashes
If a stash is old or likely to conflict with the current branch state, restoring it on a fresh branch is often cleaner.
That command creates a new branch at the stash base, applies the stash there, and drops the entry if the apply succeeds. It is often safer than applying an old stash onto a heavily changed branch tip.
Add Good Messages and Inspect Before Dropping
Stash messages are cheap and save time later. A clear message such as "WIP before hotfix" is far more useful than a pile of anonymous stash entries.
Before deleting a stash, inspect it:
That habit reduces the chance of dropping the wrong temporary work.
Common Pitfalls
The biggest pitfall is running plain git stash and assuming untracked files were included. They were not.
Another common issue is using -a without realizing that ignored files are now part of the stash too. That can make the stash larger and messier than intended.
People also pop a stash immediately during a conflict-heavy situation and lose the easy fallback of keeping the stash entry around for another try.
Summary
- Use
git stash push -uto include untracked files in a stash. - Use
-aonly when ignored files also need to be shelved. - Prefer
git stash applyfirst when you want a safer restore path. - Use path-limited stashes and
--keep-indexwhen only part of the work should move aside. - Add clear stash messages so temporary work stays understandable.
Related reading
- How do you stop tracking a remote branch in Git?
- How do you stop tracking a remote branch in Git?
- How do you synchronise projects to GitHub with Android Studio?
- How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?
- How do you use both Spring Data JPA and Spring Data Elasticsearch repositories on the same domain class in a Spring Boot application?
- How do you use git --bare init repository?
- How do you use Git with multiple computers?
- How do you version your database schema?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.