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

Re: [RFC] reset --hard: warn before discarding staged content with no commit history

From
D. Ben Knoble <ben.knoble@gmail.com>
Date
Dec 12, 2025, 22:14 UTC
Message-ID
<CALnO6CC=JpKBwJbLDeBkEF5e3SnqzEXwNH_W3S5Bzhz3DD14MQ@mail.gmail.com>
In-Reply-To
<a5wKtD6Tn0gzcba1IEUhukYnXPHxMwPq6puQKIPywmjNufi5vc6vX-v5BpPJ7qj_zZsuXF5FiS2gbpsurWmVjoWHtMm8A-kAbaZyjMfrTcs=@proton.me>

On Wed, Dec 10, 2025 at 10:04 AM Koutsouflakis Stefanos <koutsouflakis.stefanos@proton.me> wrote:

Show 48 quoted lines
>
> When running "git reset --hard" in a repository where staged content
> has never been committed, the staged files are lost. This seems like a case where requiring --force could be helpful.
>
> Reproduction:
>
>     mkdir test && cd test
>     git init
>     echo "hello" > a.txt
>     git add .
>     git reset --hard
>
> Result: a.txt is removed from both the index and working tree.
> While the blob temporarily remains as a dangling object (recoverable
> via "git fsck --lost-found" until garbage collection), this is not a
> realistic safety net as the filename is lost and most users are
> unaware of this recovery mechanism.
>
> The most likely scenario is a user initializing a Git repository in an
> existing project. They have a folder with files they've been working on,
> run "git init", then "git add ." to stage everything. A mistyped or
> misunderstood command later, their entire project is wiped out.
>
> Proposed behavior:
>
> When "git reset --hard" would discard staged content that does not
> exist in any commit (i.e., the blob has no reachable reference),
> print a warning and require confirmation or --force:
>
>     warning: the following staged files have never been committed
>     and will be permanently lost:
>         a.txt
>     use --force to proceed, or commit first
>
> This would be consistent with Git's general trend toward safer
> defaults.
>
> Questions for discussion:
>
> 1. Is this safety check worth the added complexity?
>
> 2. Are there workflows where this would be annoying? (can't think of any but I might be missing something).
>
> I'm happy to work on a patch if there's interest.
>
> Thanks,
> Stefanos
>

Perhaps useful for future readers who are looking for ways to recover the objects: https://blog.plover.com/prog/git-reset-disaster.html

-- 
D. Ben Knoble
Previous: Koutsouflakis Stefanos
Message 13 of 13 in “[RFC] reset --hard: warn before discarding staged content with no commit history”
  1. Koutsouflakis StefanosDec 10, 2025
  2. Junio C HamanoDec 11, 2025
  3. Eric SunshineDec 11, 2025
  4. Junio C HamanoDec 11, 2025
  5. Koutsouflakis StefanosDec 11, 2025
  6. Johannes SixtDec 11, 2025
  7. Koutsouflakis StefanosDec 11, 2025
  8. Junio C HamanoDec 12, 2025
  9. Koutsouflakis StefanosDec 14, 2025
  10. Štefan BalogDec 14, 2025
  11. Junio C HamanoDec 14, 2025
  12. Koutsouflakis StefanosDec 15, 2025
  13. D. Ben KnobleDec 12, 2025

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.