W3docs

How to Stash an Untracked File in Git

Read this tutorial and solve the problem of stashing untracked files including ignored files in .gitignore. Find method presented by Git new versions.

The git stash command doesn't stash the untracked and ignored files by default. It is necessary to use additional options that we will explain below.

Stashing untracked files

To stash including the untracked files, it is necessary to run the <kbd class="highlighted">git stash</kbd> command with the <kbd class="highlighted">--include-untracked</kbd> option (same as <kbd class="highlighted">-u</kbd>) as follows:

git stash include untracked

git stash --include-untracked

Stashing ignored files

To stash the ignored files along with the untracked files, it is necessary to run the <kbd class="highlighted">git stash</kbd> command with the <kbd class="highlighted">--all</kbd> option:

git stash

git stash --all

Stashing Files

The git stash command shelves changes made to the working copy making it possible for you to do another work, then come back and re-apply them. If the <kbd class="highlighted">--include-untracked</kbd> option is run, all untracked files are also stashed and cleaned up with git clean making the working directory clean. If <kbd class="highlighted">--all</kbd> is used instead, ignored files are stashed and cleaned in addition to the untracked files.

Git Ignored Files

Git accepts files as one of the following: tracked, untracked, and ignored.

Ignored files are those that Git has been told to ignore. If you want to commit ignored files, you must either remove them from .gitignore or force-add them using git add -f <file>. Ignore rules are defined in a .gitignore file. Since Git has no dedicated ignore command, you must edit and commit this file manually to update ignore settings.