# [BUG] git stash --keep-index undeletes file

4 messages from 2019-04-16 to 2019-04-16. Participants: Christian Vetter, Thomas Gummerer.
Thread: https://gitlist.dev/t/50942

## Christian Vetter, 2019-04-16 18:04

Subject: [BUG] git stash --keep-index undeletes file
Message-ID: <1555437849815.60450@rasenplanscher.info>
URL: https://gitlist.dev/e/1555437849815.60450%40rasenplanscher.info

```
Steps to reproduce
+ mkdir git-stash-test
+ cd git-stash-test
+ git init
+ touch file
+ git add file
+ git commit -m init
+ git rm file
+ git stash push --keep-index
 
 
Result: 
`git status` gives this output:
> On branch master
> Changes to be committed:
>   (use "git reset HEAD <file>..." to unstage)
> 
>         deleted:    file
> 
> Untracked files:
>   (use "git add <file>..." to include in what will be committed)
> 
>         file
 
 
Expected: 
`git status` gives this output:
> On branch master
> Changes to be committed:
>   (use "git reset HEAD <file>..." to unstage)
> 
>         deleted:    file
 
 
Verified on:
+ Windows 10, git version 2.21.0.windows.1 
+ Ubuntu 14.04, git version 2.21.0

```

## Thomas Gummerer, 2019-04-16 18:48

Subject: Re: [BUG] git stash --keep-index undeletes file
Message-ID: <20190416184816.GU16414@hank.intra.tgummerer.com>
URL: https://gitlist.dev/e/20190416184816.GU16414%40hank.intra.tgummerer.com
In-Reply-To: <1555437849815.60450@rasenplanscher.info>

```
On 04/16, Christian Vetter wrote:
> Steps to reproduce
> + mkdir git-stash-test
> + cd git-stash-test
> + git init
> + touch file
> + git add file
> + git commit -m init
> + git rm file
> + git stash push --keep-index
>  
>  
> Result: 
> `git status` gives this output:
> > On branch master
> > Changes to be committed:
> >   (use "git reset HEAD <file>..." to unstage)
> > 
> >         deleted:    file
> > 
> > Untracked files:
> >   (use "git add <file>..." to include in what will be committed)
> > 
> >         file

I think this is the correct result.  --keep-index keeps the changes
that are in the index, in this example that 'file' is deleted, but
restores the working tree to 'HEAD', where 'file' existed.  It's now
showing up as untracked because Git doesn't know about it.

Similarly if you just did 'git stash push' without --keep-index,
'file' would be restored in your working tree, but 'git status' would
report the working tree as clean because it would also be restored in
the index.

> Expected: 
> `git status` gives this output:
> > On branch master
> > Changes to be committed:
> >   (use "git reset HEAD <file>..." to unstage)
> > 
> >         deleted:    file

This is what you get before 'git stash push --keep-index'.  Do you
think the documentation on '--keep-index' is unclear, and could be
improved?  Or why did you think that this is the correct output?

> Verified on:
> + Windows 10, git version 2.21.0.windows.1 
> + Ubuntu 14.04, git version 2.21.0

```

## Christian Vetter, 2019-04-16 19:57

Subject: Re: [BUG] git stash --keep-index undeletes file
Message-ID: <1555444649793.14863@rasenplanscher.info>
URL: https://gitlist.dev/e/1555444649793.14863%40rasenplanscher.info
In-Reply-To: <20190416184816.GU16414@hank.intra.tgummerer.com>

```
Thank you, Thomas!

Now that you explained this, I do understand the behaviour, and agree that it is correct.
While this seems to be clear with a deeper understanding of git than I have,
it might be a good idea to specifically point this out in the docs in the context of `--keep-index`.
That would have helped me.

This does, however, bring me back to a related issue by which I stumbled across this:
If, in the given scenario, I run `git stash apply`, the file is not removed. `git status` still outputs this:
> On branch master
> Changes to be committed:
>   (use "git reset HEAD <file>..." to unstage)
>
>         deleted:    file
>
> Untracked files:
>   (use "git add <file>..." to include in what will be committed)
>
>         file

Can you explain that behaviour as well?
The way I understand it, `git stash push --keep-index; git stash apply` should result in zero change to the output of `git status`.
What am I not getting?


________________________________________
From: Thomas Gummerer <t.gummerer@gmail.com>
Sent: Tuesday, April 16, 2019 20:48
To: Christian Vetter
Cc: git@vger.kernel.org
Subject: Re: [BUG] git stash --keep-index undeletes file

On 04/16, Christian Vetter wrote:
> Steps to reproduce
> + mkdir git-stash-test
> + cd git-stash-test
> + git init
> + touch file
> + git add file
> + git commit -m init
> + git rm file
> + git stash push --keep-index
>
>
> Result:
> `git status` gives this output:
> > On branch master
> > Changes to be committed:
> >   (use "git reset HEAD <file>..." to unstage)
> >
> >         deleted:    file
> >
> > Untracked files:
> >   (use "git add <file>..." to include in what will be committed)
> >
> >         file

I think this is the correct result.  --keep-index keeps the changes
that are in the index, in this example that 'file' is deleted, but
restores the working tree to 'HEAD', where 'file' existed.  It's now
showing up as untracked because Git doesn't know about it.

Similarly if you just did 'git stash push' without --keep-index,
'file' would be restored in your working tree, but 'git status' would
report the working tree as clean because it would also be restored in
the index.

> Expected:
> `git status` gives this output:
> > On branch master
> > Changes to be committed:
> >   (use "git reset HEAD <file>..." to unstage)
> >
> >         deleted:    file

This is what you get before 'git stash push --keep-index'.  Do you
think the documentation on '--keep-index' is unclear, and could be
improved?  Or why did you think that this is the correct output?

> Verified on:
> + Windows 10, git version 2.21.0.windows.1
> + Ubuntu 14.04, git version 2.21.0

```

