Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I include untracked files when saving work in a Git stash?

Quick answer

Use git stash push -u to stash tracked changes along with untracked files. Git documents that this option also removes those untracked files from the working tree. Afterward, inspect the entry with git stash show --include-untracked. A plain stash does not include untracked files, and the inspection command needs its include-untracked option to display them. This does not include ignored files.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I ran git stash, but a new file I had created is still there. How do I include it?

Geez

A plain git stash is equivalent to git stash push; it does not include untracked files. In a disposable local repository, use git stash push -u. The -u option, also written --include-untracked, adds untracked files to the stash and then cleans them from the working tree. [S1]

For example, suppose you have a tracked file you edited and a new, untracked draft file. Run:

git stash push -u

The untracked files covered by the command are removed from the working tree. If you only meant to save tracked changes and leave new files in place, don’t add -u.

Follow-up question

How can I check that the new file actually went into the stash?

Geez

Inspect the saved entry with git stash show --include-untracked. Git documents that git stash show may not display untracked contents unless you pass --include-untracked (or its short form, -u). [S1]

In a disposable repository, the basic sequence is:

git stash push -u
git stash show --include-untracked

Compare the displayed contents with the files you intended to save, especially the name of the new file. Without the include-untracked option, the display may not show untracked contents. Review the output rather than assuming the stash contains exactly the files you intended.

Follow-up question

I also have a file I think is ignored. Does -u cover that too?

Geez

No. The documented -u option includes untracked files, while --all is broader: it includes ignored as well as untracked files. [S1] Since your goal is to include untracked files, use -u rather than expanding the command to --all by default.

Decide separately whether the ignored file should be part of this saved work. The source documents the scope of --all, but it does not establish whether any particular ignored file is necessary or appropriate to save. For this guide’s limited task, keep ignored files outside the stash unless you have a specific reason to include them.

Follow-up question

Can you give me a quick coverage check I can use in a disposable local repository?

Geez

Use this checklist before relying on the stash:

  1. Identify the intended changes in tracked files and the intended untracked files.
  2. Decide separately whether any ignored files should be included; -u does not include them.
  3. In the disposable repository, run git stash push -u to include tracked changes and untracked files.
  4. Inspect the saved entry with git stash show --include-untracked and compare its displayed contents with your intended file list.
  5. If you expected an ignored file to be included, don’t treat a -u stash as covering it. The broader --all option includes ignored files as well as untracked files. [S1]

Worked example: assume the tracked edit and untracked draft are the two items you want, and an ignored file is out of scope. Run git stash push -u, then inspect with git stash show --include-untracked. Check the output for the tracked edit and draft, and do not count the ignored file as included. This is a coverage check, not a reason to assume files were saved without reviewing the output.

A practical checklist

  • List the tracked edits and untracked files you intend to save.

  • Decide whether ignored files are out of scope; -u does not include them.

  • In a disposable local repository, run git stash push -u for tracked changes plus untracked files.

  • Inspect with git stash show --include-untracked and compare the output with your intended file list.

  • Treat --all as a broader choice because it includes ignored files as well as untracked files.

A few more useful answers

Why doesn’t plain `git stash` pick up a new file?

A plain stash is equivalent to git stash push; the documented -u or --include-untracked option is what adds untracked files. [S1]

Why use `--include-untracked` when showing a stash?

Without that option, the show command may not display untracked contents. Use git stash show --include-untracked when checking coverage. [S1]

Does `git stash push -u` include ignored files?

No. Git documents --all as including ignored files as well as untracked ones; that is broader than -u. [S1]

Sources

References used for this guide.

  1. [S1] Git Reference: git-stash
    git-scm.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work