* [PATCH 0/2] rm: revisiting --staged and --worktree
@ 2026-12-05 7:19 Adam T via GitGitGadget
2026-12-05 7:19 ` [PATCH 1/2] rm: add " adam torres encarnacion via GitGitGadget
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Adam T via GitGitGadget @ 2026-12-05 7:19 UTC (permalink / raw)
To: git
Cc: Junio C Hamano, Elijah Newren,
Nguyễn Thái Ngọc Duy, Adam T
RFC: I'd like feedback on some design choices below.
rm uses --cached while some (newer) commands like restore use --staged. In
[1], a discussion raised this confusion, advocating for an alias. Junio
objected, saying it adds nothing unless rm gets a working-tree-only target.
This series introduces a working-tree-only target for rm and aims to restart
conversations about a wider push to --staged in user-facing commands (see
question #2 below). It also makes --cached an alias for --staged & preserves
existing default behaviors.
Open questions:
1. How to handle git rm --worktree attempts? There are three approaches:
(1) remove the working tree & keep .gitmodules; (2) remove the working
tree & remove the .gitmodules entry; (3) refuse the request. (1) could
work but it duplicates git submodule deinit, (2) modifies the index,
which is probably wrong for a flag that aims to be "worktree-only." I
chose (3) and added a hint to use git submodule deinit instead since
that's the more explicit command.
2. In [2], Duy stated:
> * The patch 'rm --staged' is dropped. It could come back. But if it
> does, it should be in a series where commands that take
> --cached/--index will now take both --staged/--worktree. Those are
> 'rm', 'apply', 'check-attr', 'grep', 'diff' and maybe 'ls-files'.
While this series only introduces the feature for rm, I generally agree
with this. It makes --worktree and --staged the standard, which reduces
the friction you'd expect while using Git. I have the bandwidth to pick
them up, but I wanted to draw some feedback before attempting it.
[1] https://lore.kernel.org/git/xmqq4l73ztxf.fsf@gitster-ct.c.googlers.com/
[2] https://lore.kernel.org/git/20190425094600.15673-1-pclouds@gmail.com/
adam torres encarnacion (2):
rm: add --staged and --worktree
rm: relax --worktree-only restrictions
Documentation/git-rm.adoc | 41 ++++++++++++--------
builtin/rm.c | 71 ++++++++++++++++++++++++-----------
t/t3600-rm.sh | 79 +++++++++++++++++++++++++++++++++++++--
3 files changed, 150 insertions(+), 41 deletions(-)
base-commit: 566b6997dd4710b625c0882cf10a7443bf52d183
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2349%2F0xABAN%2Frm-staged-worktree-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2349/0xABAN/rm-staged-worktree-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2349
--
gitgitgadget
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 1/2] rm: add --staged and --worktree 2026-12-05 7:19 [PATCH 0/2] rm: revisiting --staged and --worktree Adam T via GitGitGadget @ 2026-12-05 7:19 ` adam torres encarnacion via GitGitGadget 2026-12-05 15:15 ` D. Ben Knoble 2026-12-05 7:19 ` [PATCH 2/2] rm: relax --worktree-only restrictions adam torres encarnacion via GitGitGadget 2026-12-05 15:15 ` [PATCH 0/2] rm: revisiting --staged and --worktree D. Ben Knoble 2 siblings, 1 reply; 7+ messages in thread From: adam torres encarnacion via GitGitGadget @ 2026-12-05 7:19 UTC (permalink / raw) To: git Cc: Junio C Hamano, Elijah Newren, Nguyễn Thái Ngọc Duy, Adam T, adam torres encarnacion From: adam torres encarnacion <admr0805@gmail.com> `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 <admr0805@gmail.com> --- 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=<file> [--pathspec-file-nul]] +git rm [-f | --force] [-n] [-r] [-W | --worktree] [-S | --staged] + [--cached] [--ignore-unmatch] [--quiet] + [--pathspec-from-file=<file> [--pathspec-file-nul]] [--] [<pathspec>...] 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 _<pathspec>_ 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.<name>` 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.<name>` +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=<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=<file> [--pathspec-file-nul]]\n" " [--] [<pathspec>...]"), 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 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] rm: add --staged and --worktree 2026-12-05 7:19 ` [PATCH 1/2] rm: add " adam torres encarnacion via GitGitGadget @ 2026-12-05 15:15 ` D. Ben Knoble 2026-12-05 16:32 ` Adam 0 siblings, 1 reply; 7+ messages in thread From: D. Ben Knoble @ 2026-12-05 15:15 UTC (permalink / raw) To: adam torres encarnacion via GitGitGadget Cc: git, Junio C Hamano, Elijah Newren, Nguyễn Thái Ngọc Duy, adam torres encarnacion On Sat, Dec 5, 2026 at 2:19 AM adam torres encarnacion via GitGitGadget <gitgitgadget@gmail.com> wrote: > > From: adam torres encarnacion <admr0805@gmail.com> > > `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. Mental note: "command runs --option" is not really how our docs talk about a command line, is it? > 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 <admr0805@gmail.com> > --- > 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=<file> [--pathspec-file-nul]] > +git rm [-f | --force] [-n] [-r] [-W | --worktree] [-S | --staged] > + [--cached] [--ignore-unmatch] [--quiet] > + [--pathspec-from-file=<file> [--pathspec-file-nul]] > [--] [<pathspec>...] > > 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, Hm. Do we really want to lose this limitation? It is more consistent to not reject "git rm -W" without "-S", and I suppose we don't risk losing data. I'm just wondering why this existed in the first place---the docs blame points to 25dc720077 (Clarify and fix English in "git-rm" documentation, 2008-04-16), but that doesn't really explain where the limitation came from. So, maybe it's ok to have this mode now. > 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 Here's that "run --option" construct again: the form we usually use is "When --option is given" or similar. Let's stick with that. > 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 _<pathspec>_ 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.<name>` 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.<name>` > +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=<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=<file> [--pathspec-file-nul]]\n" > " [--] [<pathspec>...]"), > 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 > > -- D. Ben Knoble ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] rm: add --staged and --worktree 2026-12-05 15:15 ` D. Ben Knoble @ 2026-12-05 16:32 ` Adam 0 siblings, 0 replies; 7+ messages in thread From: Adam @ 2026-12-05 16:32 UTC (permalink / raw) To: D. Ben Knoble Cc: git, Junio C Hamano, Elijah Newren, Nguyễn Thái Ngọc Duy, adam torres encarnacion via GitGitGadget On Sat, Dec 5, 2026 at 10:15 AM D. Ben Knoble <ben.knoble@gmail.com> wrote: > Mental note: "command runs --option" is not really how our docs talk > about a command line, is it? 👍 > Hm. Do we really want to lose this limitation? It is more consistent > to not reject "git rm -W" without "-S", and I suppose we don't risk > losing data. > > I'm just wondering why this existed in the first place---the docs > blame points to 25dc720077 (Clarify and fix English in "git-rm" > documentation, 2008-04-16), but that doesn't really explain where the > limitation came from. It seems that the original git rm just wasn't designed that way. Most of the core features were decided in 9f95069beb (git-rm: update to saner semantics, 2006-12-25) by Junio, and this version of git rm didn't have a use for -W. But now there's git restore, and adding the flag gives us the benefit of consistency Adam ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] rm: relax --worktree-only restrictions 2026-12-05 7:19 [PATCH 0/2] rm: revisiting --staged and --worktree Adam T via GitGitGadget 2026-12-05 7:19 ` [PATCH 1/2] rm: add " adam torres encarnacion via GitGitGadget @ 2026-12-05 7:19 ` adam torres encarnacion via GitGitGadget 2026-12-05 15:15 ` [PATCH 0/2] rm: revisiting --staged and --worktree D. Ben Knoble 2 siblings, 0 replies; 7+ messages in thread From: adam torres encarnacion via GitGitGadget @ 2026-12-05 7:19 UTC (permalink / raw) To: git Cc: Junio C Hamano, Elijah Newren, Nguyễn Thái Ngọc Duy, Adam T, adam torres encarnacion From: adam torres encarnacion <admr0805@gmail.com> `git rm` refuses to delete paths from the working tree and index if information will be lost. This guard is too restrictive when `--worktree` is used without `--staged` (since the changes will remain in the index & therefore won't be lost). Add a new branch in `check_local_mod` to handle `--worktree` only behavior. Block only if the file differs from the index. Signed-off-by: adam torres encarnacion <admr0805@gmail.com> --- builtin/rm.c | 28 +++++++++++++++++----------- t/t3600-rm.sh | 25 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/builtin/rm.c b/builtin/rm.c index 4160bdefe4..1f0bfbec8a 100644 --- a/builtin/rm.c +++ b/builtin/rm.c @@ -98,7 +98,7 @@ static void submodules_absorb_gitdir_if_needed(void) } } -static int check_local_mod(struct object_id *head, int index_only) +static int check_local_mod(struct object_id *head, int worktree, int staged) { /* * Items in list are already sorted in the cache order, @@ -197,16 +197,21 @@ 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 --staged definitely - * will lose information; (2) "git rm --staged" will - * lose information unless it is about removing an - * "intent to add" entry. + * either, (1) "git rm" definitely will lose information; + * (2) "git rm --staged" will lose information unless it + * is about removing an "intent to add" entry. + * "git rm --worktree" keeps the index entry, so only + * local changes are at risk. */ - if (local_changes && staged_changes) { - if (!index_only || !ce_intent_to_add(ce)) + if (worktree && !staged) { + if (local_changes) + string_list_append(&files_local, name); + } + else if (local_changes && staged_changes) { + if (worktree || !ce_intent_to_add(ce)) string_list_append(&files_staged, name); } - else if (!index_only) { + else if (worktree) { if (staged_changes) string_list_append(&files_cached, name); if (local_changes) @@ -236,8 +241,9 @@ 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 --staged to keep the file," - " or -f to force removal)"), + staged ? _("\n(use --staged to keep the file," + " or -f to force removal)") + : _("\n(use -f to force removal)"), &errs); string_list_clear(&files_local, 0); @@ -403,7 +409,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, !worktree)) + if (check_local_mod(&oid, worktree, staged)) exit(1); } diff --git a/t/t3600-rm.sh b/t/t3600-rm.sh index d12c3448d4..a1bc8cca22 100755 --- a/t/t3600-rm.sh +++ b/t/t3600-rm.sh @@ -203,6 +203,31 @@ test_expect_success 'rm --worktree deletes the file and keeps it in the index' ' git ls-files --error-unmatch worktree-only ' +test_expect_success 'rm --worktree allows changes staged in the index' ' + echo content >worktree-staged && + git add worktree-staged && + git rm --worktree worktree-staged && + test_path_is_missing worktree-staged && + git ls-files --error-unmatch worktree-staged +' + +test_expect_success 'rm --worktree refuses to drop local modifications' ' + cat >expect <<-\EOF && + error: the following file has local modifications: + worktree-local + (use -f to force removal) + EOF + echo content >worktree-local && + git add worktree-local && + echo changed >worktree-local && + test_must_fail git rm --worktree worktree-local 2>actual && + test_cmp expect actual && + test_path_is_file worktree-local && + git rm -f --worktree worktree-local && + test_path_is_missing worktree-local && + git ls-files --error-unmatch worktree-local +' + test_expect_success 'rm --staged --worktree removes the file from both' ' echo content >staged-and-worktree && git add staged-and-worktree && -- gitgitgadget ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 0/2] rm: revisiting --staged and --worktree 2026-12-05 7:19 [PATCH 0/2] rm: revisiting --staged and --worktree Adam T via GitGitGadget 2026-12-05 7:19 ` [PATCH 1/2] rm: add " adam torres encarnacion via GitGitGadget 2026-12-05 7:19 ` [PATCH 2/2] rm: relax --worktree-only restrictions adam torres encarnacion via GitGitGadget @ 2026-12-05 15:15 ` D. Ben Knoble 2026-12-05 16:32 ` Adam 2 siblings, 1 reply; 7+ messages in thread From: D. Ben Knoble @ 2026-12-05 15:15 UTC (permalink / raw) To: Adam T via GitGitGadget Cc: git, Junio C Hamano, Elijah Newren, Nguyễn Thái Ngọc Duy, Adam T On Sat, Dec 5, 2026 at 2:19 AM Adam T via GitGitGadget <gitgitgadget@gmail.com> wrote: > > RFC: I'd like feedback on some design choices below. > > rm uses --cached while some (newer) commands like restore use --staged. In > [1], a discussion raised this confusion, advocating for an alias. Junio > objected, saying it adds nothing unless rm gets a working-tree-only target. > This series introduces a working-tree-only target for rm and aims to restart > conversations about a wider push to --staged in user-facing commands (see > question #2 below). It also makes --cached an alias for --staged & preserves > existing default behaviors. "git rm --worktree" is likely to sound like "git worktree remove"---we probably want a different name? The concepts are similar, but very distinct I think. And yet: restore already uses --worktree, so I guess we're going to go that route. I'll enjoy the consistency, and I don't know what *other* name we could have used :shrug: > Open questions: > > 1. How to handle git rm --worktree attempts? There are three approaches: (This must mean "git rm --worktree <submodule> attempts" based on [1/2]; better to be clear.) > (1) remove the working tree & keep .gitmodules; (2) remove the working > tree & remove the .gitmodules entry; (3) refuse the request. (1) could > work but it duplicates git submodule deinit, (2) modifies the index, > which is probably wrong for a flag that aims to be "worktree-only." I > chose (3) and added a hint to use git submodule deinit instead since > that's the more explicit command. (3) sounds right to me; it's convenient that git-mv, git-rm work on submodules, but sometimes I wonder if it's too much magic ;) > 2. In [2], Duy stated: > > > * The patch 'rm --staged' is dropped. It could come back. But if it > > does, it should be in a series where commands that take > > --cached/--index will now take both --staged/--worktree. Those are > > 'rm', 'apply', 'check-attr', 'grep', 'diff' and maybe 'ls-files'. > > While this series only introduces the feature for rm, I generally agree > with this. It makes --worktree and --staged the standard, which reduces > the friction you'd expect while using Git. I have the bandwidth to pick > them up, but I wanted to draw some feedback before attempting it. > > [1] https://lore.kernel.org/git/xmqq4l73ztxf.fsf@gitster-ct.c.googlers.com/ > [2] https://lore.kernel.org/git/20190425094600.15673-1-pclouds@gmail.com/ > > adam torres encarnacion (2): > rm: add --staged and --worktree > rm: relax --worktree-only restrictions Small comment on [2/2]: "--worktree-only" in the subject looks like an option which doesn't exist, even though it's really a hyphenated "--worktree" and "only". Maybe "relax worktree-only restrictions" avoids that confusion? > Documentation/git-rm.adoc | 41 ++++++++++++-------- > builtin/rm.c | 71 ++++++++++++++++++++++++----------- > t/t3600-rm.sh | 79 +++++++++++++++++++++++++++++++++++++-- > 3 files changed, 150 insertions(+), 41 deletions(-) > > > base-commit: 566b6997dd4710b625c0882cf10a7443bf52d183 > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2349%2F0xABAN%2Frm-staged-worktree-v1 > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2349/0xABAN/rm-staged-worktree-v1 > Pull-Request: https://github.com/gitgitgadget/git/pull/2349 > -- > gitgitgadget > -- D. Ben Knoble ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 0/2] rm: revisiting --staged and --worktree 2026-12-05 15:15 ` [PATCH 0/2] rm: revisiting --staged and --worktree D. Ben Knoble @ 2026-12-05 16:32 ` Adam 0 siblings, 0 replies; 7+ messages in thread From: Adam @ 2026-12-05 16:32 UTC (permalink / raw) To: D. Ben Knoble Cc: git, Junio C Hamano, Elijah Newren, Nguyễn Thái Ngọc Duy, Adam T via GitGitGadget On Sat, Dec 5, 2026 at 10:15 AM D. Ben Knoble <ben.knoble@gmail.com> wrote: > (This must mean "git rm --worktree <submodule> attempts" based on > [1/2]; better to be clear.) That's what I meant, seems to be a formatting issue on my end (sorry about that) > Small comment on [2/2]: "--worktree-only" in the subject looks like an > option which doesn't exist, even though it's really a hyphenated > "--worktree" and "only". Maybe "relax worktree-only restrictions" > avoids that confusion? 👍 Adam ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-05 16:32 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-12-05 7:19 [PATCH 0/2] rm: revisiting --staged and --worktree Adam T via GitGitGadget 2026-12-05 7:19 ` [PATCH 1/2] rm: add " adam torres encarnacion via GitGitGadget 2026-12-05 15:15 ` D. Ben Knoble 2026-12-05 16:32 ` Adam 2026-12-05 7:19 ` [PATCH 2/2] rm: relax --worktree-only restrictions adam torres encarnacion via GitGitGadget 2026-12-05 15:15 ` [PATCH 0/2] rm: revisiting --staged and --worktree D. Ben Knoble 2026-12-05 16:32 ` Adam
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox