# Prevent reset --hard from deleting everything if one doesn't have any commits yet

5 messages from 2019-02-28 to 2019-03-03. Participants: Manuel Guilamo, Johannes Sixt, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/50617

## Manuel Guilamo, 2019-02-28 21:43

Subject: Prevent reset --hard from deleting everything if one doesn't have any commits yet
Message-ID: <15568DDA-F501-48C9-A1C0-97C72C29DA23@publicis.com.do>
URL: https://gitlist.dev/e/15568DDA-F501-48C9-A1C0-97C72C29DA23%40publicis.com.do

```
I accidentally executed git reset —hard in a project that doesn’t have any commits yet. git erased everything, everything I’ve worked the past week, I believe this is not a desired behavior, considering I’m not able to undo that action, because git doesn’t have any history whatsoever.
```

## Johannes Sixt, 2019-03-01 06:56

Subject: Re: Prevent reset --hard from deleting everything if one doesn't have any commits yet
Message-ID: <d1b34077-1c0e-7a2a-d2c7-da013dd56cd9@kdbg.org>
URL: https://gitlist.dev/e/d1b34077-1c0e-7a2a-d2c7-da013dd56cd9%40kdbg.org
In-Reply-To: <15568DDA-F501-48C9-A1C0-97C72C29DA23@publicis.com.do>

```
Am 28.02.19 um 22:43 schrieb Manuel Guilamo:
> I accidentally executed git reset —hard in a project that doesn’t
> have any commits yet. git erased everything, everything I’ve worked
> the past week, I believe this is not a desired behavior, considering
> I’m not able to undo that action, because git doesn’t have any
> history whatsoever.

I tested this, and it does not happen for me as long as I do not `git
add` anything.

So, I assume you did `git add` your content and then you did a `git
reset --hard`. In that case, I'm afraid Git behaved as designed and
"doesn't have any commits" is a red herring.

-- Hannes

```

## Manuel Guilamo, 2019-03-01 13:46

Subject: Re: Prevent reset --hard from deleting everything if one doesn't have any commits yet
Message-ID: <11C5FB4E-A4FF-430A-9305-E5BDA6A11049@publicis.com.do>
URL: https://gitlist.dev/e/11C5FB4E-A4FF-430A-9305-E5BDA6A11049%40publicis.com.do
In-Reply-To: <d1b34077-1c0e-7a2a-d2c7-da013dd56cd9@kdbg.org>

```
Oh yes... I did. Sigh

Regardless,

wouldn’t be nice to have a confirmation in cases like this? considering git isn’t only used by experts. It would’ve helped me A LOT, that’s for sure… and I’m 100% sure I won’t be that last person in the history of git that would suffer this.

Thank you for your quick reply.



> On Mar 1, 2019, at 2:56 AM, Johannes Sixt <j6t@kdbg.org> wrote:
> 
> Am 28.02.19 um 22:43 schrieb Manuel Guilamo:
>> I accidentally executed git reset —hard in a project that doesn’t
>> have any commits yet. git erased everything, everything I’ve worked
>> the past week, I believe this is not a desired behavior, considering
>> I’m not able to undo that action, because git doesn’t have any
>> history whatsoever.
> 
> I tested this, and it does not happen for me as long as I do not `git
> add` anything.
> 
> So, I assume you did `git add` your content and then you did a `git
> reset --hard`. In that case, I'm afraid Git behaved as designed and
> "doesn't have any commits" is a red herring.
> 
> -- Hannes


```

## Jeff King, 2019-03-01 13:47

Subject: Re: Prevent reset --hard from deleting everything if one doesn't have any commits yet
Message-ID: <20190301134759.GA13402@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20190301134759.GA13402%40sigill.intra.peff.net
In-Reply-To: <d1b34077-1c0e-7a2a-d2c7-da013dd56cd9@kdbg.org>

```
On Fri, Mar 01, 2019 at 07:56:00AM +0100, Johannes Sixt wrote:

> Am 28.02.19 um 22:43 schrieb Manuel Guilamo:
> > I accidentally executed git reset —hard in a project that doesn’t
> > have any commits yet. git erased everything, everything I’ve worked
> > the past week, I believe this is not a desired behavior, considering
> > I’m not able to undo that action, because git doesn’t have any
> > history whatsoever.
> 
> I tested this, and it does not happen for me as long as I do not `git
> add` anything.
> 
> So, I assume you did `git add` your content and then you did a `git
> reset --hard`. In that case, I'm afraid Git behaved as designed and
> "doesn't have any commits" is a red herring.

Wouldn't that mean all of the file data is available in the object
database? Unfortunately without an index, there's nothing to mark which
file was which. But `git fsck --lost-found` should copy out all of the
file content into .git/lost-found.

-Peff

```

## Junio C Hamano, 2019-03-03 01:38

Subject: Re: Prevent reset --hard from deleting everything if one doesn't have any commits yet
Message-ID: <xmqqa7ic3fgn.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqa7ic3fgn.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20190301134759.GA13402@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> Wouldn't that mean all of the file data is available in the object
> database? Unfortunately without an index, there's nothing to mark which
> file was which. But `git fsck --lost-found` should copy out all of the
> file content into .git/lost-found.

If we had a hierachically stored index that stores subdirectory
contents as a tree (or some tree-like things), "fsck --lost-found"
would have been able to recover even pathnames, but with the current
flat index in a fresh directory without any commit, we'd get bunch
of blobs without any clue as to where each of them goes.  It would
be far better than nothing, but leaves room for improvement.

```
