How to Stash Untracked Files in Git

Avatar

By squashlabs, Last Updated: Oct. 28, 2023

How to Stash Untracked Files in Git

Table of Contents

Method 1:

To stash untracked files in Git, you can use the "git stash" command with the "--include-untracked" or "-u" option. This allows you to save your untracked changes and remove them from your working directory temporarily. Here are the steps to stash untracked files in Git:

1. Check your Git status to identify any untracked files:

$ git status

2. Stage and commit any tracked files before stashing the untracked files:

$ git add <file>
$ git commit -m "Committing tracked files"

3. Stash the untracked files using the "--include-untracked" or "-u" option:

$ git stash save --include-untracked

4. Verify that the stash was created successfully:

$ git stash list

5. You can now switch branches or perform other operations without the untracked files interfering with your work.

6. To apply the stash and restore the untracked files later:

$ git stash apply stash@{0}

7. If you no longer need the stash, you can also drop it:

$ git stash drop stash@{0}

Related Article: How to Discard All Local Changes in a Git Project

Method 2:

Related Article: Fixing the Git Error: "Git Not Recognized As A Command"

Another way to stash untracked files in Git is by using the "git add" command with the "--intent-to-add" option. This allows you to add the untracked files to the Git index without committing them, effectively stashing them. Here's how you can do it:

1. Check your Git status to identify any untracked files:

$ git status

2. Add the untracked files to the Git index using the "--intent-to-add" option:

$ git add --intent-to-add <file>

3. Verify that the files are added to the index:

$ git status

4. You can now switch branches or perform other operations without the untracked files interfering with your work.

5. To restore the stashed files later, you can simply remove them from the index:

$ git reset HEAD <file>

6. If you want to completely remove the untracked files from your working directory, you can use the "git clean" command:

$ git clean -f <file>

Please note that stashing untracked files is useful when you want to temporarily save your changes without committing them. It allows you to switch branches or perform other operations without affecting your untracked files. However, it's important to remember that stashes are not meant to be a long-term solution for storing changes. It's recommended to commit your changes properly once they are ready.

It's also worth mentioning that stashing untracked files is different from stashing tracked files in Git. When you stash tracked files, you save both the changes and the staged changes, whereas stashing untracked files only saves the changes that have not been added to the Git index.

More Articles from the Git Tutorial: From Basics to Advanced Concepts series:

How to Remove a File From a Git Repository

Deleting files from a Git repository is a common task for software developers. This article provides two methods for removing files: using the git rm… read more

How To Use Git Reset Hard Head To Revert To A Previous Commit

Reverting to a previous commit in your Git repositories can be a simple and process using the git reset --hard HEAD command. This article will guide … read more

How To Handle Git Refusing To Merge Unrelated Histories On Rebase

Git refusing to merge unrelated histories on rebase can be a frustrating issue to encounter. This article provides possible answers and suggestions t… read more

How to Perform a Hard Reset of a Single File in Git

Executing a hard reset on a single file in Git is a useful skill for software engineers. This guide provides step-by-step instructions on how to perf… read more

How To Fix Git Error: Pre-Receive Hook Declined

Git is a powerful tool for version control, but it can sometimes throw errors like "pre-receive hook declined." In this article, we will explore the … read more

How to Move Recent Commits to a New Branch with Git

Guide to relocating recent commits to a new branch using Git commands. Learn two methods: using git branch and git cherry-pick commands, or using the… read more

How to Create and Checkout Git Remote Tags

Creating and checking out Git remote tags is a fundamental aspect of version control in software development. This article provides a simple guide on… read more

How to Download a Single Folder from a Github Repo

Downloading a single folder from a GitHub repository using Git can be done in two ways: using the GitHub website or using the Git command-line tool. … read more

Tutorial: HEAD in Git

A brief guide on using 'Head' in Git for version control. This article covers key aspects such as viewing the commit pointed by 'Head', moving 'Head'… read more

How to Fix a Git Detached Head

Solving the issue of a Git detached head in your repository can be a simple task. This article provides a guide with two methods to fix the problem. … read more