NeuroAgent

How to Stash Untracked Files in Git: Complete Guide

Learn how to stash untracked files in Git using the -u flag. Complete guide to saving both tracked changes and new files together, keeping your workspace clean.

Question

How do I stash an untracked file in Git?

I have changes to an existing file plus a new untracked file in my Git repository. I want to use git stash to temporarily save these changes while I switch to another task. However, git stash by default only stashes changes to tracked files, leaving the new untracked file in my working directory. This creates clutter when I return to my original task. What is the proper way to stash both tracked changes and untracked files together in Git?

NeuroAgent

To stash untracked files in Git, you need to use the -u (or --include-untracked) flag with the git stash command. This tells Git to include both tracked changes and untracked files in the stash. For example, run git stash -u or git stash push -u to save all changes including untracked files.

Contents

Basic Stashing with Untracked Files

The simplest way to stash both tracked changes and untracked files is to use the -u flag:

bash
git stash -u

or

bash
git stash --include-untracked

This command will:

  • Save all changes to tracked files (modified, staged, and deleted)
  • Include all untracked files (new files not yet added to Git)
  • Leave your working directory clean

If you want to include ignored files as well (like .env files or build artifacts), use the -a or --all flag:

bash
git stash -a

Note: When you use git stash -u, Git creates a stash entry that contains both your tracked changes and untracked files. When you later apply this stash, both will be restored to your working directory.

Advanced Stashing Options

Using git stash push with options

The modern approach is to use git stash push with explicit options:

bash
git stash push -u
git stash push --include-untracked
git stash push -a
git stash push --all

Stashing specific untracked files

If you only want to stash specific untracked files (not all), you can:

  1. First add the untracked files to staging:
bash
git add new_file.txt
  1. Then stash as usual:
bash
git stash

Stashing with a custom message

You can also add a descriptive message to your stash:

bash
git stash push -m "Work in progress: Feature implementation with new files" -u

Alternative Approaches

Method 1: Stash tracked files first, then handle untracked files

bash
# Stash only tracked changes
git stash

# Move untracked files to a temporary location
mkdir temp_stash && mv untracked_file.txt temp_stash/

# Do your other work...

# Return and restore
git stash pop
mv temp_stash/untracked_file.txt .
rm -rf temp_stash

Method 2: Create a temporary commit

bash
# Add and commit untracked files
git add untracked_file.txt
git commit -m "Temp commit for untracked files"

# Stash your other changes
git stash

# When you return
git stash pop
git reset HEAD~1  # Remove the temporary commit

Common Scenarios and Solutions

Scenario: You want to stash everything including ignored files

bash
git stash --all
git stash -a

Scenario: You want to stash only specific untracked files

bash
git add specific_untracked_file.txt
git stash

Scenario: You want to stash tracked changes but exclude certain untracked files

bash
# Move files you want to keep out of the way
mkdir keep && mv important_new_file.txt keep/

# Stash everything else
git stash -u

# Restore your important file
mv keep/important_new_file.txt .
rm -rf keep/

Scenario: You want to see what’s in your stash before applying

bash
# List all stashes
git stash list

# Show stash contents
git stash show -p stash@{0}

# Apply specific stash
git stash apply stash@{0}

Best Practices

  1. Use descriptive messages: Always add meaningful messages to your stashes when working with untracked files:

    bash
    git stash push -m "WIP: Feature X with new config files" -u
    
  2. Regular cleanup: Periodically clean up old stashes you no longer need:

    bash
    git stash drop stash@{0}
    git stash clear  # Remove all stashes
    
  3. Verify before applying: Always check what’s in a stash before applying it:

    bash
    git stash show -p
    
  4. Use branch for complex changes: For major work with many untracked files, consider creating a temporary branch instead of stashing:

    bash
    git checkout -b temp-work-branch
    # Do your work
    git checkout main
    
  5. Configuration shortcut: Add an alias to your ~/.gitconfig for easier stashing:

    ini
    [alias]
    stashu = stash push --include-untracked
    

The git stash -u command is the most straightforward solution for stashing both tracked changes and untracked files together. It’s particularly useful when you need to quickly save your work and switch contexts without worrying about losing untracked files.

Sources

  1. Official Git Documentation - git stash
  2. Atlassian Git Tutorial - Stashing
  3. GitHub Docs - Stashing Your Work

Conclusion

  • Use git stash -u or git stash --include-untracked to stash both tracked changes and untracked files together
  • For including ignored files as well, use git stash -a or git stash --all
  • Always add descriptive messages to your stashes when working with untracked files
  • Consider alternative approaches like creating temporary branches for complex work involving many untracked files
  • Regularly clean up old stashes to keep your repository tidy

The -u flag is the key solution for your specific problem - it allows you to stash everything in your working directory at once, keeping your workspace clean when switching between tasks.