threads / bug / 66429

[BUG] sparse-checkout: checkout clobbers untracked in-cone file

Subject: [BUG] sparse-checkout: checkout clobbers untracked in-cone file

## tl;dr

2 messages between Sep 30, 2026 and Oct 9, 2026.

replies: 1people: 2as markdown or json

Webstrand· Sep 30, 2026, 20:20 UTC · lore
What did you do before the bug happened? (Steps to reproduce your issue)
    git init --initial-branch=locus repo
    cd repo
    mkdir src docs
    echo "tracked" >src/tracked.txt
    echo "tracked" >docs/tracked.txt
    git add .
    git commit --message="initial"
    git sparse-checkout set src
    git branch some-feature-branch
    git checkout some-feature-branch
    echo "incoming" >src/new.txt
    git add src/new.txt
    git commit --message="add src/new.txt"
    git checkout locus
    echo "local" >src/new.txt # conflicting untracked file
    git checkout some-feature-branch
    echo $?
    cat src/new.txt
What did you expect to happen? (Expected behavior)
The checkout operation should be aborted as a whole:
 1. No files in the working tree should be updated.
 2. HEAD should not be moved and should continue to reference locus.
 3. src/new.txt must be unmodified, preserving the untracked content
    "local".
 4. git should exit with status 1 and print the following error:
    error: The following untracked working tree files would be
overwritten by checkout:
        src/new.txt
    Please move or remove them before you switch branches.
    Aborting

Sparse checkouts should have the same behavior as non-sparse checkouts of the same commits.

What happened instead? (Actual behavior)
The checkout operation clobbered untracked files:
 1. Files in the working tree were updated.
 2. HEAD was moved to reference some-feature-branch.
 3. src/new.txt was overwritten with the content "incoming", clobbering
    it.
 4. git exited with status 0 and printed the following warning:
    warning: The following paths were already present and thus not
updated despite sparse patterns:
        src/new.txt
    After fixing the above paths, you may want to run `git
sparse-checkout reapply`.
    Switched to branch 'some-feature-branch'
Additionally, the produced warning is incorrect and misleading.
What's different between what you expected and what actually happened?

The checkout completed instead of being aborted, src/new.txt was clobbered instead of preserved, and the conflict was reported as a warning instead of an error, and the warning was incorrect and misleading. As a result, the untracked content of src/new.txt was destroyed.

Anything else you want to add:

The behavior is present from v2.27.0, the first release to contain 681c637b4a, through the tip of master (a018953688).

The behavior was introduced by
681c637b4a unpack-trees: failure to set SKIP_WORKTREE bits always just a warning

Prior to 681c6, git refused the sparse checkout, producing the same error as the non-sparse checkout, with the same behavior as in "Expected behavior". src/new.txt was not clobbered. Starting with 681c6, git completes the sparse checkout with the warning shown in "Actual behavior", and src/new.txt is clobbered.

[System Info]
git version 2.56.0
cpu: x86_64
built from commit: a018953688f1b10bddf91bff8747068f5f4746a4
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
rust: enabled
feature: fsmonitor--daemon
gettext: enabled
libcurl: 8.22.0
OpenSSL: OpenSSL 3.5.3 16 Sep 2025
zlib: 1.3.1
SHA-1: SHA1_DC
SHA-256: SHA256_BLK
default-ref-format: files
default-hash: sha1
uname: Linux 7.2.7-1-default #1 SMP PREEMPT_DYNAMIC Tue Sep 22
08:00:24 UTC 2026 (e601a2d) x86_64
compiler info: gnuc: 16.2
libc info: glibc: 2.44
$SHELL (typically, interactive shell): /bin/bash

[Enabled Hooks] not run from a git repository - no hooks to show

Muhammed Dilshad A· Oct 9, 2026, 14:00 UTC · re: Webstrand · lore

[PATCH] unpack-trees: protect untracked files in sparse checkouts

In a sparse checkout, switching to a branch that adds a file over an untracked local path can warn and continue, replacing the local contents. Ordinary checkout refuses the same switch.

merged_entry() defers the check for new paths until sparse patterns are known. The deferred check reports a sparsity warning, and unpack_trees() clears its failure before updating the working tree.

Use the ordinary untracked overwrite error for that check. Finish checking all new entries before applying sparsity changes, and abort through the existing failure path if any conflict is found. Keep advisory warnings for sparsity changes to existing entries.

Add a regression comparing full checkout, sparse checkout, and sparse-index checkout. Check both conflicting files, complete diagnostics, HEAD, an unrelated tracked file, and an existing staged change. Adjust the warning test to use existing skipped entries and verify that their local contents survive.

Reported-by: Webstrand <webstrand@gmail.com>
Link: https://lore.kernel.org/git/CACm1TQd5b9tX368LrsD26Q6tm_mzdi1nwGtGd2V1jwYW+r2c1A@mail.gmail.com/
Signed-off-by: Muhammed Dilshad A <dilsheddilu123@gmail.com>
---
 t/t1011-read-tree-sparse-checkout.sh     | 16 ++++++---
 t/t1092-sparse-checkout-compatibility.sh | 41 ++++++++++++++++++++++++
 unpack-trees.c                           |  9 +++++-
 3 files changed, 60 insertions(+), 6 deletions(-)
