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

Re: git reset --hard should not irretrievably destroy new files

From
Christian Couder <christian.couder@gmail.com>
Date
Dec 3, 2016, 08:11 UTC
Message-ID
<CAP8UFD0ipS_4p+njfbbDGpYSDJhp43e9XDP69MOruZz9c136ew@mail.gmail.com>
In-Reply-To
<CAJZCeG1Eu+5DfaxavX_WGUCa+SY+yepDWZhPXxiFcV__h0xjrw@mail.gmail.com>
On Sat, Dec 3, 2016 at 6:04 AM, Julian de Bhal <julian.debhal@gmail.com> wrote:
> If you `git add new_file; git reset --hard`, new_file is gone forever.
>
> This is totally what git says it will do on the box, but it caught me out.

Yeah, you are not the first one, and probably not the last unfortunately, to be caught by it, see for example the last discussion about it:

https://public-inbox.org/git/loom.20160523T023140-975@post.gmane.org/
which itself refers to this previous discussion:
https://public-inbox.org/git/CANWD=rX-MEiS4cNzDWr2wwkshz2zu8-L31UrKwbZrJSBcJX-nQ@mail.gmail.com/
Show 15 quoted lines
> It might seem a little less stupid if I explain what I was doing: I was
> breaking apart a chunk of work into smaller changes:
>
> git commit -a -m 'tmp'           # You feel pretty safe now, right?
> git checkout -b backup/my-stuff  # Not necessary, just a convenience
> git checkout -
> git reset HEAD^                  # mixed
> git add new_file
> git add -p                       # also not necessary, but distracting
> git reset --hard                 # decided copy from backed up diff
> # boom. new_file is gone forever
>
>
> Now, again, this is totally what git says it's going to do, and that was
> pretty stupid, but that file is gone for good, and it feels bad.

Yeah, I agree that it feels bad even if there are often ways to get back your data as you can see from the links in Yotam's email above.

Show 7 quoted lines
> Everything that was committed is safe, and the other untracked files in
> my local directory are also fine, but that particular file is
> permanently destroyed. This is the first time I've lost something since I
> discovered the reflog a year or two ago.
>
> The behaviour that would make the most sense to me (personally) would be
> for a hard reset to unstage new files,
This has already been proposed last time...
> but I'd be nearly as happy if a
> commit was added to the reflog when the reset happens (I can probably make
> that happen with some configuration now that I've been bitten).

Not sure if this has been proposed. Perhaps it would be simpler to just output the sha1, and maybe the filenames too, of the blobs, that are no more referenced from the trees, somewhere (in a bloblog?).

> If there's support for this idea but no-one is keen to write the code, let
> me know and I could have a crack at it.

Not sure if your report and your offer will make us more likely to agree to do something, but thanks for trying!

Previous: Julian de BhalNext: Julian de Bhal
Message 4 of 7 in “git reset --hard should not irretrievably destroy new files”
  1. Julian de BhalDec 3, 2016
  2. Johannes SixtDec 3, 2016
  3. Julian de BhalDec 4, 2016
  4. Christian CouderDec 3, 2016
  5. Julian de BhalDec 4, 2016
  6. Christian CouderDec 4, 2016
  7. Junio C HamanoDec 4, 2016

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.