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 (like dir, to remove dir/file1 and dir/file2) and that removes all files in the directory and recursively all sub-directories β€” but that requires the -r option 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 directories d and d2, there is a difference between using git rm 'd*' and git rm 'd/*', because the former will also remove all of directory d2. for more details, see the <pathspec> entry in gitglossary(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. normally git rm refuses to update index entries whose paths do not fit within the sparse-checkout cone πŸ‘€ see git-sparse-checkout(1) for more.
  • -q, --quiet β€” git rm normally outputs one line (in the form of an rm command) 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 variable core.quotePath (see git-config(1)). also see --pathspec-file-nul and 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 *.txt files from the index that are under the Documentation directory 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 the Documentation/ 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 remove subdir/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