GIT-RM(1)
π git-rm(1) β the delete-your-files era π
okay bestie so this is from the Git Manual bestie, and it's literally all about git-rm. yes. the one that removes stuff. buckle up π
β¨ SYNOPSIS β¨
git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]
[--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]
[--] [<pathspec>...]
that's the whole vibe check. moving on π
πΈ DESCRIPTION πΈ
so git rm removes files matching a pathspec from the index, or from the working tree and the index together. and this is the important part bestie, listen: git rm will not remove a file from just your working directory. it just won't. there is literally no option to remove a file only from the working tree while keeping it in the index β if that's your dream, use /bin/rm instead, that's what it's there for π«Ά
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 ngl that default behavior can be overridden with the -f option. (force is the vibe.)
when --cached is given, the staged content has to match either the tip of the branch or the file on disk β which is what allows the file to be removed from just the index β¨
and when sparse-checkouts are in use (see git-sparse-checkout(1)), git rm will only remove paths within the sparse-checkout patterns. periodt.
π OPTIONS π
<pathspec>...β the files to remove, bestie. a leading directory name can be given (likedir, to removedir/file1anddir/file2) and that removes all files in the directory and recursively all sub-directories β but that requires the-roption to be explicitly given. also, the command removes only the paths that are known to Git. and heads up: file globbing matches across directory boundaries π so given two directoriesdandd2, there is a difference between usinggit rm 'd*'andgit rm 'd/*', because the former will also remove all of directoryd2. for more details, see the<pathspec>entry ingitglossary(7).-f,--forceβ override the up-to-date check. πͺ-n,--dry-runβ don't actually remove any file(s)! instead just show if they exist in the index and would otherwise be removed by the command. it's giving window shopping ποΈ-rβ allow recursive removal when a leading directory name is given.--β this one separates command-line options from the list of files (super useful when filenames might be mistaken for command-line options, iykyk).--cachedβ use this to unstage and remove paths only from the index. working tree files, whether modified or not, will be left alone π«Ά--ignore-unmatchβ exit with a zero status even if no files matched. no match, no problem π--sparseβ allow updating index entries outside of the sparse-checkout cone. normallygit rmrefuses to update index entries whose paths do not fit within the sparse-checkout cone π seegit-sparse-checkout(1)for more.-q,--quietβgit rmnormally outputs one line (in the form of anrmcommand) for each file removed. this option suppresses that output. silence. iconic π€«--pathspec-from-file=<file>β the pathspec is passed in<file>instead of args. if<file>is exactly-then standard input is used. pathspec elements are separated by LF or CR/LF. pathspec elements can be quoted as explained for the configuration variablecore.quotePath(seegit-config(1)). also see--pathspec-file-nuland global--literal-pathspecsπ--pathspec-file-nulβ only meaningful with--pathspec-from-file. pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes) π¦
π§ REMOVING FILES THAT HAVE DISAPPEARED FROM THE FILESYSTEM π§
okay so there is no option for git rm to remove from the index only the paths that have disappeared from the filesystem. tragic, i know. but depending on the use case, there are several ways that can be done π
π Using "git commit -a" π
if you intend that your next commit should record all modifications of tracked files in the working tree and record all removals of files that have been removed from the working tree with rm (as opposed to git rm), use git commit -a β it will automatically notice and record all removals. you can also have a similar effect without committing by using git add -u β¨
π Using "git add -A" π
when accepting a new code drop for a vendor branch, you probably want to record both the removal of paths and additions of new paths, as well as modifications of existing paths.
typically you would first remove all tracked files from the working tree using this command:
git ls-files -z | xargs -0 rm -f
and then untar the new code in the working tree. alternately you could rsync the changes into the working tree π
after that, the easiest way to record all removals, additions, and modifications in the working tree is:
git add -A
see git-add(1) π
π Other ways π
if all you really want to do is remove from the index the files that are no longer present in the working tree (perhaps because 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
π¦ SUBMODULES π¦
only submodules using a gitfile (which means they were cloned with a Git version 1.7.8 or newer) will be removed from the work tree, since 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 submodule's git directory into the superproject's git directory to protect the submodule's history π«Ά
if it exists, the submodule.<name> section in the gitmodules(5) file will also be removed and that file will be staged (unless --cached or -n are used).
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 files that aren't ignored are present in the submodule's work tree. ignored files are deemed expendable and won't stop a submodule's work tree from being removed π
if you only want to remove the local checkout of a submodule from your work tree without committing the removal, use git-submodule(1) deinit instead. also see gitsubmodules(7) for details on submodule removal β¨
β EXAMPLES β
git rm Documentation/\*.txtβ removes all*.txtfiles from the index that are under theDocumentationdirectory and any of its subdirectories. note that the asterisk*is quoted from the shell in this example β this lets Git, and not the shell, expand the pathnames of files and subdirectories under theDocumentation/directory πgit rm -f git-*.shβ because this example lets the shell expand the asterisk (i.e. you are listing the files explicitly), it does not removesubdir/git-foo.sh. sneaky little difference, fr fr π
π BUGS π
each time a superproject update removes a populated submodule (e.g. when switching between commits before and after the removal) a stale submodule checkout will remain in the old location. removing the old directory is only safe when it uses a gitfile β as otherwise the history of the submodule will be deleted too π. this step will be obsolete when recursive submodule update has been implemented.
bestie please actually read that one, deleting submodule history is not the slay you think it is π
πΈ SEE ALSO πΈ
π GIT π
part of the git(1) suite π«Ά
and that's the whole page, bestie!! brought to you by Git 2.56.0 and dated 2026-09-27 β go forth and remove things responsibly π β¨π
π¦ where this page comes from
- arch rolling: git 2.56.0-1 Β· GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms