1GIT-RM(1)                         Git Manual                         GIT-RM(1)
2
3
4

NAME

6       git-rm - Remove files from the working tree and from the index
7

SYNOPSIS

9       git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]
10                 [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]
11                 [--] [<pathspec>...]
12

DESCRIPTION

14       Remove files matching pathspec from the index, or from the working tree
15       and the index. git rm will not remove a file from just your working
16       directory. (There is no option to remove a file only from the working
17       tree and yet keep it in the index; use /bin/rm if you want to do that.)
18       The files being removed have to be identical to the tip of the branch,
19       and no updates to their contents can be staged in the index, though
20       that default behavior can be overridden with the -f option. When
21       --cached is given, the staged content has to match either the tip of
22       the branch or the file on disk, allowing the file to be removed from
23       just the index. When sparse-checkouts are in use (see git-sparse-
24       checkout(1)), git rm will only remove paths within the sparse-checkout
25       patterns.
26

OPTIONS

28       <pathspec>...
29           Files to remove. A leading directory name (e.g.  dir to remove
30           dir/file1 and dir/file2) can be given to remove all files in the
31           directory, and recursively all sub-directories, but this requires
32           the -r option to be explicitly given.
33
34           The command removes only the paths that are known to Git.
35
36           File globbing matches across directory boundaries. Thus, given two
37           directories d and d2, there is a difference between using git rm
38           'd*' and git rm 'd/*', as the former will also remove all of
39           directory d2.
40
41           For more details, see the pathspec entry in gitglossary(7).
42
43       -f, --force
44           Override the up-to-date check.
45
46       -n, --dry-run
47           Don’t actually remove any file(s). Instead, just show if they exist
48           in the index and would otherwise be removed by the command.
49
50       -r
51           Allow recursive removal when a leading directory name is given.
52
53       --
54           This option can be used to separate command-line options from the
55           list of files, (useful when filenames might be mistaken for
56           command-line options).
57
58       --cached
59           Use this option to unstage and remove paths only from the index.
60           Working tree files, whether modified or not, will be left alone.
61
62       --ignore-unmatch
63           Exit with a zero status even if no files matched.
64
65       -q, --quiet
66           git rm normally outputs one line (in the form of an rm command) for
67           each file removed. This option suppresses that output.
68
69       --pathspec-from-file=<file>
70           Pathspec is passed in <file> instead of commandline args. If <file>
71           is exactly - then standard input is used. Pathspec elements are
72           separated by LF or CR/LF. Pathspec elements can be quoted as
73           explained for the configuration variable core.quotePath (see git-
74           config(1)). See also --pathspec-file-nul and global
75           --literal-pathspecs.
76
77       --pathspec-file-nul
78           Only meaningful with --pathspec-from-file. Pathspec elements are
79           separated with NUL character and all other characters are taken
80           literally (including newlines and quotes).
81

REMOVING FILES THAT HAVE DISAPPEARED FROM THE FILESYSTEM

83       There is no option for git rm to remove from the index only the paths
84       that have disappeared from the filesystem. However, depending on the
85       use case, there are several ways that can be done.
86
87   Using “git commit -a”
88       If you intend that your next commit should record all modifications of
89       tracked files in the working tree and record all removals of files that
90       have been removed from the working tree with rm (as opposed to git rm),
91       use git commit -a, as it will automatically notice and record all
92       removals. You can also have a similar effect without committing by
93       using git add -u.
94
95   Using “git add -A”
96       When accepting a new code drop for a vendor branch, you probably want
97       to record both the removal of paths and additions of new paths as well
98       as modifications of existing paths.
99
100       Typically you would first remove all tracked files from the working
101       tree using this command:
102
103           git ls-files -z | xargs -0 rm -f
104
105       and then untar the new code in the working tree. Alternately you could
106       rsync the changes into the working tree.
107
108       After that, the easiest way to record all removals, additions, and
109       modifications in the working tree is:
110
111           git add -A
112
113       See git-add(1).
114
115   Other ways
116       If all you really want to do is to remove from the index the files that
117       are no longer present in the working tree (perhaps because your working
118       tree is dirty so that you cannot use git commit -a), use the following
119       command:
120
121           git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached
122

SUBMODULES

124       Only submodules using a gitfile (which means they were cloned with a
125       Git version 1.7.8 or newer) will be removed from the work tree, as
126       their repository lives inside the .git directory of the superproject.
127       If a submodule (or one of those nested inside it) still uses a .git
128       directory, git rm will move the submodules git directory into the
129       superprojects git directory to protect the submodule’s history. If it
130       exists the submodule.<name> section in the gitmodules(5) file will also
131       be removed and that file will be staged (unless --cached or -n are
132       used).
133
134       A submodule is considered up to date when the HEAD is the same as
135       recorded in the index, no tracked files are modified and no untracked
136       files that aren’t ignored are present in the submodules work tree.
137       Ignored files are deemed expendable and won’t stop a submodule’s work
138       tree from being removed.
139
140       If you only want to remove the local checkout of a submodule from your
141       work tree without committing the removal, use git-submodule(1) deinit
142       instead. Also see gitsubmodules(7) for details on submodule removal.
143

EXAMPLES

145       git rm Documentation/\*.txt
146           Removes all *.txt files from the index that are under the
147           Documentation directory and any of its subdirectories.
148
149           Note that the asterisk * is quoted from the shell in this example;
150           this lets Git, and not the shell, expand the pathnames of files and
151           subdirectories under the Documentation/ directory.
152
153       git rm -f git-*.sh
154           Because this example lets the shell expand the asterisk (i.e. you
155           are listing the files explicitly), it does not remove
156           subdir/git-foo.sh.
157

BUGS

159       Each time a superproject update removes a populated submodule (e.g.
160       when switching between commits before and after the removal) a stale
161       submodule checkout will remain in the old location. Removing the old
162       directory is only safe when it uses a gitfile, as otherwise the history
163       of the submodule will be deleted too. This step will be obsolete when
164       recursive submodule update has been implemented.
165

SEE ALSO

167       git-add(1)
168

GIT

170       Part of the git(1) suite
171
172
173
174Git 2.33.1                        2021-10-12                         GIT-RM(1)
Impressum