{"id":1226,"date":"2023-06-16T06:42:33","date_gmt":"2023-06-16T06:42:33","guid":{"rendered":"https:\/\/www.dedicatedcore.com\/blog\/?p=1226"},"modified":"2024-10-28T06:14:54","modified_gmt":"2024-10-28T06:14:54","slug":"stash-untracked-file-git","status":"publish","type":"post","link":"https:\/\/www.dedicatedcore.com\/blog\/stash-untracked-file-git\/","title":{"rendered":"How to Stash an Untracked File in Git"},"content":{"rendered":"<p>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 &#8220;stashing,&#8221; 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 <a href=\"https:\/\/www.dedicatedcore.com\/blog\/extract-unzip-tar-gz-file-linux-command\/\" target=\"_blank\" rel=\"noopener\">accessing the contents<\/a> of a compressed library file you need to learn to Unzip\/Extract tar.gz Files in Linux with commands.<\/p>\n<p>In Git, an untracked file has been created in the repository&#8217;s working directory but has not yet been added using the git add command to the repository&#8217;s tracking index. Git stashes untracked files by default unless specifically instructed to include them.<\/p>\n<p><strong>Requirements<\/strong><\/p>\n<ul>\n<li>Installed Git (use our instructions to install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8 if you don&#8217;t already have it installed).<\/li>\n<li>A repository for Git.<\/li>\n<\/ul>\n<h2>How Can I View Git&#8217;s Untracked Files?<\/h2>\n<p>Apply the git status command to a repository to find untracked files:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git status<\/pre>\n<p><strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1229\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-status-command.png\" alt=\"check untracked files in repository\" width=\"800\" height=\"270\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-status-command.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-status-command-300x101.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-status-command-150x51.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-status-command-768x259.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-status-command-100x34.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-status-command-700x236.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>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.<\/p>\n<p>How Are the Untracked Files Stash?<\/p>\n<p>Untracked files can be stored in three different ways:<\/p>\n<ul>\n<li>The command to stash git. From the working directory, the command stores modifications. Untracked files in the cache are included when the &#8211;include-untracked or -u option is used.<\/li>\n<li>The command &#8220;git add.&#8221; By adding the files to the tracking index, the command enables users to store the files without requiring any more actions.<\/li>\n<li>The option &#8211;all. Git stash &#8211;all stores all files, even files that are ignored and untracked.<\/li>\n<\/ul>\n<p>Examples of stashing untracked files using each of the three approaches are shown in the sections below.<\/p>\n<h3>Method 1: Use the git stash command to show Untracked Files<\/h3>\n<p>To stash untracked files, use git stash and choose one of the two alternatives available:<\/p>\n<h4>1. Using the option &#8211;include-untracked<\/h4>\n<p>Git is instructed to store untracked files and other modifications in the working directory by using the &#8211;include-untracked option. Execute the subsequent command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash --include-untracked<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1230\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-include-untracked-files.png\" alt=\"option instructs git to stash untracked files\" width=\"800\" height=\"189\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-include-untracked-files.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-include-untracked-files-300x71.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-include-untracked-files-150x35.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-include-untracked-files-768x181.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-include-untracked-files-100x24.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-include-untracked-files-700x165.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>All uncommitted changes from the working directory, including untracked files, are stored by the command.<\/p>\n<p>To add a message and describe the Git stash, use the -m option. The messages make it easier to keep track of what&#8217;s in the stockpile. Here&#8217;s the syntax:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash --include-untracked -m \"message\"<\/pre>\n<h4>2. Employing the -u Parameter<\/h4>\n<p>Git stashes untracked files and changes from the working directory together when using the -u option, which is a shorthand for the &#8211;include-untracked option.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash -u<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1231\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-u.png\" alt=\"command stashes all untracked files\" width=\"800\" height=\"171\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-u.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-u-300x64.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-u-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-u-768x164.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-u-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-u-700x150.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>All of the working directory&#8217;s untracked files and uncommitted modifications are stored by the command.<\/p>\n<h3>Method 2: Use Git Add to Store the Untracked Files<\/h3>\n<p>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&#8217;s the syntax:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git add [filename]<\/pre>\n<p>Give the name of the untracked file for [filename]. Alternatively, run the following to add every file to the tracking index:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git add<\/pre>\n<p>Git is instructed to track every file in the working directory by using the git stash command, which enables users to stash files.<\/p>\n<h3>Method 3: Store Every File<\/h3>\n<p>Files in the working directory are divided into three groups by Git:<\/p>\n<ul>\n<li>Tracked documents. Every file or directory that Git is aware of.<\/li>\n<li>Files not tracked. newly added, not yet staged files or directories in the working directory.<\/li>\n<li>Files disregarded. folders or files that Git entirely disregards and excludes.<\/li>\n<\/ul>\n<p>Git is instructed to store all uncommitted files, even ignored and untracked ones, in the repository when the &#8211;all option is used. To store every file, execute:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git stash --all<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1232\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-all.png\" alt=\"command stashes all tracked, untracked files in the repository\" width=\"800\" height=\"169\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-all.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-all-300x63.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-all-150x32.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-all-768x162.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-all-100x21.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-stash-all-700x148.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>All tracked, untracked, and ignored files in the repository are stored by the command.<\/p>\n<h4>Examine the Stash Files<\/h4>\n<p>Use the following syntax to verify every tracked file in a stash after stashing:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git show -p stash@{n}<\/pre>\n<p>Give the stash index number for {n}.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1233\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-p-stash.png\" alt=\"command displays tracked files from specified stash\" width=\"800\" height=\"311\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-p-stash.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-p-stash-300x117.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-p-stash-150x58.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-p-stash-768x299.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-p-stash-100x39.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-p-stash-700x272.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The command in the aforementioned example shows only the files that are tracked from the designated stash@{0}.<\/p>\n<p>On the other hand, Git keeps the untracked files in a stash commit&#8217;s third parent. As a result, the output for tracked and untracked files differs.<\/p>\n<p>Use the following syntax to view every file that has been stored untracked:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git show stash@{n}^3<\/pre>\n<p>For instance, run: to display untracked stashed files from stash@{0}<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">git show stash@{0}^3<\/pre>\n<p><strong><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1234\" src=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-stash.png\" alt=\"output shows the untracked files in specified stash\" width=\"800\" height=\"310\" srcset=\"https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-stash.png 800w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-stash-300x116.png 300w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-stash-150x58.png 150w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-stash-768x298.png 768w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-stash-100x39.png 100w, https:\/\/www.dedicatedcore.com\/blog\/wp-content\/uploads\/git-show-stash-700x271.png 700w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/strong><\/p>\n<p>The untracked files in the designated cache are displayed in the output.<\/p>\n<h4>Final Thoughts on How to Stash Untracked Files in Git<\/h4>\n<p>To sum up, Git&#8217;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.<\/p>\n<p>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.<\/p>\n<h3>Frequently Asked Questions (FAQ)<\/h3>\n<p><strong>1. What is Git Stash?<\/strong><\/p>\n<p>Git stash temporarily saves your local changes without dedicating them<\/p>\n<p><strong>2. How do I add Untracked Files in Git?<\/strong><\/p>\n<p>Using <strong>git add -i<\/strong> 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1252,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/1226"}],"collection":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/comments?post=1226"}],"version-history":[{"count":7,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/1226\/revisions"}],"predecessor-version":[{"id":2513,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/posts\/1226\/revisions\/2513"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media\/1252"}],"wp:attachment":[{"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/media?parent=1226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/categories?post=1226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dedicatedcore.com\/blog\/wp-json\/wp\/v2\/tags?post=1226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}