How do I stash an untracked file in Git?
I have changes to an existing file plus a new untracked file in my Git repository. I want to use git stash to temporarily save these changes while I switch to another task. However, git stash by default only stashes changes to tracked files, leaving the new untracked file in my working directory. This creates clutter when I return to my original task. What is the proper way to stash both tracked changes and untracked files together in Git?
To stash untracked files in Git, you need to use the -u (or --include-untracked) flag with the git stash command. This tells Git to include both tracked changes and untracked files in the stash. For example, run git stash -u or git stash push -u to save all changes including untracked files.
Contents
- Basic Stashing with Untracked Files
- Advanced Stashing Options
- Alternative Approaches
- Common Scenarios and Solutions
- Best Practices
Basic Stashing with Untracked Files
The simplest way to stash both tracked changes and untracked files is to use the -u flag:
git stash -u
or
git stash --include-untracked
This command will:
- Save all changes to tracked files (modified, staged, and deleted)
- Include all untracked files (new files not yet added to Git)
- Leave your working directory clean
If you want to include ignored files as well (like .env files or build artifacts), use the -a or --all flag:
git stash -a
Note: When you use
git stash -u, Git creates a stash entry that contains both your tracked changes and untracked files. When you later apply this stash, both will be restored to your working directory.
Advanced Stashing Options
Using git stash push with options
The modern approach is to use git stash push with explicit options:
git stash push -u git stash push --include-untracked git stash push -a git stash push --all
Stashing specific untracked files
If you only want to stash specific untracked files (not all), you can:
- First add the untracked files to staging:
git add new_file.txt
- Then stash as usual:
git stash
Stashing with a custom message
You can also add a descriptive message to your stash:
git stash push -m "Work in progress: Feature implementation with new files" -u
Alternative Approaches
Method 1: Stash tracked files first, then handle untracked files
# Stash only tracked changes
git stash
# Move untracked files to a temporary location
mkdir temp_stash && mv untracked_file.txt temp_stash/
# Do your other work...
# Return and restore
git stash pop
mv temp_stash/untracked_file.txt .
rm -rf temp_stash
Method 2: Create a temporary commit
# Add and commit untracked files
git add untracked_file.txt
git commit -m "Temp commit for untracked files"
# Stash your other changes
git stash
# When you return
git stash pop
git reset HEAD~1 # Remove the temporary commit
Common Scenarios and Solutions
Scenario: You want to stash everything including ignored files
git stash --all git stash -a
Scenario: You want to stash only specific untracked files
git add specific_untracked_file.txt git stash
Scenario: You want to stash tracked changes but exclude certain untracked files
# Move files you want to keep out of the way
mkdir keep && mv important_new_file.txt keep/
# Stash everything else
git stash -u
# Restore your important file
mv keep/important_new_file.txt .
rm -rf keep/
Scenario: You want to see what’s in your stash before applying
# List all stashes
git stash list
# Show stash contents
git stash show -p stash@{0}
# Apply specific stash
git stash apply stash@{0}
Best Practices
-
Use descriptive messages: Always add meaningful messages to your stashes when working with untracked files:
bashgit stash push -m "WIP: Feature X with new config files" -u -
Regular cleanup: Periodically clean up old stashes you no longer need:
bashgit stash drop stash@{0} git stash clear # Remove all stashes -
Verify before applying: Always check what’s in a stash before applying it:
bashgit stash show -p
-
Use branch for complex changes: For major work with many untracked files, consider creating a temporary branch instead of stashing:
bashgit checkout -b temp-work-branch # Do your work git checkout main -
Configuration shortcut: Add an alias to your
~/.gitconfigfor easier stashing:ini[alias] stashu = stash push --include-untracked
The git stash -u command is the most straightforward solution for stashing both tracked changes and untracked files together. It’s particularly useful when you need to quickly save your work and switch contexts without worrying about losing untracked files.
Sources
- Official Git Documentation - git stash
- Atlassian Git Tutorial - Stashing
- GitHub Docs - Stashing Your Work
Conclusion
- Use
git stash -uorgit stash --include-untrackedto stash both tracked changes and untracked files together - For including ignored files as well, use
git stash -aorgit stash --all - Always add descriptive messages to your stashes when working with untracked files
- Consider alternative approaches like creating temporary branches for complex work involving many untracked files
- Regularly clean up old stashes to keep your repository tidy
The -u flag is the key solution for your specific problem - it allows you to stash everything in your working directory at once, keeping your workspace clean when switching between tasks.