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

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

From
Julian de Bhal <julian.debhal@gmail.com>
Date
Dec 4, 2016, 00:14 UTC
Message-ID
<CAJZCeG02xJ7XWVU8s_GwTctZ8fwJ1_tCMrvDoCRnTXUjBrjr1Q@mail.gmail.com>
In-Reply-To
<f2f8c7c9-cbe0-870d-3c13-fc928dd91dd1@kdbg.org>
On Sat, Dec 3, 2016 at 5:49 PM, Johannes Sixt <j6t@kdbg.org> wrote:
Show 7 quoted lines
> Am 03.12.2016 um 06:04 schrieb Julian de Bhal:
>>
>> If you `git add new_file; git reset --hard`, new_file is gone forever.
>
> AFAIC, this is a feature ;-) I occasionally use it to remove a file when I
> already have git-gui in front of me. Then it's often less convenient to type
> the path in a shell, or to pointy-click around in a file browser.

Yeah, I'm conscious that it would be a change in behaviour and would almost certainly break things in the wild.

On the other hand, `rm` deletes perfectly well, but there's no good way to recover the lost files after the fact. You can take some precautions after you've been bitten, but git usually means never saying "you should have".

Show 7 quoted lines
>> git add new_file
>> [...]
>> git reset --hard                 # decided copy from backed up diff
>> # boom. new_file is gone forever
>
> ... it is not. The file is still among the dangling blobs in the repository
> until you clean it up with 'git gc'. Use 'git fsck --lost-found':
Thank you so much! Super glad to be wrong here.
Cheers,
Jules
On Sat, Dec 3, 2016 at 5:49 PM, Johannes Sixt <j6t@kdbg.org> wrote:
Show 30 quoted lines
> Am 03.12.2016 um 06:04 schrieb Julian de Bhal:
>>
>> If you `git add new_file; git reset --hard`, new_file is gone forever.
>
>
> AFAIC, this is a feature ;-) I occasionally use it to remove a file when I
> already have git-gui in front of me. Then it's often less convenient to type
> the path in a shell, or to pointy-click around in a file browser.
>
>> git add new_file
>
>
> Because of this ...
>
>> git add -p                       # also not necessary, but distracting
>> git reset --hard                 # decided copy from backed up diff
>> # boom. new_file is gone forever
>
>
> ... it is not. The file is still among the dangling blobs in the repository
> until you clean it up with 'git gc'. Use 'git fsck --lost-found':
>
> --lost-found
>
>     Write dangling objects into .git/lost-found/commit/ or
> .git/lost-found/other/, depending on type. If the object is a blob, the
> contents are written into the file, rather than its object name.
>
> -- Hannes
>
Previous: Johannes SixtNext: Christian Couder
Message 3 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.