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

Re: bug report: git checkout deletes worktree file even though it is excluded by sparse-checkout

From
Derrick Stolee <stolee@gmail.com>
Date
Jun 2, 2021, 01:37 UTC
Message-ID
<28a211cc-f210-8ce8-e7c2-90e02ff449fd@gmail.com>
In-Reply-To
<F58C0A40-A1B4-4B47-A8EA-20FEAB011B0D@botech.co.uk>
On 5/28/2021 4:14 PM, Tim Renouf (open source) wrote:
> Hi all
> 
> I have a bug report: git checkout deletes a worktree file even though it is excluded by sparse-checkout, even if it is dirty.
...
> I set up sparse-checkout to include only file1, not file2. file2 is now not in the worktree, even though it is in the commit I am checked out at. Then I create file2 with arbitrary content. Then a git checkout switching to the commit where file2 is removed also deletes it from the worktree.
>
> I assert that file2 should be left untouched by that checkout, because it is excluded by sparse-checkout. I guess file2 had its skip-worktree bit set before the checkout that removed it from the index; that should stop it being deleted in the worktree.

In this case, 'file2' exists in the index at 'master' and 'git checkout master' made the working directory appear as if it was at that position with the sparse-checkout patterns applied.

The confusion happens that Git behaves differently when a file exists outside the sparse-checkout patterns based on whether the file actually exists in the index or not. Only an exact path match with a sparse index entry will cause this deletion.

This is another reason why I prefer the "cone mode" patterns:
it is harder to get this kind of problem. You could easily
make it happen with "folder2/file" where "folder2" is not in
the cone mode sparse-checkout, but it's less likely in some
way.
 
> To be clear, I expect that last “ls” to still show “file1  file2”.

I can understand this expectation. I don't think that if we changed this behavior that it would cause any confusion. That is, as long as the file disappears when no longer dirty, _and_ a command such as "git reset --hard" overwrites the file. It is important to provide ways for users to remove the file when they want it gone.

Thanks, -Stolee

Previous: Tim Renouf (open source)
Message 10 of 10 in “bug report: git checkout deletes worktree file even though it is excluded by sparse-checkout”
  1. Tim Renouf (open source)May 28, 2021
  2. Elijah NewrenMay 28, 2021
  3. unpack-trees: add core.sparseCheckoutRmFiles configTim Renouf, Jun 1, 2021
  4. Derrick StoleeJun 2, 2021
  5. Junio C HamanoJun 2, 2021
  6. Elijah NewrenJun 2, 2021
  7. Tim Renouf (open source)Jun 2, 2021
  8. Elijah NewrenJun 2, 2021
  9. Tim Renouf (open source)Jun 5, 2021
  10. Derrick StoleeJun 2, 2021

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.