From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.222.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796196537; b=psLJoiNllDN12hd7RmDreGJTd6Qu6kKWBV52lNRWLOOrUiom3GL9CCn1EkURftkPxFDW MGMfCz6Xz34HI1WDpZJ1NXN+TnjWHbog7Y1CyyG6oqLB/HzQbiBxJSS5PmjoQ8GFmg/GR fgcadK/cSvjY+XI4+Dg8jimQ4KRW4ofaBg3HUuRJakaX/NMJBmBgsu3zprUL/EHggzXgv KIQM4mJk5bR5H85haxR2cZ9soDGMc/OpJfrszrW4ow/KMeRDxPLkUFd1EIbytKe/L56QG XJeakyDU4owEXtKDpOJeKLl66v7OoRQX/pf9tvaTZX7AhvWKGd19kxb/WkS/5NQPH/w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796196537; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=FegJtsTF6dW/SyQWu/h5QwELJMS2J7NCEogBiQcPqdc=; b=Jox6MtJnMMJ6lTia6fiRZ8HOlfDKbeTZmVoIW5r6ZIfMjXyla0pEwnGj1QxAvTAqtXbF YvB1rNB4vOHVLy7jCX4BwXYB878p+ZHuCyoLJ7bLSiCQn4mdjcO/CVzf4HfD30ipmdFm0 ZDLRqU1cJxg7qZKuNhZTHTuSmByKRbKS7rem72r1VnHnjQEnrm3gUfH0Ac+az67Y3Kj6S Sf7SQLEmabbNhCyPvCQM0OMYtUk/uy32moWRnY2UV/9uyTk2sjEoOTsLLU6+1WMZIe2t1 ZALTFLjcXlWB/B/zck7sngiEw4sSecoE1Lz2HhI3NM3zq389X2eQf5pgrVVO0nvIMsA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=jaspermayone.com; spf=pass smtp.mailfrom=singlefeather.com; dkim=pass header.d=jaspermayone.com header.i=@jaspermayone.com header.a=rsa-sha256 header.s=google header.b=InpUq1+a; arc=none smtp.remote-ip=209.85.222.182 Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A396606A327 for ; Wed, 2 Dec 2026 07:28:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5A396606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=jaspermayone.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=singlefeather.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=jaspermayone.com header.i=@jaspermayone.com header.a=rsa-sha256 header.s=google header.b=InpUq1+a Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-94a440c3a02so296741885a.2 for ; Tue, 01 Dec 2026 23:28:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jaspermayone.com; s=google; t=1796196536; x=1796801336; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=FegJtsTF6dW/SyQWu/h5QwELJMS2J7NCEogBiQcPqdc=; b=InpUq1+awnnw232S/wAYd1l40b40w1RvOfRtBIKririMoFL4PBWO83v1cNX68xrxTZ BNi/aIDrCKmR9Ffa6NWeQVrDhBhOWKxrzKggIq/uyw73s6Uq8PSjqqSDPWtORLOBLh+C rEzvq3ECkq61suxd5r7JXx+hqJCy+1JDx+UsWRIvuf9dICWzSEF6ZTwDl1Nl8PJ6zQB/ 15L4ke0F7R0X3oNv+VQGHxbVdPB8VZ3VCpy+PceAJ6tsD9e0jg5Y/FQjjBE7x2Ti1u3B nvEbFyMozxvdoKa/oRBkEG2qmEAeCd0swoex+GpnhPKaKl+J31Xrv7tRnwC6c841l6Xu C45g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796196536; x=1796801336; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=FegJtsTF6dW/SyQWu/h5QwELJMS2J7NCEogBiQcPqdc=; b=iSYxw6Xqhd6EWbR8M47ETW/ls9encfY0Qy6/W7E8wlOnbUO2BVGdjvT10S/4AdIJQI ZhRoB1rLSFjphq1z0qbJXAo1R/bB95BdFgrTmWn7hwziVf8BTFEbYw5tE+5f4G4rBroi Pe+XDs1tRBRFEKu0yDuorzc1nT9ufWuOyj8NjivY+AYH6/nzckDGq/LLcjo91d5F6MPm Xr251KcniiM5GU6z/TEECs4C4tSPBNJiAa8EZ5uwnCS2p5zYfi19PWBT1BNt6TMXPXnm EqVb3Ex3OH/xqDdc5Nrmn3JIxB79P+KC76WckS4qFzKHVsKTqjvPj4VmflruJueR4b6E aJng== X-Gm-Message-State: APuU4Z/rhQDIT7EybsLqcK1hi6ndSDWh0JVZqnxmAoHBmV7WRE3obAQ+ bfqSuhSzOQqZblXj15MGPPItMsIc/I5lwJ8fN+QNUl4QkZW1qJJmpQfFe+4rlDrS3Az403egfFu A0eFlVd0ZWUy8UxDS/VTfXS8aHvVY3bD/AnJ9Hx0ZAPfC1W/Va1kutY/LMXmql6cC6iEAGoCl29 m+39Hfs3ldhDOHjKoJZEtmPjI0QfLkwM6rHd+ioHYUO0AGk0JwjNmuYyqQkXGd7GhKc+ftf+y2w jcZECav+7YSf6ha2e+oo/Koqro7F1nKdyyqMJ33Wl/qXQ== X-Gm-Gg: AZFdTZfJPgi9peiS0859wIodqrpyKCH5Z/nWJxhDh1JTvD6IT+c+isSmo0MqFuKJmc7 MrhJ1t7Nwfw70QkAR4Jz/JSxxBKci76sLVG2eTJWRVhHNFMDwmd8k7f59FN1lKppCWQIb0Tyq5T d0ZDHr3Y9bjLh6bYnMRayVWRUDi2TFMK9vwxhX7FOVxe69bH2U8iNMRc7gdixLvbhQGWRv6IGGz PBHNyluHy/77Gexf3IkSseNT6RucJIG/N/gK35L5QedXIyNYISDWpJCa8e3o+Og/P2fvwUOfuUF oU9iJWaC2hPilb9bY/zg2q1t2ilOGYyleL3k+6eliyOT+9H6Mqm2pfMSCV64Ky7eS/Ax29PUzwB 07CPmzgR8QT1d2yfp0vPhJDDxMkewX6UqowdX4XOsuWKgZa+iZ61eUTyOiGK7peBjTFUL7bp/CV ovNeAyP9TndsAtv+LkxS1tnuUit4+PN7uE+HpkNUHdBzbNfOcYRxk+zP0kFSG2OI8cgoascC0r3 hurUZsb+i1rhywjXo7KcHw8qLLYxrqNvFU14K3/Jd0aFARoxTpkTjWFuF0aUzhpOYC+VQYu5HLy U7D1uUuJ80mzVsx3945VYFCCnQEg4xjdeK6NtCndV3zAuEAu2h4mqbAxODNYxZUtrci3O+XRo4t iv1hi6ga2vjSi8HdY9eTUHcwhqHD62hK+dYOFJHg6PRMd8sOhbXNDjOeFYtAAdP73sKmrs2kdB8 B7KlaQJW91mJ8Ay9BM+aZz7VhGiZbkutCNqosQ2MaOg1Up38vgfnCPMluOUYHSTUUWRNYGK6hSB xL9ItRpKkNTMC/3NT/WnXWUOJ7KWpOz7gNZR1CK1kVHwWr4 X-Received: by 2002:a05:620a:2681:b0:949:752d:31e2 with SMTP id af79cd13be357-949bab3e8d1mr6692868485a.82.1796196536031; Tue, 01 Dec 2026 23:28:56 -0800 (PST) Received: from remus.wildebeest-stargazer.ts.net ([69.43.65.112]) by smtp.gmail.com with ESMTPSA id af79cd13be357-949bb35005dsm3021240685a.11.2026.12.01.23.28.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 23:28:55 -0800 (PST) From: Jasper Mayone To: git@vger.kernel.org Cc: gitster@pobox.com, phillip.wood123@gmail.com, sunshine@sunshineco.com, kristofferhaugsbakk@fastmail.com, ben.knoble@gmail.com, haraldnordgren@gmail.com, Jasper Mayone Subject: [PATCH v6 1/3] worktree remove: refuse a worktree in the middle of an operation Date: Wed, 2 Dec 2026 02:28:48 -0500 Message-ID: <20261202072850.1555-2-me@jaspermayone.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261202072850.1555-1-me@jaspermayone.com> References: <20261111152811.99986-1-me@jaspermayone.com> <20261202072850.1555-1-me@jaspermayone.com> X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit "git worktree remove" refuses to remove a worktree with modified or untracked files unless --force is given, but it removes a worktree that is in the middle of a rebase, a "git am" session, a bisect, a cherry-pick or a revert when its files happen to be clean. The state of that operation lives in the worktree's own $GIT_DIR, so it is lost with the worktree. Examples of such a clean worktree are a rebase stopped at an "edit" or "break" line or after a failed "exec", a cherry-pick sequence after a conflict was resolved and committed, and a single cherry-pick or revert whose conflict was resolved to the current contents, which leaves CHERRY_PICK_HEAD or REVERT_HEAD behind. Treat such a worktree as unclean: refuse to remove it unless --force is given, and name the operation in the message. A worktree whose directory is already gone is still removed without --force, as before: the check is part of the check for a clean worktree, which is only done when the directory exists. Reported-by: D. Ben Knoble Signed-off-by: Jasper Mayone --- Documentation/git-worktree.adoc | 17 ++++-- builtin/worktree.c | 37 ++++++++++++- t/t2403-worktree-move.sh | 94 +++++++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 6 deletions(-) diff --git a/Documentation/git-worktree.adoc b/Documentation/git-worktree.adoc index e6e77252ab..cde17c9ab5 100644 --- a/Documentation/git-worktree.adoc +++ b/Documentation/git-worktree.adoc @@ -141,10 +141,16 @@ the new working tree location. `remove`:: -Remove a worktree. Only clean worktrees (no untracked files and no -modification in tracked files) can be removed. Unclean worktrees or ones -with submodules can be removed with `--force`. The main worktree cannot be -removed. +Remove a worktree. The main worktree cannot be removed. Without +`--force`, a worktree is not removed in these cases: ++ +-- +* The worktree has untracked files or modifications in tracked files. +* The worktree has submodules. +* The worktree is in the middle of a `git rebase`, `git am`, + `git bisect`, `git cherry-pick` or `git revert`. Removing the + worktree would lose the state of that operation. +-- `repair [...]`:: @@ -189,7 +195,8 @@ twice. If the destination is already assigned to some other worktree but is missing (for instance, if __ was deleted manually), then `--force` allows the move to proceed; use `--force` twice if the destination is locked. + -`remove` refuses to remove an unclean worktree unless `--force` is used. +`remove` refuses to remove a worktree in the cases listed under +`remove` above unless `--force` is used. To remove a locked worktree, specify `--force` twice. `-b `:: diff --git a/builtin/worktree.c b/builtin/worktree.c index 77ecd0f71f..7f53215cda 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -26,6 +26,7 @@ #include "submodule.h" #include "utf8.h" #include "worktree.h" +#include "wt-status.h" #include "quote.h" #define BUILTIN_WORKTREE_ADD_USAGE \ @@ -1408,6 +1409,38 @@ static void check_clean_worktree(struct worktree *wt, original_path, ret); } +/* + * A rebase, "git am", bisect, cherry-pick or revert keeps its state in + * the worktree's own $GIT_DIR, and removing the worktree throws that + * away even when "git status" says the files are clean. + */ +static void check_no_operation_in_progress(struct worktree *wt, + const char *original_path) +{ + struct wt_status_state state = { 0 }; + + if (wt_status_check_rebase(wt, &state)) { + if (state.am_in_progress) + die(_("'%s' is in the middle of an am session, use --force to delete it"), + original_path); + die(_("'%s' is in the middle of a rebase, use --force to delete it"), + original_path); + } + if (wt_status_check_bisect(wt, &state)) + die(_("'%s' is in the middle of a bisect, use --force to delete it"), + original_path); + wt_status_state_free_buffers(&state); + if (refs_ref_exists(get_worktree_ref_store(wt), "CHERRY_PICK_HEAD")) + die(_("'%s' is in the middle of a cherry-pick, use --force to delete it"), + original_path); + if (refs_ref_exists(get_worktree_ref_store(wt), "REVERT_HEAD")) + die(_("'%s' is in the middle of a revert, use --force to delete it"), + original_path); + if (is_directory(worktree_git_path(wt, "sequencer"))) + die(_("'%s' is in the middle of a cherry-pick or revert, use --force to delete it"), + original_path); +} + static int delete_git_work_tree(struct worktree *wt) { struct strbuf sb = STRBUF_INIT; @@ -1461,8 +1494,10 @@ static int remove_worktree(int ac, const char **av, const char *prefix, strbuf_release(&errmsg); if (file_exists(wt->path)) { - if (!force) + if (!force) { + check_no_operation_in_progress(wt, av[0]); check_clean_worktree(wt, av[0]); + } ret |= delete_git_work_tree(wt); } diff --git a/t/t2403-worktree-move.sh b/t/t2403-worktree-move.sh index 69768c1207..2e45cd77f9 100755 --- a/t/t2403-worktree-move.sh +++ b/t/t2403-worktree-move.sh @@ -271,4 +271,98 @@ test_expect_success 'move worktree with relative path to absolute path' ' test_cmp expect .git/worktrees/absolute/gitdir ' +test_expect_success 'setup commits that conflict for am, cherry-pick and revert' ' + git worktree add --detach conflict-setup init && + ( + cd conflict-setup && + test_commit base conflict.t base && + git switch --detach init && + test_commit side conflict.t side && + test_commit side-two + ) && + git worktree remove conflict-setup +' + +test_expect_success 'remove worktree in the middle of a rebase' ' + test_when_finished "rm -rf mid-rebase; git worktree prune" && + git worktree add --detach mid-rebase side && + test_must_fail git -C mid-rebase rebase -f -x false init && + test_must_fail git worktree remove mid-rebase 2>err && + test_grep "in the middle of a rebase" err && + test_path_is_dir mid-rebase && + git worktree remove --force mid-rebase && + test_path_is_missing mid-rebase +' + +test_expect_success 'remove worktree in the middle of an am session' ' + test_when_finished "rm -rf mid-am patch; git worktree prune" && + git format-patch -1 --stdout side >patch && + git worktree add --detach mid-am base && + ( + cd mid-am && + test_must_fail git am ../patch && + git reset --hard + ) && + test_must_fail git worktree remove mid-am 2>err && + test_grep "in the middle of an am session" err && + git worktree remove --force mid-am +' + +test_expect_success 'remove worktree in the middle of a bisect' ' + test_when_finished "rm -rf mid-bisect; git worktree prune" && + git worktree add --detach mid-bisect side-two && + git -C mid-bisect bisect start side-two init && + test_must_fail git worktree remove mid-bisect 2>err && + test_grep "in the middle of a bisect" err && + git worktree remove --force mid-bisect +' + +test_expect_success 'remove worktree in the middle of a cherry-pick' ' + test_when_finished "rm -rf mid-pick; git worktree prune" && + git worktree add --detach mid-pick base && + ( + cd mid-pick && + test_must_fail git cherry-pick side && + git checkout --ours conflict.t && + git add conflict.t && + git rev-parse --verify CHERRY_PICK_HEAD + ) && + test_path_is_missing .git/worktrees/mid-pick/sequencer && + test_must_fail git worktree remove mid-pick 2>err && + test_grep "in the middle of a cherry-pick," err && + git worktree remove --force mid-pick +' + +test_expect_success 'remove worktree in the middle of a revert' ' + test_when_finished "rm -rf mid-revert; git worktree prune" && + git worktree add --detach mid-revert base && + ( + cd mid-revert && + test_must_fail git revert side && + git checkout --ours conflict.t && + git add conflict.t && + git rev-parse --verify REVERT_HEAD + ) && + test_path_is_missing .git/worktrees/mid-revert/sequencer && + test_must_fail git worktree remove mid-revert 2>err && + test_grep "in the middle of a revert" err && + git worktree remove --force mid-revert +' + +test_expect_success 'remove worktree in the middle of a cherry-pick sequence' ' + test_when_finished "rm -rf mid-pick-seq; git worktree prune" && + git worktree add --detach mid-pick-seq base && + ( + cd mid-pick-seq && + test_must_fail git cherry-pick side side-two && + git checkout --theirs conflict.t && + git add conflict.t && + git commit --no-edit + ) && + test_path_is_dir .git/worktrees/mid-pick-seq/sequencer && + test_must_fail git worktree remove mid-pick-seq 2>err && + test_grep "in the middle of a cherry-pick or revert" err && + git worktree remove --force mid-pick-seq +' + test_done -- 2.56.0