It can be difficult to manage untracked files in a Git repository. Particularly if you need to move branches or tidy up your working directory without committing these files. Git offers a potent workaround in the form of “stashing,” which lets you store and revisit your untracked changes later. Stowing untracked files away is a useful technique that will help you keep your Git workflow organized and productive. Apart, from accessing the contents of a compressed library file you need to learn to Unzip/Extract tar.gz Files in Linux with commands.

In Git, an untracked file has been created in the repository’s working directory but has not yet been added using the git add command to the repository’s tracking index. Git stashes untracked files by default unless specifically instructed to include them.

Requirements

  • Installed Git (use our instructions to install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8 if you don’t already have it installed).
  • A repository for Git.

How Can I View Git’s Untracked Files?

Apply the git status command to a repository to find untracked files:

git status


check untracked files in repository

The status of the files in the repository is displayed in the command output. Untracked files are indicated in red, and tracked files with changes prepared for commit are displayed in green.

How Are the Untracked Files Stash?

Untracked files can be stored in three different ways:

  • The command to stash git. From the working directory, the command stores modifications. Untracked files in the cache are included when the –include-untracked or -u option is used.
  • The command “git add.” By adding the files to the tracking index, the command enables users to store the files without requiring any more actions.
  • The option –all. Git stash –all stores all files, even files that are ignored and untracked.

Examples of stashing untracked files using each of the three approaches are shown in the sections below.

Method 1: Use the git stash command to show Untracked Files

To stash untracked files, use git stash and choose one of the two alternatives available:

1. Using the option –include-untracked

Git is instructed to store untracked files and other modifications in the working directory by using the –include-untracked option. Execute the subsequent command:

git stash --include-untracked

option instructs git to stash untracked files

All uncommitted changes from the working directory, including untracked files, are stored by the command.

To add a message and describe the Git stash, use the -m option. The messages make it easier to keep track of what’s in the stockpile. Here’s the syntax:

git stash --include-untracked -m "message"

2. Employing the -u Parameter

Git stashes untracked files and changes from the working directory together when using the -u option, which is a shorthand for the –include-untracked option.

git stash -u

command stashes all untracked files

All of the working directory’s untracked files and uncommitted modifications are stored by the command.

Method 2: Use Git Add to Store the Untracked Files

You can stash untracked files without giving them any extra settings by using the git add command, which adds them to the tracking index. Here’s the syntax:

git add [filename]

Give the name of the untracked file for [filename]. Alternatively, run the following to add every file to the tracking index:

git add

Git is instructed to track every file in the working directory by using the git stash command, which enables users to stash files.

Method 3: Store Every File

Files in the working directory are divided into three groups by Git:

  • Tracked documents. Every file or directory that Git is aware of.
  • Files not tracked. newly added, not yet staged files or directories in the working directory.
  • Files disregarded. folders or files that Git entirely disregards and excludes.

Git is instructed to store all uncommitted files, even ignored and untracked ones, in the repository when the –all option is used. To store every file, execute:

git stash --all

command stashes all tracked, untracked files in the repository

All tracked, untracked, and ignored files in the repository are stored by the command.

Examine the Stash Files

Use the following syntax to verify every tracked file in a stash after stashing:

git show -p stash@{n}

Give the stash index number for {n}.

command displays tracked files from specified stash

The command in the aforementioned example shows only the files that are tracked from the designated stash@{0}.

On the other hand, Git keeps the untracked files in a stash commit’s third parent. As a result, the output for tracked and untracked files differs.

Use the following syntax to view every file that has been stored untracked:

git show stash@{n}^3

For instance, run: to display untracked stashed files from stash@{0}

git show stash@{0}^3

output shows the untracked files in specified stash

The untracked files in the designated cache are displayed in the output.

Final Thoughts on How to Stash Untracked Files in Git

To sum up, Git’s untracked file storage feature is essential for maintaining a tidy workspace and a spotless version control history. Developing the skill of stashing can help you keep your development process more structured and efficient. Whether you need to switch branches mid-task or just wish to save your uncommitted work for a while.

This post demonstrated three distinct ways to stash untracked files in Git. Additionally, you discovered how to identify which files Git is tracking and examine the contents of a stash.

Frequently Asked Questions (FAQ)

1. What is Git Stash?

Git stash temporarily saves your local changes without dedicating them

2. How do I add Untracked Files in Git?

Using git add -i add Untracked Files in Git as it is also accomplished. It permits you to clean your working directory while maintaining the modifications to use afterward.