public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
* [RFC] builtin/stash: data loss from reset --hard
@ 2026-01-04 11:05 Tsahi Elkayam
  2026-01-05  1:47 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Tsahi Elkayam @ 2026-01-04 11:05 UTC (permalink / raw)
  To: git@vger•kernel.org

Hi,

I am a beginner C developer exploring the Git codebase and came across
something I would like to understand better.

In builtin/stash.c line 1747, there is a comment:

    /* BUG: this nukes untracked files in the way */
    strvec_pushl(&cp.args, "reset", "--hard", "-q",
                 "--no-recurse-submodules", NULL);

Steps to reproduce:

    $ git init test && cd test
    $ echo "tracked" > foo && git add foo && git commit -m "init"
    $ git rm foo
    $ mkdir foo && echo "precious" > foo/file
    $ git stash
    $ cat foo/file
    cat: foo/file: Not a directory   # precious data is lost

The reset --hard restores the original tracked file "foo" from HEAD,
destroying the untracked directory "foo/" and its contents.

There is also a test_expect_failure test in t/t2500-untracked-overwriting.sh
that documents this behavior.

I am not sure if this is considered a bug to be fixed, or intentional
behavior that is simply documented.

If it is a bug, would this fix be reasonable:

-       /* BUG: this nukes untracked files in the way */
-       strvec_pushl(&cp.args, "reset", "--hard", "-q",
+       strvec_pushl(&cp.args, "reset", "--merge", "-q",
                     "--no-recurse-submodules", NULL);

I understand --merge would fail instead of silently overwriting,
which seems safer.

I would appreciate any feedback or guidance.

Thanks,
Tsahi



Sent with Proton Mail secure email.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-01-05  1:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-04 11:05 [RFC] builtin/stash: data loss from reset --hard Tsahi Elkayam
2026-01-05  1:47 ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox