0
0
GitHow-ToBeginner · 3 min read

How to Stash Untracked Files in Git Quickly and Safely

To stash untracked files in Git, use git stash push -u or git stash push --include-untracked. This command saves both tracked and untracked files temporarily so you can work cleanly without losing changes.
📐

Syntax

The command to stash untracked files includes the -u or --include-untracked option. This tells Git to save untracked files along with tracked changes.

  • git stash push: Saves tracked changes.
  • -u or --include-untracked: Includes untracked files in the stash.
bash
git stash push -u
💻

Example

This example shows how to stash both tracked and untracked files, then list the stash entries to confirm the stash was created.

bash
echo "tracked change" > tracked.txt
mkdir new_folder
echo "untracked file" > new_folder/untracked.txt

git add tracked.txt

# Stash tracked and untracked files
git stash push -u -m "Save tracked and untracked"

# List stash entries
git stash list
Output
Saved working directory and index state On main: Save tracked and untracked stash@{0}: On main: Save tracked and untracked
⚠️

Common Pitfalls

Many users forget to add the -u option and only stash tracked files, leaving untracked files unstashed. Also, using git stash without push is legacy and may not include untracked files.

Wrong way (does not stash untracked files):

git stash

Right way (includes untracked files):

git stash push -u
📊

Quick Reference

CommandDescription
git stash pushStash tracked changes only
git stash push -uStash tracked and untracked files
git stash push -aStash all files including ignored ones
git stash listShow all stash entries
git stash popApply and remove the latest stash

Key Takeaways

Use git stash push -u to stash untracked files along with tracked changes.
Without -u, untracked files remain unstashed and can be lost if not saved.
Check your stash list with git stash list to confirm your changes are saved.
Avoid using the legacy git stash command without push for modern workflows.
Use git stash pop to restore stashed changes when ready.