diff --git a/t/t1011-read-tree-sparse-checkout.sh b/t/t1011-read-tree-sparse-checkout.sh
index 93244ee134..54a7258b03 100755
--- a/t/t1011-read-tree-sparse-checkout.sh
+++ b/t/t1011-read-tree-sparse-checkout.sh
@@ -263,12 +263,15 @@ test_expect_success 'read-tree --reset removes outside worktree' '
 '
 
 test_expect_success 'print warnings when some worktree updates disabled' '
-	echo sub >.git/info/sparse-checkout &&
-	git checkout -f init &&
+	echo init.t >.git/info/sparse-checkout &&
+	git checkout -f top &&
 	mkdir sub &&
-	touch sub/added sub/addedtoo &&
+	test_write_lines local >sub/added &&
+	test_write_lines local >sub/addedtoo &&
+	echo sub >.git/info/sparse-checkout &&
+	# Keep skip-worktree bits on the materialized paths to test the warning.
 	# Use -q to suppress "Previous HEAD position" and "Head is now at" msgs
-	git checkout -q top 2>actual &&
+	git -c sparse.expectFilesOutsideOfPatterns=true checkout -q top 2>actual &&
 	cat >expected <<\EOF &&
 warning: The following paths were already present and thus not updated despite sparse patterns:
 	sub/added
@@ -276,7 +279,10 @@ warning: The following paths were already present and thus not updated despite s
 
 After fixing the above paths, you may want to run `git sparse-checkout reapply`.
 EOF
-	test_cmp expected actual
+	test_cmp expected actual &&
+	test_write_lines local >expected-content &&
+	test_cmp expected-content sub/added &&
+	test_cmp expected-content sub/addedtoo
 '
 
 test_expect_success 'checkout without --ignore-skip-worktree-bits' '
diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh
index 05b54062b3..0958af35c4 100755
--- a/t/t1092-sparse-checkout-compatibility.sh
+++ b/t/t1092-sparse-checkout-compatibility.sh
@@ -486,6 +486,47 @@ test_expect_success 'checkout with modified sparse directory' '
 	test_all_match git checkout base
 '
 
+test_expect_success 'checkout protects untracked files inside sparse cone' '
+	init_repos &&
+	git -C initial-repo rev-parse base >expect-head &&
+	test_write_lines local >expect-untracked &&
+	test_write_lines a >expect-tracked &&
+	test_write_lines staged >expect-staged &&
+	test_write_lines refs/heads/base >expect-branch &&
+	for repo in full-checkout sparse-checkout sparse-index
+	do
+		(
+			cd "$repo" &&
+			git checkout -b new-file &&
+			test_write_lines incoming >deep/new-file &&
+			test_write_lines incoming >deep/other-file &&
+			test_write_lines changed >deep/a &&
+			git add deep/new-file deep/other-file deep/a &&
+			git commit -m "add a file and change a tracked file" &&
+			git checkout base &&
+			test_write_lines local >deep/new-file &&
+			test_write_lines local >deep/other-file &&
+			test_write_lines staged >a &&
+			git add a &&
+			git write-tree >"../$repo-index-before"
+		) || return 1
+	done &&
+	test_all_match test_must_fail git checkout new-file &&
+	for repo in full-checkout sparse-checkout sparse-index
+	do
+		git -C "$repo" rev-parse HEAD >actual-head &&
+		test_cmp expect-head actual-head &&
+		git -C "$repo" symbolic-ref HEAD >actual-branch &&
+		test_cmp expect-branch actual-branch &&
+		test_cmp expect-untracked "$repo/deep/new-file" &&
+		test_cmp expect-untracked "$repo/deep/other-file" &&
+		test_cmp expect-tracked "$repo/deep/a" &&
+		test_cmp expect-staged "$repo/a" &&
+		git -C "$repo" write-tree >actual-index &&
+		test_cmp "$repo-index-before" actual-index || return 1
+	done
+'
+
 test_expect_success 'checkout orphan then non-orphan' '
 	init_repos &&
 
diff --git a/unpack-trees.c b/unpack-trees.c
index 1802809ad3..f763f284a6 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -2058,8 +2058,15 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
 			 * correct CE_NEW_SKIP_WORKTREE
 			 */
 			if (ce->ce_flags & CE_ADDED &&
-			    verify_absent(ce, WARNING_SPARSE_ORPHANED_NOT_OVERWRITTEN, o))
+			    verify_absent(ce,
+				ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
 				ret = 1;
+		}
+		if (ret)
+			goto return_failed;
+
+		for (i = 0; i < o->internal.result.cache_nr; i++) {
+			struct cache_entry *ce = o->internal.result.cache[i];
 
 			if (apply_sparse_checkout(&o->internal.result, ce, o))
 				ret = 1;

base-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd
-- 
2.55.0

← back to recent threads