Introduction
An untracked file in Git is a file created in the repository's working directory but not yet added to the repo's tracking index via the git add
command. By default, Git ignores untracked files when stashing unless explicitly told to include them.
In this tutorial, you will learn to stash untracked files in Git.
Prerequisites
- Git installed (if you don't have Git installed, follow our tutorials for installing Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
How to Check Untracked Files in Git?
Check for untracked files in a repository with the git status
command:
git status
The command output shows the status of the files in the repository. Tracked files with changes ready for committing are highlighted in green, while untracked files are highlighted in red.
How To Stash the Untracked Files?
There are three ways to stash untracked files:
- The
git stash
command. The command shelves changes from the working directory. Specifying the--include-untracked
or-u
option includes untracked files in the stash. - The
git add
command. The command adds the files to the tracking index, allowing users to stash them without additional options. - The
--all
option. Thegit stash --all
command stashes all files, including untracked and ignored files.
The sections below show examples of stashing untracked files using all three methods.
Method 1: Stash Untracked Files with the git stash Command
Use git stash
to stash untracked files by specifying one of the two available options:
1: Using the --include-untracked Option
The --include-untracked
option instructs Git to stash untracked files and other changes in the working directory. Run the following command:
git stash --include-untracked
The command stashes all uncommitted changes from the working directory, including untracked files.
Specify the -m
option to include a message and describe the Git stash. The messages help keep track of what the stash contains. The syntax is:
git stash --include-untracked -m "message"
2: Using the -u Option
The -u
option is a shorthand for the --include-untracked
option, and the result is the same - Git stashes the changes from the working directory along with untracked files.
git stash -u
The command stashes all the uncommitted changes and untracked files from the working directory.
Method 2: Stash the Untracked Files with Git Add
The git add
command adds untracked files to the tracking index, allowing you to stash them without specifying additional options. The syntax is:
git add [filename]
For [filename]
, specify the name of the untracked file. Alternatively, add all files to the tracking index by running:
git add .
The command instructs Git to track all the files in the working directory, allowing users to stash them using git stash
.
Method 3: Stash All Files
Git categorizes files in the working directory into three categories:
- Tracked files. All the files or directories Git knows about.
- Untracked files. New files or directories in the working directory that have not been staged yet.
- Ignored files. Files or directories that Git completely excludes and ignores.
The --all
option instructs Git to stash all uncommitted files in the repository, including untracked and ignored files. To stash all files, run:
git stash --all
The command stashes all tracked, untracked, and ignored files in the repository.
Check Files in Stash
After stashing, check all tracked files in a stash using the following syntax:
git show -p stash@{n}
- For
{n}
, specify the stash index number.
For example:
In the example above, the command displays only the tracked files from the specified stash@{0}
.
However, Git stores the untracked files in the third parent of a stash commit. Thus, the untracked files don't appear in the same output as tracked files.
To see all stashed untracked files, use the following syntax:
git show stash@{n}^3
For example, to show untracked stashed files from stash@{0}
, run:
git show stash@{0}^3
The output shows the untracked files in the specified stash.
Note: Learn how to remove untracked files in Git.
Conclusion
This tutorial showed how to stash untracked files in Git using three different methods. You also learned to check the contents of a stash and see which files Git is tracking.
For more Git tutorials, see how to use Git tags, stash a specific file in Git, or learn to drop a Git stash.