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

[USABILITY] git-reset removes directories

From
YLYutian Li <hotpxless@gmail.com>
Date
Apr 21, 2018, 02:58 UTC
Message-ID
<CABvSBngwh_C5dna=U9dLpg3r=BWQH8imsa-3q=yiLFeYdbT17Q@mail.gmail.com>
Ok I have been bitten by this issue twice. :(

`git reset --hard` will reset modifications in the working directory (of course). But if I remove a file, add a directory with the same name, `git reset --hard` will erase that whole directory. Specifically the following steps: ``` touch file git add file git commit -m "add file" # now the HEAD is constructed rm file mkdir file touch file/a # now here is the issue git reset --hard # this will remove the directory and all its files ```

I guess this has to do with Git not tracking "changing a file to a directory", especially because one is a blob and another is a tree.

In case of `git reset --hard`, people would expect reverting modifications, bringing up removed files, but not removing a whole directory with the same name. Especially if the directory is already populated. Then the whole thing is gone. I think it would better if Git could output a warning and abort, like in case of checkout ("Updating the following ... would lose ...").

But honestly I usually take `git reset --hard` seriously and will double check before invocation. What actually bit me was `git stash`. It will call `git reset --hard -q` after saving the working directory. But in the case I just described, since the new directory is not tracked, it will not be saved and will be erased. `git stash` usually gives me the mental model of "saving stuff in working directory", but not like in this case where it also deletes stuff in working directory.

Message 1 of 1 in “[USABILITY] git-reset removes directories”
  1. Yutian LiApr 21, 2018

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.