threads / discuss / 37189

.gitignore and git stash -u

Subject: .gitignore and git stash -u

## tl;dr

2 messages between Jul 22, 2014 and Jul 22, 2014.

replies: 1people: 2as markdown or json

Jeffry Johnston· Jul 22, 2014, 20:55 UTC · lore

We had been experiencing "random" deletions of files and directories, and we finally figured out what they were: git stash -u. A coworker happened upon a webpage (after losing a weeks worth of experimental work, back to his last backup), which described our problems exactly:

http://blog.icefusion.co.uk/git-stash-can-delete-ignored-files-git-stash-u/

The command git stash -u deletes ignored files from the hard drive.. very dangerous! Which leads me to my first question: Why does git stash -u delete these files but a checkout doesn't? It seems inconsistent.

We have the following in .gitignore, which is directly related to the problems we're having (please note, we're using git on Windows, in case that makes a syntax difference):

*/**/* ![Ss][Rr][Cc]/**/*

So what that does is ignores every file and subdirectory at the top level, and then un-ignores Src/ and all files/dirs under that (we have a few more that we unignore). The reason behind this is we have files that are generated in the same tree as the source code, and unfortunately it is infeasible to change that design. So, if someone creates Data and puts their own custom configuration file it in, it doesn't contaminate the repository. However, if course they don't want to lose their configuration file either.

My understanding from the linked page is that our problem is having the /* at the end which causes the files to be ignored and not the directories themselves. However, I couldn't seem to find a combination without the trailing /* that duplicated the functionality we had, but that prevented git stash from deleting our files. Can anyone provide me with one? This is what I am really after.

I have come up with an inelegant workaround. If I add this line to .gitignore:
!Data/.placeholder

And then commit an empty .placeholder file, it seems to fix the deletion problem. After that, we can place all sorts of untracked files and sub-directories and they don't get deleted during a stash. I guess it's because that tracked file keeps the directory "alive" in some fashion, but I am not sure I understand why the sub-directories are not deleted though, can anyone enlighten me on that? I would have expected to have to put a .placeholder file in every sub-directory under Data to prevent them from getting deleted too (because of the ** in the .gitignore line).

Another thing we were considering is modifying git itself to provide a command-line option not to delete ignored files during a stash. Can someone point me to the appropriate file?

Thanks a lot, Jeff

Junio C Hamano· Jul 22, 2014, 21:22 UTC · re: Jeffry Johnston · lore

Re: .gitignore and git stash -u

Jeffry Johnston <jeff@kidsquid.com> writes:
Show 9 quoted lines
> We had been experiencing "random" deletions of files and directories,
> and we finally figured out what they were: git stash -u.  A coworker
> happened upon a webpage (after losing a weeks worth of experimental
> work, back to his last backup), which described our problems exactly:
>
> http://blog.icefusion.co.uk/git-stash-can-delete-ignored-files-git-stash-u/
>
> The command git stash -u deletes ignored files from the hard drive..
> very dangerous!

Although I never use "stash --include-untracked" myself, I think it is deliberate that the command removes all the untracked files. The author of that "feature" explains its use this way:

commit 787513027a7d0af3c2cd2f04b85bc7136d580586
Author: David Caldwell <david@porkrind.org>
Date:   Fri Jun 24 17:56:06 2011 -0700
    stash: Add --include-untracked option to stash and remove all untracked files
    
    The --include-untracked option acts like the normal "git stash save" but
    also adds all untracked files in the working directory to the stash and then
    calls "git clean --force --quiet" to restore the working directory to a
    pristine state.
    
    This is useful for projects that need to run release scripts. With this
    option, the release scripts can be from the main working directory so one
    does not have to maintain a "clean" directory in parallel just for
    releasing. Basically the work-flow becomes:
    
       $ git tag release-1.0
       $ git stash --include-untracked
       $ make release
       $ git clean -f
       $ git stash pop
    
    "git stash" alone is not enough in this case--it leaves untracked files
    lying around that might mess up a release process that expects everything to
    be very clean or might let a release succeed that should actually fail (due
    to a new source file being created that hasn't been committed yet).
    
    Signed-off-by: David Caldwell <david@porkrind.org>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>

← back to recent threads