From: "adam torres encarnacion via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
"Elijah Newren" <newren@gmail.com>,
"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
"Adam T" <admr0805@gmail.com>,
"adam torres encarnacion" <admr0805@gmail.com>
Subject: [PATCH 1/2] rm: add --staged and --worktree
Date: Sat, 05 Dec 2026 07:19:03 +0000 [thread overview]
Message-ID: <66bb3de1d1caf138cf04d23bb8e6dcc9238af4a8.1796455144.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2349.git.1796455144.gitgitgadget@gmail.com>
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
next prev parent reply other threads:[~2026-12-05 7:19 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-12-05 15:15 ` [PATCH 1/2] rm: add " 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=66bb3de1d1caf138cf04d23bb8e6dcc9238af4a8.1796455144.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=admr0805@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=newren@gmail.com \
--cc=pclouds@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox