git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC] builtin/stash: data loss from reset --hard

From
TETsahi 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 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.
Next: Junio C Hamano
Message 1 of 2 in “[RFC] builtin/stash: data loss from reset --hard”
  1. Tsahi ElkayamJan 4, 2026
  2. Junio C HamanoJan 5, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.