1GIT-RM(1) Git Manual GIT-RM(1)
2
3
4
6 git-rm - Remove files from the working tree and from the index
7
9 git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch]
10 [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]]
11 [--] [<pathspec>...]
12
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
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 --sparse
66 Allow updating index entries outside of the sparse-checkout cone.
67 Normally, git rm refuses to update index entries whose paths do not
68 fit within the sparse-checkout cone. See git-sparse-checkout(1) for
69 more.
70
71 -q, --quiet
72 git rm normally outputs one line (in the form of an rm command) for
73 each file removed. This option suppresses that output.
74
75 --pathspec-from-file=<file>
76 Pathspec is passed in <file> instead of commandline args. If <file>
77 is exactly - then standard input is used. Pathspec elements are
78 separated by LF or CR/LF. Pathspec elements can be quoted as
79 explained for the configuration variable core.quotePath (see git-
80 config(1)). See also --pathspec-file-nul and global
81 --literal-pathspecs.
82
83 --pathspec-file-nul
84 Only meaningful with --pathspec-from-file. Pathspec elements are
85 separated with NUL character and all other characters are taken
86 literally (including newlines and quotes).
87
89 There is no option for git rm to remove from the index only the paths
90 that have disappeared from the filesystem. However, depending on the
91 use case, there are several ways that can be done.
92
93 Using “git commit -a”
94 If you intend that your next commit should record all modifications of
95 tracked files in the working tree and record all removals of files that
96 have been removed from the working tree with rm (as opposed to git rm),
97 use git commit -a, as it will automatically notice and record all
98 removals. You can also have a similar effect without committing by
99 using git add -u.
100
101 Using “git add -A”
102 When accepting a new code drop for a vendor branch, you probably want
103 to record both the removal of paths and additions of new paths as well
104 as modifications of existing paths.
105
106 Typically you would first remove all tracked files from the working
107 tree using this command:
108
109 git ls-files -z | xargs -0 rm -f
110
111 and then untar the new code in the working tree. Alternately you could
112 rsync the changes into the working tree.
113
114 After that, the easiest way to record all removals, additions, and
115 modifications in the working tree is:
116
117 git add -A
118
119 See git-add(1).
120
121 Other ways
122 If all you really want to do is to remove from the index the files that
123 are no longer present in the working tree (perhaps because your working
124 tree is dirty so that you cannot use git commit -a), use the following
125 command:
126
127 git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached
128
130 Only submodules using a gitfile (which means they were cloned with a
131 Git version 1.7.8 or newer) will be removed from the work tree, as
132 their repository lives inside the .git directory of the superproject.
133 If a submodule (or one of those nested inside it) still uses a .git
134 directory, git rm will move the submodules git directory into the
135 superprojects git directory to protect the submodule’s history. If it
136 exists the submodule.<name> section in the gitmodules(5) file will also
137 be removed and that file will be staged (unless --cached or -n are
138 used).
139
140 A submodule is considered up to date when the HEAD is the same as
141 recorded in the index, no tracked files are modified and no untracked
142 files that aren’t ignored are present in the submodules work tree.
143 Ignored files are deemed expendable and won’t stop a submodule’s work
144 tree from being removed.
145
146 If you only want to remove the local checkout of a submodule from your
147 work tree without committing the removal, use git-submodule(1) deinit
148 instead. Also see gitsubmodules(7) for details on submodule removal.
149
151 git rm Documentation/\*.txt
152 Removes all *.txt files from the index that are under the
153 Documentation directory and any of its subdirectories.
154
155 Note that the asterisk * is quoted from the shell in this example;
156 this lets Git, and not the shell, expand the pathnames of files and
157 subdirectories under the Documentation/ directory.
158
159 git rm -f git-*.sh
160 Because this example lets the shell expand the asterisk (i.e. you
161 are listing the files explicitly), it does not remove
162 subdir/git-foo.sh.
163
165 Each time a superproject update removes a populated submodule (e.g.
166 when switching between commits before and after the removal) a stale
167 submodule checkout will remain in the old location. Removing the old
168 directory is only safe when it uses a gitfile, as otherwise the history
169 of the submodule will be deleted too. This step will be obsolete when
170 recursive submodule update has been implemented.
171
173 git-add(1)
174
176 Part of the git(1) suite
177
178
179
180Git 2.39.1 2023-01-13 GIT-RM(1)