How do you stash an untracked file?
Categories:
How to Stash Untracked Files in Git
Learn how to effectively use git stash
to temporarily save changes, including those from untracked files, without committing them.
Git's stash
command is an incredibly useful tool for developers, allowing you to temporarily shelve changes you're not ready to commit. This is particularly handy when you need to switch branches quickly for an urgent fix or to pull new updates, but your current working directory is messy. While git stash
by default only stores tracked files, there are specific options to include untracked and even ignored files in your stash.
Understanding Git Stash Basics
By default, git stash
saves your modified tracked files and staged changes. It essentially takes your working directory and index, records them, and then reverts your working directory to the HEAD commit. This leaves you with a clean working directory, ready for other tasks. However, new files that Git doesn't know about (untracked files) are not included in this default operation.
git stash
Stashes only tracked, modified files.
Stashing Untracked Files
When you have new files that haven't been added to the Git index (i.e., you haven't run git add
on them), they are considered untracked. To include these files in your stash, you need to use the -u
or --include-untracked
option with git stash
. This tells Git to not only stash your modified and staged changes but also any new files that are present in your working directory.
git stash -u
# Or the longer form:
git stash --include-untracked
Stashes tracked modified files and untracked files.
Flow of git stash -u
Stashing Ignored Files (and Untracked)
In some scenarios, you might even want to stash files that are explicitly listed in your .gitignore
file. This is less common but can be useful if you're experimenting with files that should generally be ignored but are temporarily relevant to your work. To include both untracked and ignored files in your stash, use the -a
or --all
option.
git stash -a
# Or the longer form:
git stash --all
Stashes tracked modified files, untracked files, and ignored files.
git stash -a
. Stashing ignored files might include sensitive or large generated files that you typically wouldn't want to save or restore. Always review your changes before stashing with this option.Restoring Stashed Changes
Once you've stashed your changes, you can bring them back to your working directory using git stash apply
or git stash pop
. apply
will reapply the changes but leave the stash entry in the stash list, allowing you to apply it multiple times. pop
will reapply the changes and remove the entry from the stash list.
git stash list # View your stashes
git stash apply # Apply the most recent stash
git stash pop # Apply and remove the most recent stash
git stash apply stash@\{1\} # Apply a specific stash
Commands to list and apply stashed changes.
git stash show
to see a summary of the changes, or git stash show -p
to see the full diff.Practical Example: Working with Untracked Files
Let's walk through a common scenario where you create a new file, make some changes to an existing one, and then need to stash everything.
1. Step 1
Initialize a new Git repository and create a tracked file: mkdir myrepo && cd myrepo && git init && echo 'initial content' > tracked.txt && git add . && git commit -m 'Initial commit'
2. Step 2
Create an untracked file: echo 'new untracked content' > untracked.txt
3. Step 3
Modify the tracked file: echo 'modified content' >> tracked.txt
4. Step 4
Check your status: git status
(you'll see both modified and untracked files)
5. Step 5
Stash all changes, including the untracked file: git stash -u
6. Step 6
Verify your working directory is clean: git status
(it should show a clean tree)
7. Step 7
List your stashes: git stash list
(you should see one stash entry)
8. Step 8
Reapply the stashed changes: git stash pop
9. Step 9
Check your status again: git status
(the modified and untracked files should be back)