## Thomas Gummerer, 2019-04-16 21:59

Subject: Re: [BUG] git stash --keep-index undeletes file
Message-ID: <20190416215945.GV16414@hank.intra.tgummerer.com>
URL: https://gitlist.dev/e/20190416215945.GV16414%40hank.intra.tgummerer.com
In-Reply-To: <1555444649793.14863@rasenplanscher.info>

```
[please don't top post on this list]

On 04/16, Christian Vetter wrote:
> Thank you, Thomas!
> 
> Now that you explained this, I do understand the behaviour, and agree that it is correct.
> While this seems to be clear with a deeper understanding of git than I have,
> it might be a good idea to specifically point this out in the docs in the context of `--keep-index`.
> That would have helped me.

On second thought, I might have been too quick to jump to conclusions
here, and might have misunderstood --keep-index myself.  I think what
you described here is actually a bug, although it's a pretty long
standing one.  So I'm a bit surprised nobody noticed it before.

Thanks for reporting it.

> This does, however, bring me back to a related issue by which I stumbled across this:
> If, in the given scenario, I run `git stash apply`, the file is not removed. `git status` still outputs this:
> > On branch master
> > Changes to be committed:
> >   (use "git reset HEAD <file>..." to unstage)
> >
> >         deleted:    file
> >
> > Untracked files:
> >   (use "git add <file>..." to include in what will be committed)
> >
> >         file
> 
> Can you explain that behaviour as well?
> The way I understand it, `git stash push --keep-index; git stash apply` should result in zero change to the output of `git status`.
> What am I not getting?

Your interpretation of what should happen sounds like the right
behaviour here, and what actually happens seems incorrect.  I don't
have time to dig into this more right now though unfortunately.

> ________________________________________
> From: Thomas Gummerer <t.gummerer@gmail.com>
> Sent: Tuesday, April 16, 2019 20:48
> To: Christian Vetter
> Cc: git@vger.kernel.org
> Subject: Re: [BUG] git stash --keep-index undeletes file
> 
> On 04/16, Christian Vetter wrote:
> > Steps to reproduce
> > + mkdir git-stash-test
> > + cd git-stash-test
> > + git init
> > + touch file
> > + git add file
> > + git commit -m init
> > + git rm file
> > + git stash push --keep-index
> >
> >
> > Result:
> > `git status` gives this output:
> > > On branch master
> > > Changes to be committed:
> > >   (use "git reset HEAD <file>..." to unstage)
> > >
> > >         deleted:    file
> > >
> > > Untracked files:
> > >   (use "git add <file>..." to include in what will be committed)
> > >
> > >         file
> 
> I think this is the correct result.  --keep-index keeps the changes
> that are in the index, in this example that 'file' is deleted, but
> restores the working tree to 'HEAD', where 'file' existed.  It's now
> showing up as untracked because Git doesn't know about it.
> 
> Similarly if you just did 'git stash push' without --keep-index,
> 'file' would be restored in your working tree, but 'git status' would
> report the working tree as clean because it would also be restored in
> the index.
> 
> > Expected:
> > `git status` gives this output:
> > > On branch master
> > > Changes to be committed:
> > >   (use "git reset HEAD <file>..." to unstage)
> > >
> > >         deleted:    file
> 
> This is what you get before 'git stash push --keep-index'.  Do you
> think the documentation on '--keep-index' is unclear, and could be
> improved?  Or why did you think that this is the correct output?
> 
> > Verified on:
> > + Windows 10, git version 2.21.0.windows.1
> > + Ubuntu 14.04, git version 2.21.0

```
