# git gc destroys autostash

3 messages from 2024-02-07 to 2024-02-07. Participants: Orgad Shaneh, Junio C Hamano, Kristoffer Haugsbakk.
Thread: https://gitlist.dev/t/60869

## Orgad Shaneh, 2024-02-07 20:52

Subject: git gc destroys autostash
Message-ID: <CAGHpTBKpYp370QTw93wK_RP+X2S+44jd-8kjodiUj4k0BoAEqA@mail.gmail.com>
URL: https://gitlist.dev/e/CAGHpTBKpYp370QTw93wK_RP%2BX2S%2B44jd-8kjodiUj4k0BoAEqA%40mail.gmail.com

```
Running git gc --prune=now during rebase with autostash deletes the
autostash object, and it cannot be recovered when the rebase ends.

Example:

#!/bin/sh

git init
echo 1 > foo; git add foo; git commit -m 'Initial commit'
echo 2 > foo; git add foo; git commit -m 'Second commit'
echo 3 > foo; git rebase -i --autostash HEAD^
# Choose edit
git gc --prune=now
git rebase --continue
# fatal: '3b88163a1bff3859a005554c168d94e5357ee45b' is not a stash-like commit
# error: cannot store 3b88163a1bff3859a005554c168d94e5357ee45b
# Successfully rebased and updated refs/heads/master.

Thanks,
- Orgad

```

## Junio C Hamano, 2024-02-07 21:05

Subject: Re: git gc destroys autostash
Message-ID: <xmqq7cjg6l3r.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq7cjg6l3r.fsf%40gitster.g
In-Reply-To: <CAGHpTBKpYp370QTw93wK_RP+X2S+44jd-8kjodiUj4k0BoAEqA@mail.gmail.com>

```
Orgad Shaneh <orgads@gmail.com> writes:

> Running git gc --prune=now during rebase with autostash deletes the
> autostash object, and it cannot be recovered when the rebase ends.
>
> Example:
>
> #!/bin/sh
>
> git init
> echo 1 > foo; git add foo; git commit -m 'Initial commit'
> echo 2 > foo; git add foo; git commit -m 'Second commit'
> echo 3 > foo; git rebase -i --autostash HEAD^
> # Choose edit
> git gc --prune=now

This is totally expected, unfortunately, as the autostash does not
use the ref API to use the refs/stash (presumably in order to avoid
shifting the shash@{$N} numbers). Because of that, the stash entry
is not protected from the garbage collection.  This currently falls
into "it hurts when I twist my arm this way. --do not do it then"
category.

It may be a simple fix to teach sequencer.c:apply_save_autostash()
not to use a random on-disk file to store the returned value from
"git stash create", but use a dedicated ref that is not refs/stash
for its own use via proper use of the ref API.

> git rebase --continue
> # fatal: '3b88163a1bff3859a005554c168d94e5357ee45b' is not a stash-like commit
> # error: cannot store 3b88163a1bff3859a005554c168d94e5357ee45b
> # Successfully rebased and updated refs/heads/master.
>
> Thanks,
> - Orgad

```

## Kristoffer Haugsbakk, 2024-02-07 21:47

Subject: Re: git gc destroys autostash
Message-ID: <a18532ba-b168-4928-bced-d1604a483f24@app.fastmail.com>
URL: https://gitlist.dev/e/a18532ba-b168-4928-bced-d1604a483f24%40app.fastmail.com
In-Reply-To: <CAGHpTBKpYp370QTw93wK_RP+X2S+44jd-8kjodiUj4k0BoAEqA@mail.gmail.com>

```
On Wed, Feb 7, 2024, at 21:52, Orgad Shaneh wrote:
> Running git gc --prune=now during rebase with autostash deletes the
> autostash object, and it cannot be recovered when the rebase ends.

Things like this is why I don’t use the stash.[1][2] I just commit
everything of value and non-value.

† 1: With very rare exceptions where the stash is popped before the
    whole operation leaves my own working memory
† 2: Also how you have to find unreachable objects using git-fsck(1) if
    you pop the last stash

-- 
Kristoffer Haugsbakk

```
