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-tracked-directory/
 --> my-first-tracked-file.txt

./my-untracked-directory/
 --> my-untracked-file.txt

./my-ignored-directory/
 --> my-ignored-file.txt

./my-second-tracked-file.txt
./.gitignore

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:

/my-ignored-directory/*

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:

/my-ignored-directory

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.

UPDATE:

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…!

UPDATE 2:

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.

2 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: Fixed How do you stash an untracked file? #dev #it #asnwer | Good Answer

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>