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=1796455148; b=My60o0PHWbXcEZifFPT4zjeVJyh73LJWM5AjiKZtEb8fEYDWrCK081Tz4qlsHjo1CVEj DEYsafPE6UfzTBaQdju92xBG7hE0nG/a8ISHfWYtrX6XpYgu7FEl26M3RnLp3h512kGMT GqUjJROY2On29i2eROqaL3vO3U1NxgsJKYIhI6zn1ZSspohU6nzlRVuIFHcoEeqW5yu/A mLSuuxoiHOo2fcyu2sfOGoUAnc+4Y9gcyNMErfaqY4BurKL7aIDl84kS8jdQvDiNUotTQ W5flt0MWuqClbC4kYw/md5yoOxpB2ZH8qSbvlhUam/0wkiNvV3UHTqIk9TLYX2/KABQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796455148; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:In-Reply-To: References:From:Date:Subject:Fcc:Content-Type: Content-Transfer-Encoding:MIME-Version:To:Cc; bh=8QPY4U1/uNkbnQfxg5FSdq77lts/+pUnqg0Km//S0XM=; b=2sGIAxSaVWhMQEdGGHegTLoqJFsrZKfHRJ7JJiq/ZFXF9Y/f9cCK4lT4WDQUYHf/QQvt o+NyPGw8zTxXi7Fns5BKcJ8tLIZMAACHrFdtWN7S6JtLlZM4GKxf9LL5frmw40NulTYjE VFcsTxLmMzdggvHyJh9rZFEMeusnxwjIEm307IFVL07eSbhHtxDGjtuNoncRnVTXf9Rgb 0BOhVBucgplOF7De1huv/p1CuGcugyFymlAMQ8tVXsgI8tPnw7NBMswhtiwjex+Tf60X+ f24yvWVRT670Y3ew0X2/I8IQHlTf7yq5I3VtFlwcrQtNSklKFuC8Z2FCZWLlpvYjxGw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=wawGJ1gJ; 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 00D07606A336 for ; Sat, 5 Dec 2026 07:19:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 00D07606A336 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=wawGJ1gJ Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-94a7cf5badbso234157385a.1 for ; Fri, 04 Dec 2026 23:19:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796455147; x=1797059947; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=8QPY4U1/uNkbnQfxg5FSdq77lts/+pUnqg0Km//S0XM=; b=wawGJ1gJDhbroW8+P5jR3K895V1ekXxmTtiXefDgNrNLiU1MocWX6fsmzDj0SMfVnR QtX4O+NoEKHOlxqNn22opyjrTDYu99WyI2gNRSU92vrD6UYLticrZWc4VLFCk9QQ8p6Q fJM6WI7i7KpSLLz91+uxpktfd5YYPAEsqXbSESnk0oZzFNCC4EqPadiBXPAxhO2kYhcZ x3k6x3Wl4t2r5dyEKDT8Am1Pk5i3xTPWayGNcpSBFy6pSF+ZJv4tJWlG84E5BL2XQU8Y p55TQb6JLMfNybOxAcEd/imELH9P3Vynm3w8NoGDM0SHIad/ngcC63hHFzh5kKaz1Jmb N44w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796455147; x=1797059947; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=8QPY4U1/uNkbnQfxg5FSdq77lts/+pUnqg0Km//S0XM=; b=WQ51A+Isj3Dbvhkhv+0O90F7Zwb9SsBz7mta8UpW6fVJiz/O7ds5qtmv/K57RT7dH/ TVfHm1BNtxJ9iaSg4iNEdmv+6peIYmpcDQ3yMkOGsw2FzlSmBpJfIAPJja2kvZRGlxrJ hghD8nu4lsU97vIo486oN29cOhPEhEl/TITZ9KHwB+bIj5TP1lG3BwpRkFKAMoJ4YRKD JYATCuSYl1ie7v3EDQJ0gSQbRo2dtrlwL5q9ZNRyYczK/MHF+Ocvm5r8oVCExrH/Mcpm 8IP7GO1h5zFxznN9uIZGxWRz2UcYtYBY6Tukf+WIKdZosRftieFeIfuwI43riJV19yZD i48A== X-Gm-Message-State: APuU4Z/qPc2+LLXcMewM6JD9+1iG5/HjXnruCH8C4WaGTT0ONSUw8OVD JMdpM3R5M8tjryaloONdVdmxAmpM0C3pjVTwrOU8tfZKiUPd8h6x6H/lmbTKl+Ag X-Gm-Gg: AZFdTZcfGXc8hhbUwtxzkyAtH5+AXSCDwlSqi7qUTHMExE3ucNKPU0iuIhIsO7Hx49s mDCodMF8NBGYhI4S3Wktz465aXSMKgbHSZKGf4STe5B+WLB0CT/VDWpM9QOdvVZdFNIGK/J8liD +XBjpveOtwBjTeQaoK4anDoMISTYl6HK6V1gBfIo3mqt3rjvCKtaafUPyFCzz2gwHVEY4Yt+2yu AQsvfvjrc9wvLyZJdKKIIXD+gEDHLFRtYjnb2IPmgaZtyS6IYZl8fnbbg1pevNHWL48Ao4prlPl LmJTplDFEOOOaYBxLKJBBiJMkDrJok8qWfwRDgdKvrahcVVJPd9Cl5i+e123x4yMHp3qCwQkcz+ PkFeUnghCwjef8wM/wWR5P8g/yjflbENhQOGi3W3ccvWI4cItwfFr4n5fsatzYLfdnJDsbeFxCJ 96YCJh+fftBYU4TkKZHnN45AQKGlhc9xAxmosaFulqydVnWJrz5F0I8wLwMoyUUZY+UD3iKsCNK l8LHVpcov+crdSYdVKU27m/hxwYDG77Opmat6oPJm2PHoC4thFfgCwWdtYvwkCWA/0CX3uxiCyA LijYu9LJxvCpCJP8Hk5VPixNEU6bHsVa2Q== X-Received: by 2002:a05:620a:2699:b0:93f:4a02:ed99 with SMTP id af79cd13be357-94a722d185amr1346015885a.20.1796455146734; Fri, 04 Dec 2026 23:19:06 -0800 (PST) Received: from [127.0.0.1] ([20.81.230.132]) by smtp.gmail.com with ESMTPSA id af79cd13be357-94a7f47d6f9sm423723785a.10.2026.12.04.23.19.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 23:19:06 -0800 (PST) Message-Id: <66bb3de1d1caf138cf04d23bb8e6dcc9238af4a8.1796455144.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "adam torres encarnacion via GitGitGadget" Date: Sat, 05 Dec 2026 07:19:03 +0000 Subject: [PATCH 1/2] rm: add --staged and --worktree Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 To: git@vger.kernel.org Cc: Junio C Hamano , Elijah Newren , =?UTF-8?Q?Nguy=E1=BB=85n_Th=C3=A1i_Ng=E1=BB=8Dc?= Duy , Adam T , adam torres encarnacion From: adam torres encarnacion `git rm` has no way to remove paths from the working tree only. Further, the lack of consistency between `rm` (which uses `--cached`) and commands like `restore` (which uses `--staged`) makes the interface confusing. In a 2019 discussion, Junio argued that a `--staged` alias just to achieve parity adds nothing unless `rm`, too, gains a working tree only option [1]. Add `--worktree`, `--staged`, and make `--cached` a synonym for `--staged`, matching `git restore`. By default, `git rm` runs `--worktree` and `--staged`, which is logically equivalent to what we already do. Refuse `git rm --worktree` without `--staged` on a submodule & tell the user to run `git submodule deinit` instead. This is because 1.) deleting the folder & removing its entry from `.gitmodules` would affect the index, which is weird for a "worktree only" flag, and 2.) `git submodule deinit` already covers removing only a submodule's working tree. [1] https://lore.kernel.org/git/xmqq4l73ztxf.fsf@gitster-ct.c.googlers.com/ Signed-off-by: adam torres encarnacion --- Documentation/git-rm.adoc | 41 +++++++++++++++++------------ builtin/rm.c | 51 +++++++++++++++++++++++++----------- t/t3600-rm.sh | 54 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 112 insertions(+), 34 deletions(-) diff --git a/Documentation/git-rm.adoc b/Documentation/git-rm.adoc index 67061e961f..0ac47b381e 100644 --- a/Documentation/git-rm.adoc +++ b/Documentation/git-rm.adoc @@ -8,20 +8,19 @@ git-rm - Remove files from the working tree and from the index SYNOPSIS -------- [synopsis] -git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] - [--quiet] [--pathspec-from-file= [--pathspec-file-nul]] +git rm [-f | --force] [-n] [-r] [-W | --worktree] [-S | --staged] + [--cached] [--ignore-unmatch] [--quiet] + [--pathspec-from-file= [--pathspec-file-nul]] [--] [...] DESCRIPTION ----------- -Remove files matching pathspec from the index, or from the working tree -and the index. `git rm` will not remove a file from just your working -directory. (There is no option to remove a file only from the working -tree and yet keep it in the index; use `/bin/rm` if you want to do -that.) The files being removed have to be identical to the tip of the -branch, and no updates to their contents can be staged in the index, +Remove files matching pathspec from the index, the working tree, or +both. By default, `git rm` removes from both. The files being removed +have to be identical to the tip of the branch, and no updates to their +contents can be staged in the index, though that default behavior can be overridden with the `-f` option. -When `--cached` is given, the staged content has to +When running `--staged` without `--worktree`, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index. When sparse-checkouts are in use (see linkgit:git-sparse-checkout[1]), @@ -64,10 +63,18 @@ For more details, see the __ entry in linkgit:gitglossary[7]. the list of files (useful when filenames might be mistaken for command-line options). +`-W`:: +`--worktree`:: + Remove paths from the working tree. Paths stay in the + index unless paired with `--staged`. + +`-S`:: +`--staged`:: + Remove paths from the index. Paths stay in the working + tree, whether modified or not, unless paired with `--worktree`. + `--cached`:: - Use this option to unstage and remove paths only from the index. - Working tree files, whether modified or not, will be - left alone. + Synonym for `--staged`. `--ignore-unmatch`:: Exit with a zero status even if no files matched. @@ -146,7 +153,7 @@ your working tree is dirty so that you cannot use `git commit -a`), use the following command: ---------------- -git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached +git diff --name-only --diff-filter=D -z | xargs -0 git rm --staged ---------------- SUBMODULES @@ -157,9 +164,11 @@ tree, as their repository lives inside the `.git` directory of the superproject. If a submodule (or one of those nested inside it) still uses a `.git` directory, `git rm` moves the submodules git directory into the superprojects git directory to protect -the submodule's history. If it exists the `submodule.` section -in the linkgit:gitmodules[5] file will also be removed and that file -will be staged (unless `--cached` or `-n` are used). +the submodule's history. If it exists the `submodule.` +section in the linkgit:gitmodules[5] file will also be removed and +that file will be staged (unless `--staged` is used without `--worktree`, +or `-n` is used). Using `--worktree` without `--staged` on a submodule is +refused; see `deinit` below. A submodule is considered up to date when the `HEAD` is the same as recorded in the index, no tracked files are modified and no untracked diff --git a/builtin/rm.c b/builtin/rm.c index 081d0bc375..4160bdefe4 100644 --- a/builtin/rm.c +++ b/builtin/rm.c @@ -26,8 +26,9 @@ #include "pathspec.h" static const char * const builtin_rm_usage[] = { - N_("git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]\n" - " [--quiet] [--pathspec-from-file= [--pathspec-file-nul]]\n" + N_("git rm [-f | --force] [-n] [-r] [-W | --worktree] [-S | --staged]\n" + " [--cached] [--ignore-unmatch] [--quiet]\n" + " [--pathspec-from-file= [--pathspec-file-nul]]\n" " [--] [...]"), NULL }; @@ -164,7 +165,7 @@ static int check_local_mod(struct object_id *head, int index_only) * the current commit in the index. * * In such a case, you would need to --force the - * removal. However, "rm --cached" (remove only from + * removal. However, "rm --staged" (remove only from * the index) is safe if the index matches the file in * the work tree or the HEAD commit, as it means that * the content being removed is available elsewhere. @@ -196,8 +197,8 @@ static int check_local_mod(struct object_id *head, int index_only) /* * If the index does not match the file in the work * tree and if it does not match the HEAD commit - * either, (1) "git rm" without --cached definitely - * will lose information; (2) "git rm --cached" will + * either, (1) "git rm" without --staged definitely + * will lose information; (2) "git rm --staged" will * lose information unless it is about removing an * "intent to add" entry. */ @@ -226,7 +227,7 @@ static int check_local_mod(struct object_id *head, int index_only) "staged in the index:", "the following files have changes " "staged in the index:", files_cached.nr), - _("\n(use --cached to keep the file," + _("\n(use --staged to keep the file," " or -f to force removal)"), &errs); string_list_clear(&files_cached, 0); @@ -235,7 +236,7 @@ static int check_local_mod(struct object_id *head, int index_only) Q_("the following file has local modifications:", "the following files have local modifications:", files_local.nr), - _("\n(use --cached to keep the file," + _("\n(use --staged to keep the file," " or -f to force removal)"), &errs); string_list_clear(&files_local, 0); @@ -243,7 +244,8 @@ static int check_local_mod(struct object_id *head, int index_only) return errs; } -static int show_only = 0, force = 0, index_only = 0, recursive = 0, quiet = 0; +static int show_only = 0, force = 0, recursive = 0, quiet = 0; +static int worktree = -1, staged = -1; static int ignore_unmatch = 0, pathspec_file_nul; static int include_sparse; static char *pathspec_from_file; @@ -251,7 +253,9 @@ static char *pathspec_from_file; static struct option builtin_rm_options[] = { OPT__DRY_RUN(&show_only, N_("dry run")), OPT__QUIET(&quiet, N_("do not list removed files")), - OPT_BOOL( 0 , "cached", &index_only, N_("only remove from the index")), + OPT_BOOL('S', "staged", &staged, N_("remove from the index")), + OPT_BOOL('W', "worktree", &worktree, N_("remove from the working tree")), + OPT_BOOL( 0 , "cached", &staged, N_("synonym for --staged")), OPT__FORCE(&force, N_("override the up-to-date check"), PARSE_OPT_NOCOMPLETE), OPT_BOOL('r', NULL, &recursive, N_("allow recursive removal")), OPT_BOOL( 0 , "ignore-unmatch", &ignore_unmatch, @@ -277,6 +281,19 @@ int cmd_rm(int argc, argc = parse_options(argc, argv, prefix, builtin_rm_options, builtin_rm_usage, 0); + if (worktree == -1 && staged == -1) { + worktree = 1; + staged = 1; + } else { + if (worktree == -1) + worktree = 0; + if (staged == -1) + staged = 0; + if (!worktree && !staged) + die(_("neither '%s' or '%s' is specified"), + "--staged", "--worktree"); + } + parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD, prefix, argv); @@ -295,7 +312,7 @@ int cmd_rm(int argc, if (!pathspec.nr) die(_("No pathspec was given. Which files should I remove?")); - if (!index_only) + if (worktree) setup_work_tree(the_repository); prepare_repo_settings(the_repository); @@ -324,6 +341,10 @@ int cmd_rm(int argc, ALLOC_GROW(list.entry, list.nr + 1, list.alloc); list.entry[list.nr].name = xstrdup(ce->name); list.entry[list.nr].is_submodule = S_ISGITLINK(ce->ce_mode); + if (list.entry[list.nr].is_submodule && worktree && !staged) + die(_("cannot remove submodule '%s' with '%s' alone\n" + "(use 'git submodule deinit' to remove its working tree)"), + ce->name, "--worktree"); if (list.entry[list.nr++].is_submodule && !is_staging_gitmodules_ok(the_repository->index)) die(_("please stage your changes to .gitmodules or stash them to proceed")); @@ -365,7 +386,7 @@ int cmd_rm(int argc, clear_pathspec(&pathspec); free(seen); - if (!index_only) + if (worktree) submodules_absorb_gitdir_if_needed(); /* @@ -382,7 +403,7 @@ int cmd_rm(int argc, struct object_id oid; if (repo_get_oid(the_repository, "HEAD", &oid)) oidclr(&oid, the_repository->hash_algo); - if (check_local_mod(&oid, index_only)) + if (check_local_mod(&oid, !worktree)) exit(1); } @@ -395,7 +416,7 @@ int cmd_rm(int argc, if (!quiet) printf("rm '%s'\n", path); - if (remove_file_from_index(the_repository->index, path)) + if (staged && remove_file_from_index(the_repository->index, path)) die(_("git rm: unable to remove %s"), path); } @@ -403,14 +424,14 @@ int cmd_rm(int argc, return 0; /* - * Then, unless we used "--cached", remove the filenames from + * Then, if "--worktree" is in effect, remove the filenames from * the workspace. If we fail to remove the first one, we * abort the "git rm" (but once we've successfully removed * any file at all, we'll go ahead and commit to it all: * by then we've already committed ourselves and can't fail * in the middle) */ - if (!index_only) { + if (worktree) { int removed = 0, gitmodules_modified = 0; struct strbuf buf = STRBUF_INIT; int flag = force ? REMOVE_DIR_PURGE_ORIGINAL_CWD : 0; diff --git a/t/t3600-rm.sh b/t/t3600-rm.sh index a371ea690e..d12c3448d4 100755 --- a/t/t3600-rm.sh +++ b/t/t3600-rm.sh @@ -185,6 +185,42 @@ test_expect_success 'remove intent-to-add file without --force' ' git rm --cached intent-to-add ' +test_expect_success 'rm --staged keeps the file on disk and removes it from the index' ' + echo content >staged-only && + git add staged-only && + git commit -m "add staged-only" && + git rm --staged staged-only && + test_path_is_file staged-only && + test_must_fail git ls-files --error-unmatch staged-only +' + +test_expect_success 'rm --worktree deletes the file and keeps it in the index' ' + echo content >worktree-only && + git add worktree-only && + git commit -m "add worktree-only" && + git rm --worktree worktree-only && + test_path_is_missing worktree-only && + git ls-files --error-unmatch worktree-only +' + +test_expect_success 'rm --staged --worktree removes the file from both' ' + echo content >staged-and-worktree && + git add staged-and-worktree && + git commit -m "add staged-and-worktree" && + git rm --staged --worktree staged-and-worktree && + test_path_is_missing staged-and-worktree && + test_must_fail git ls-files --error-unmatch staged-and-worktree +' + +test_expect_success 'rm --no-staged --no-worktree fails' ' + echo content >neither && + git add neither && + git commit -m "add neither" && + test_must_fail git rm --no-staged --no-worktree neither && + test_path_is_file neither && + git ls-files --error-unmatch neither +' + test_expect_success 'Recursive test setup' ' mkdir -p frotz && echo qfwfq >frotz/nitfol && @@ -394,6 +430,18 @@ test_expect_success 'rm --cached leaves work tree of populated submodules and .g git config -f .gitmodules submodule.sub.path ' +test_expect_success 'rm --worktree on a submodule fails and leaves it alone' ' + git reset --hard && + git submodule update && + test_must_fail git rm --worktree submod 2>err && + test_grep "cannot remove submodule .submod. with .--worktree. alone" err && + test_path_is_dir submod && + test_path_is_file submod/.git && + git diff-index --exit-code HEAD && + git config -f .gitmodules submodule.sub.url && + git config -f .gitmodules submodule.sub.path +' + test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' ' git reset --hard && git submodule update && @@ -837,7 +885,7 @@ test_expect_success 'rm file with local modification' ' cat >expect <<-\EOF && error: the following file has local modifications: foo.txt - (use --cached to keep the file, or -f to force removal) + (use --staged to keep the file, or -f to force removal) EOF git commit -m "testing rm 3" && echo content3 >foo.txt && @@ -859,7 +907,7 @@ test_expect_success 'rm file with changes in the index' ' cat >expect <<-\EOF && error: the following file has changes staged in the index: foo.txt - (use --cached to keep the file, or -f to force removal) + (use --staged to keep the file, or -f to force removal) EOF git reset --hard && echo content5 >foo.txt && @@ -885,7 +933,7 @@ test_expect_success 'rm files with two different errors' ' (use -f to force removal) error: the following file has changes staged in the index: bar1.txt - (use --cached to keep the file, or -f to force removal) + (use --staged to keep the file, or -f to force removal) EOF echo content >foo1.txt && git add foo1.txt && -- gitgitgadget