[RFC] builtin/stash: data loss from reset --hard
From: Tsahi Elkayam <hidden>
Date: 2026-01-04 11:05:05
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.