Categories
git git-stash

How do you stash an untracked file?

2026

I have changes to a file, plus a new file, and would like to use git stash to put them away while I switch to another task. But git stash by itself stashes only the changes to the existing file; the new file remains in my working tree, cluttering up my future work. How do I stash this untracked file?

1

  • 58

    Be aware that if you only have untracked files in your stash, it will look like it’s an empty one, as in git stash show returns nothing and you may be tempted to drop it (as I just did while it contained a useful script I wrote some months ago → how to recover a dropped stash)

    – Maxime R.

    Jun 2, 2016 at 9:59

2894

To stash your working directory including untracked files (especially those that are in the .gitignore) then you probably want to use this cmd:

git stash --include-untracked

Alternatively, you can use the shorthand -u instead of --include-untracked, or simply git stash --all which stashes all files, including untracked and ignored files. This bahaviour changed in 2018, so make sure your git is up to date.


Warning: there seems to be (or have been) situations in which contents of ignored directories could be deleted permanently. See this archived website for more information.

24

  • 5

    Why does stash still stash changed existing files even though those changes haven’t been staged?

    Jun 9, 2011 at 3:22

  • 7

    @alan-christensen Read the DESCRIPTION of kernel.org/pub/software/scm/git/docs/git-stash.html. The point is to have a clean working tree after stashing.

    – Kelvin

    Aug 16, 2011 at 19:44

  • 5

    @Kelvin what I meant in my comment was that it doesn’t stash new files unless they have been staged however it does stash existing files even if they haven’t been staged. Seems inconsistent to me.

    Aug 29, 2011 at 1:33

  • 15

    @AlanChristensen the point is to stash things that might be overwritten by checkout of a different branch.

    – jwg

    Jan 4, 2013 at 15:07

  • 27

    As you have the top answer here, I would request that you list git stash --include-untracked before git stash --all in your answer for two reasons. First it answers the OP’s question better now in 2019 and second because –all does something that most users probably don’t want in that it removes all of the files which are .gitignored

    Apr 15, 2019 at 22:26

478

As of git 1.7.7, git stash accepts the --include-untracked option (or short-hand -u). To include untracked files in your stash, use either of the following commands:

git stash --include-untracked
# or
git stash -u

Warning, doing this will permanently delete your files if you have any directory/ entries in your gitignore file.*

9

  • 17

    Cool – it finally works as described in the manual page. Not stashing (and cleaning) new files is broken behaviour.

    Feb 20, 2012 at 6:14

  • 1

    my version of git is 1.9.1 and even if what i have in .gitignore looks like this ignoredDirectory and not ignoredDirectory/* it still deletes those untracked. Even untracked files not just directories.

    Apr 13, 2015 at 10:01

  • 29

    Can you please explain the warning? Why would it delete those files? Does it delete them and not stash them? I’ve used Git for a while and haven’t run into this problem.

    Oct 26, 2015 at 21:10

  • Does the warning apply to *.extension entries as well?

    – arekolek

    Mar 8, 2017 at 10:47

  • 6

    @aleksandr-dubinsky, @arekolek – git 1.8.3 -u (--include-untracked) option can stash and pop untracked files okay but git stash show does not list the untracked files that are in the stash

    – xilef

    May 12, 2017 at 12:26


110

Add the file to the index:

git add path/to/untracked-file
git stash

The entire contents of the index, plus any unstaged changes to existing files, will all make it into the stash.

7

  • What if you don’t want to stash changes that are already in the index? Is stashing the new file still possible?

    Dec 5, 2010 at 8:36

  • Commit the index, stash the new file, then revert the commit and/or check the files out from the commit. It’s a kludgy solution, but it should work.

    – Gdalya

    May 7, 2012 at 14:58

  • git add . wasn’t taking it into consideration for some reason

    Aug 14, 2018 at 6:38

  • 2

    Better answer because --include-untracked will pick up everything, esp hidden directories; like node_modules or .pytest_cache and whatever stuff you have lying around which is not in .gitignore.

    – John Mee

    Sep 3, 2020 at 23:46

  • 1

    @Eggon after you pop/apply the stashed changes, git reset will clear changes to the index, but leave them in the working tree, because the default mode is --mixed.

    – Walf

    Jun 15 at 5:28