How to Git Stash Untracked Files?

By default, git stash only stashes modified and staged tracked files. If you wish to also include untracked files then you could simply append the --include-untracked (or -u shorthand) flag to the git stash command:

git stash --include-untracked
git stash -u

This would include all tracked and untracked files.

Note, however, that this will exclude explicitly ignored files (as defined in .gitignore). You may force-add ignored files and then stash them, or alternatively, you may stash all files (including tracked, untracked and ignored files).

You can also use the git add command to start tracking the untracked files, and following that you can use the git stash command to stash all the tracked files, for example, in the following way:

git add path/to/untracked-file
git stash

Please note that by default, git add will exclude explicitly ignored files (as defined in .gitignore). You can, however, force-add ignored files and then stash them.

Sometimes a pattern within your .gitignore might be causing a file to be ignored when you're trying to git stash it. If you suspect that being the case, then you can find the pattern in .gitignore that's causing that file to be ignored.

This post was published (and was last revised ) by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.