How to Stash an Untracked File in Git?
Stashing is a powerful feature in Git that allows developers to save their current work and switch contexts without committing to the repository. By default, Git’s stash command only saves tracked files. However, there are situations when you might need to stash untracked files as well. This article will guide you through the process of stashing untracked files in Git.
Understanding Git Stash
Before diving into the specifics of stashing untracked files, it’s essential to understand the basic functionality of git stash
. The git stash
command temporarily shelves (or “stashes”) changes in the working directory that haven’t yet been committed, so you can work on something else and then come back and reapply the stashed changes later.
Basic Stashing
A typical git stash
command stashes changes to tracked files in your working directory. Here’s how you use it:
git stash
This command will save your modified tracked files and staging area changes to a new stash and revert the working directory to match the HEAD commit.
Including Untracked Files
By default, git stash
ignores untracked files. To include untracked files in your stash, you need to use the --include-untracked
option (or -u
for short). This option tells Git to stash not only the changes to tracked files but also any untracked files.
git stash -u
Example 1:
Implementation to stash both tracked and untracked files using the -u option.
Step 1: You have untracked files in your working directory.
Step 2: Run the git stash -u command to stash changes.
Step 3: Check the status to confirm the stash.
Using git stash with -a or –all
The -a (or –all) option includes all changes, including untracked and ignored files. This is useful when you want to stash everything in your working directory.
git stash -a
or
git stash --all
Example 2:
Implementation to stash all files, including untracked and ignored files, using the -a option.
Step 1: You have untracked and ignored files in your working directory.
Step 2: Run the git stash -a command to stash all changes.
Step 3: Check the status to confirm the stash.
Manually Adding Untracked Files Before Stashing
This approach involves manually adding untracked files to the staging area before stashing. After stashing, you can reset the staged state if necessary. This method gives you more control over which files to stash.
git stash
Example 3:
Implementation to add untracked files before stashing.
Step 1: You have untracked files in your working directory.
Step 2: Stage the untracked files
Step 3: Stash the changes
Step 4: Reset the staged state (if needed)
Using git stash with –include-untracked
The –include-untracked option with git stash is designed to include untracked files in the stash. This is useful when you have new files that are not yet tracked by Git, but you want to temporarily save them along with your other changes.
git stash --include-untracked
Example 4:
Implementation to stash both tracked and untracked files using the –include-untracked option.
Step 1: You have untracked files in your working directory.
Step 2: Run the git stash –include-untracked command to stash changes.
Step 3: Check the status to confirm the stash.
Contact Us