How to Stash Untracked Files in Git

September 29, 2022

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.

How to stash untracked files in Git - a Guide.

Prerequisites

How to Check Untracked Files in Git?

Check for untracked files in a repository with the git status command:

git status
See tracked and untracked files in a Git repository.

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. The git 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
Include untracked files when stashing.

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
Stash untracked files using the -u option.

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
Stash all files in a repository - tracked, untracked, and ignored.

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:

Show tracked files in a stash.

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
Show untracked files in a stash.

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.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Name a Stash and Retrieve a Stash by Name in Git
September 27, 2022

This tutorial shows how to add a custom name or message to a stash, and later retrieve the stash using that name.
Read more
How to Restore a Git Stash
September 27, 2022

Git stashes allow you to continue working on unfinished code previously set aside. See two different methods for restoring an existing Git stash - using the pop or the apply command.
Read more
How to Use Git Stash
September 13, 2022

Git stash is a way of storing unfinished work locally, without having to commit the changes. This step-by-step tutorial shows how to create a Git stash and the basic use cases.
Read more
Git Submodule Guide & Basic Commands to Get Started
September 1, 2022

This guide provides a simple overview of Git submodules, focusing on the most frequently used commands and workflows.
Read more