Git Stash Can Delete Ignored Files (git stash -u)

If you’ve arrived here from a search engine, I only hope that this is a pre-emptive search on your behalf and that you haven’t lost work.

Sadly, I have.

The background – I’m using TortoiseGit on Windows, with msysgit sitting beneath it (a standard setup).  For the sake of this explanation, assume I have the following Git repository structure.

 --> my-first-tracked-file.txt

 --> my-untracked-file.txt

 --> my-ignored-file.txt


Recently, I ran the following command from TortoiseGit:  TortoiseGit -> Stash Save (Tick “Include Untracked Files”).

This is the equivalent of running git stash save -u (or git stash -u which defaults to a save) from an msysgit command line.

To cut a long story short, if you run git stash save -u (whether from TortoiseGit or the command line) and you have a directory which is untracked and not ignored, but which has files within it that are, the entire directory and all files will be removed.  Yikes!

To put it another way, if you have the following in your .gitignore:


Then you’re screwed – after a git stash save -umy-ignored-file.txt will be gone (and sorry, it’s not in the stash you just created either).  Break out the undelete packages and hope for the best.  Alternatively, if you have the following in your .gitignore:


Then you’re golden – no worries, kick back, relax, my-ignored-file.txt will stay exactly where it was prior to the git stash save -u.

Oh and one last tip.  If you actually want to stash your ignored files, git stash save -a is the command you’re looking for.  As far as I’m aware, TortoiseGit does not yet offer this option.


There are suggestions that this might be a bug in Git: msysgit: stash -u deletes ignored files

Which may have been experience by other TortoiseGit users: Ignored folders can be destroyed by TortoiseGit.

And may also have been experienced in the gitextensions tool: Stash removes ignored files

Though in that last link, the words “expected behaviour” are mentioned so I guess we’ll wait and see…!


It seems that this might be the behaviour of git clean -d (the -d indicating “delete untracked directories”).  git stash save -u issues a git clean command after stashing files.  If this command includes the `-d` option (as I suspect it does) then this is what causes the deletion of an untracked directory containing ignored files.

It seems that others have noticed the issue with git clean -d: Clean bug on ignored subdirectories with no tracked files

To summarise:

git clean -d will delete all untracked directories including those containing ignored files.

9 thoughts on “Git Stash Can Delete Ignored Files (git stash -u)

  1. Thank you, this will save a lot of time. In my project we were loosing our bin files every time we did a stash and we have our folders inside git ignore like ” folder/* “. Thank you a lot!!!

  2. Pingback: How to: How do you stash an untracked file? | SevenNet

  3. Pingback: How do you stash an untracked file? | Ngoding

  4. Pingback: Solution: How do you stash an untracked file? #dev #it #computers | Technical information for you

  5. Pingback: Fixed How do you stash an untracked file? #dev #it #asnwer | Good Answer

  6. Has this been fixed on later releases or is it the “expected behaviour”?
    My git version is 1.9.1 and it also deletes any ignored files not just ignored directories.

    • As you’re said executing git stash -a and then git stash apply re-applies everything and doesn’t delete ignored files/directories. But you don’t always want to stash “ignored files/directories”.

  7. git stash –include-untracked also deletes files which are ignored with a wildcard in the middle of a path.
    For example a file placed in clients/clientX/docs will be deleted by following irgnored path: clients/*/docs.
    So it doesn’t matter where the wildcard is placed, that is my experience. Even in git 2.4.0.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>