How to Stash Untracked Files in Git

Avatar

By squashlabs, Last Updated: October 27, 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:

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.

Related Article: How to Revert Multiple Git Commits

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

How To Name And Retrieve A Git Stash By Name

Naming and retrieving a Git stash by name is a fundamental skill for effective version control. This article provides a simple guide on how to accomplish this task. It... read more

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 command and using... read more

How to Remove Files From a Git Staging Area

Removing files from Git's staging area is a simple process that can help you manage your repository more efficiently. This article provides practical examples and... 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 git rebase command.... read more

How to Rename Both Local and Remote Git Branch Names

Renaming Git branch names locally and remotely can be done with ease using a few simple methods. This guide provides step-by-step instructions on how to achieve this... read more

How to Use Git Stash Apply Version

Using the command 'Git stash apply' with specific versions in Git allows you to manage your code changes effectively. This article will guide you through the steps of... read more