How to Git Stash Untracked Files?

Find out how to stash untracked and/or ignored files using git

By default, git stash only stashes modified and staged tracked files. If you wish to stash untracked files as well, depending on your requirements, you could use any one of the following:

Stashing Only Tracked and Untracked Files

By simply appending --include-untracked (or -u shorthand) to git stash command you can include all tracked and untracked files:

git stash --include-untracked
git stash -u

Note, however, that this will exclude explicitly ignored files (as defined in .gitignore). To stash ignored files as well, see how you can stash all files or use git add instead.

Stashing Untracked Files Using git add

You could simply git add the untracked files to start tracking it, followed by git stash to stash all tracked files like so:

git add path/to/untracked-file
git stash

By default, git add will exclude explicitly ignored files (as defined in .gitignore). You can, however, force git add on ignored files.

Stashing Ignored Files Using git add

You can use the --force (or -f shorthand) flag to allow adding ignored files like so:

git add --force path/to/ignored-file
git add -f path/to/ignored-file

Then to stash them you can do git stash right after, like so:

git stash

Stashing All Files (Tracked, Untracked and Ignored)

You can stash all files (which will include tracked, untracked and ignored files) by using the --all (or -a shorthand) flag like so:

git stash --all
git stash -a

Checking If File Is Being Ignored

A pattern within your .gitignore might sometimes be causing a file to be ignored when you're trying to git stash. In such cases, you can use the following code to verify and debug which pattern is causing the file to be ignored:

git check-ignore -v path/to/file

It is possible to pass multiple file names to git check-ignore.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.