git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC] reset --hard: warn before discarding staged content with no commit history
@ 2025-12-10 15:01 Koutsouflakis Stefanos
  2025-12-11  3:24 ` Junio C Hamano
  2025-12-12 22:14 ` D. Ben Knoble
  0 siblings, 2 replies; 13+ messages in thread
From: Koutsouflakis Stefanos @ 2025-12-10 15:01 UTC (permalink / raw)
  To: git@vger.kernel.org

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

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

end of thread, other threads:[~2025-12-15  7:23 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-10 15:01 [RFC] reset --hard: warn before discarding staged content with no commit history Koutsouflakis Stefanos
2025-12-11  3:24 ` Junio C Hamano
2025-12-11  3:59   ` Eric Sunshine
2025-12-11  5:25     ` Junio C Hamano
2025-12-11 11:53   ` Koutsouflakis Stefanos
2025-12-11 12:22     ` Johannes Sixt
2025-12-11 16:33       ` Koutsouflakis Stefanos
2025-12-12  3:24       ` Junio C Hamano
2025-12-14 13:29         ` Koutsouflakis Stefanos
2025-12-14 13:45           ` Štefan Balog
2025-12-14 23:27           ` Junio C Hamano
2025-12-15  7:22             ` Koutsouflakis Stefanos
2025-12-12 22:14 ` D. Ben Knoble

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).