[RFC] builtin/stash: data loss from reset --hard
- From
- Tsahi Elkayam <tsahi.elkayam@protonmail.com>
- Date
- Jan 4, 2026, 11:05 UTC
- Message-ID
- <-98ze4v1cX5P2d_tlWY6nBZuQhY3J7OJLJX51VS53bVhirt-Gm9zA6E_Y-pNMMYhtcLN2MM_miuPfR_Nrq5JCUWDgI_BwG9rUxtuBoqf8h0=@protonmail.com>
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 lostThe 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.