# git stash --include-untracked possible destructive behavior

5 messages from 2017-06-08 to 2017-06-09. Participants: Marc Strapetz, Joel Teichroeb, Brandon Williams.
Thread: https://gitlist.dev/t/46144

## Marc Strapetz, 2017-06-08 11:52

Subject: git stash --include-untracked possible destructive behavior
Message-ID: <2b695c73-9de2-5502-6d19-ba4ad0114978@syntevo.com>
URL: https://gitlist.dev/e/2b695c73-9de2-5502-6d19-ba4ad0114978%40syntevo.com

```
One of our users has just discovered a destructive behavior of git stash 
--include-untracked: ignored files within non-ignored directories will 
not be stashed as untracked files (expected), but will still be removed 
from the working tree. The problem is reproducible by following command 
sequence:

   $ git init
   $ echo "1" > file.txt
   $ mkdir Data
   $ echo "1" > Data/ignored.txt
   $ echo "Data/*" > .gitignore
   $ git add .
   $ git commit -m "initial import"
   $ echo "2" > file.txt
   $ git stash save --include-untracked
   Saved working directory and index state WIP on master: 6ce5303 initial
   import

   $ git stash show stash@{1}
    file.txt | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)

   $ ls Data
   ls: cannot access 'Data': No such file or directory

As you can see, after saving the stash, stash@{1} doesn't contain 
Data/ignored.txt, but the entire Data directory is still gone from the 
working tree. As a consequence, applying the stash won't bring back 
Data/ignored.txt, too.

   $ git stash apply
   On branch master
   Changes not staged for commit:
     (use "git add <file>..." to update what will be committed)
     (use "git checkout -- <file>..." to discard changes in working
     directory)

           modified:   file.txt

   no changes added to commit (use "git add" and/or "git commit -a")

   $ ls Data
   ls: cannot access 'Data': No such file or directory

Note that when changing .gitignore to "Data/" instead of "Data/*", 
--include-untracked works as expected.

Tested with: git version 2.13.0.windows.1

-Marc

```

## Joel Teichroeb, 2017-06-08 22:13

Subject: Re: git stash --include-untracked possible destructive behavior
Message-ID: <197975d0-7ec3-0ff4-9201-48226fd3b60c@teichroeb.net>
URL: https://gitlist.dev/e/197975d0-7ec3-0ff4-9201-48226fd3b60c%40teichroeb.net
In-Reply-To: <2b695c73-9de2-5502-6d19-ba4ad0114978@syntevo.com>

```
It looks like it's an issue with git clean, as that's what stash uses 
internally. You can try your same test, but replace git stash with "git 
clean -df" and it will delete Data. Maybe clean should check if there 
are any ignored files in an untracked directory and only remove it if -x 
is specified?

Joel

```

## Brandon Williams, 2017-06-08 22:29

Subject: Re: git stash --include-untracked possible destructive behavior
Message-ID: <20170608222949.GA121539@google.com>
URL: https://gitlist.dev/e/20170608222949.GA121539%40google.com
In-Reply-To: <197975d0-7ec3-0ff4-9201-48226fd3b60c@teichroeb.net>

```
On 06/08, Joel Teichroeb wrote:
> It looks like it's an issue with git clean, as that's what stash
> uses internally. You can try your same test, but replace git stash
> with "git clean -df" and it will delete Data. Maybe clean should
> check if there are any ignored files in an untracked directory and
> only remove it if -x is specified?
> 
> Joel

There was someone working on fixing clean, you can look at the patch
series at
https://public-inbox.org/git/20170523100937.8752-1-sxlijin@gmail.com/
though I'm unsure of the series' status.

-- 
Brandon Williams

```

## Joel Teichroeb, 2017-06-08 22:39

Subject: Re: Re: git stash --include-untracked possible destructive behavior
Message-ID: <e45eed21-f808-b2a8-a01b-78c18a9c343a@teichroeb.net>
URL: https://gitlist.dev/e/e45eed21-f808-b2a8-a01b-78c18a9c343a%40teichroeb.net
In-Reply-To: <20170608222949.GA121539@google.com>

```
Looks like that series was merged last week, fixing this issue.

Marc, the fix will probably be in git 2.14.

Joel

```

## Marc Strapetz, 2017-06-09 08:28

Subject: Re: git stash --include-untracked possible destructive behavior
Message-ID: <7bb9829b-21a4-bd9e-c6f1-521b808793ea@syntevo.com>
URL: https://gitlist.dev/e/7bb9829b-21a4-bd9e-c6f1-521b808793ea%40syntevo.com
In-Reply-To: <e45eed21-f808-b2a8-a01b-78c18a9c343a@teichroeb.net>

```
On 09.06.2017 00:39, Joel Teichroeb wrote:
> Looks like that series was merged last week, fixing this issue.
> 
> Marc, the fix will probably be in git 2.14.

Thanks, I can confirm that with current master "git clean" as well as 
"git stash" are working properly now (tested on Linux). I'm looking 
forward to version 2.14.

-Marc

```
