THE GITHTML FIELD GUIDE

Stash untracked files without overlooking ignored files

Git stash push normally saves changes to tracked files. Add --include-untracked to include new, untracked files, and use explicit paths when only part of the checkout belongs to your task. Ignored files are a separate category and are not included by that option. Inspect the stash before relying on it to protect important work.

Inventory what needs protection

A stash is a temporary local repository record, not an automatic off-machine backup. Before using it, list the files you need to preserve and distinguish modified tracked files, untracked files, and ignored files. A new test fixture can be untracked while a local database is ignored; the same stash option does not necessarily capture both.

For valuable or irreplaceable work, keep a separate safe copy as well. Stashing removes the selected changes from their working locations after recording them, so discovering that an essential file was not included can disrupt the task you were about to begin. Avoid broad all-files options merely because you want status to look clean; ignored directories may contain enormous caches or private data.

Save a named, scoped entry

The example preserves one edited source file and one new test file. Replace those paths with reviewed targets. A descriptive message helps identify the entry later, especially when several tasks have been interrupted. The show command includes untracked content in its inspection; without that option, a summary can omit the very files you wanted to verify.

  1. Confirm the named entry contains every intended path and inspect its patch when content matters, not only its filename summary.
  2. Check the working directory afterward. Remaining ignored or unrelated files may be expected and should not be deleted merely to achieve a clean display.
shell
git status --short --untracked-files=all
git stash push --include-untracked -m "retry fix before branch update" -- parser.js parser.test.js
git stash list
git stash show --include-untracked --stat 'stash@{0}'

Restore with verification and retain a fallback

When returning to the task, inspect the selected stash and apply it to an appropriate branch. Using apply retains the entry while you verify the restored files. A different base can produce conflicts, and pre-existing untracked paths can prevent restoration of saved files. Read the complete output and status rather than assuming every component was restored because some edits appeared.

Once the recovered work is checked, tested, and safely committed or otherwise preserved, remove only the specific stash you no longer need. Do not clear the entire stash list as routine housekeeping. Stash identifiers shift as entries are added or removed, so reread the list immediately before any drop operation. The dependable workflow is inventory, scoped save, content verification, careful restore, and only then deliberate cleanup.

Before switching tasks, confirm that any ignored local database or credential file you depend on has its own appropriate backup and access protection.

Sources and further reading

AI-assisted writing with source-linked guidance and illustrative examples. Read our editorial approach or report a correction.

All git fundamentals guides →

Read as MarkdownAll guides

Stash untracked files without overlooking ignored files · gitHtml