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] [--quiet] [--] <file>...
10
11
13 Remove files from the index, or from the working tree and the index.
14 git rm will not remove a file from just your working directory. (There
15 is no option to remove a file only from the working tree and yet keep
16 it in the index; use /bin/rm if you want to do that.) The files being
17 removed have to be identical to the tip of the branch, and no updates
18 to their contents can be staged in the index, though that default
19 behavior can be overridden with the -f option. When --cached is given,
20 the staged content has to match either the tip of the branch or the
21 file on disk, allowing the file to be removed from just the index.
22
24 <file>...
25 Files to remove. Fileglobs (e.g. *.c) can be given to remove all
26 matching files. If you want Git to expand file glob characters, you
27 may need to shell-escape them. A leading directory name (e.g. dir
28 to remove dir/file1 and dir/file2) can be given to remove all files
29 in the directory, and recursively all sub-directories, but this
30 requires the -r option to be explicitly given.
31
32 -f, --force
33 Override the up-to-date check.
34
35 -n, --dry-run
36 Don’t actually remove any file(s). Instead, just show if they exist
37 in the index and would otherwise be removed by the command.
38
39 -r
40 Allow recursive removal when a leading directory name is given.
41
42 --
43 This option can be used to separate command-line options from the
44 list of files, (useful when filenames might be mistaken for
45 command-line options).
46
47 --cached
48 Use this option to unstage and remove paths only from the index.
49 Working tree files, whether modified or not, will be left alone.
50
51 --ignore-unmatch
52 Exit with a zero status even if no files matched.
53
54 -q, --quiet
55 git rm normally outputs one line (in the form of an rm command) for
56 each file removed. This option suppresses that output.
57
59 The <file> list given to the command can be exact pathnames, file glob
60 patterns, or leading directory names. The command removes only the
61 paths that are known to Git. Giving the name of a file that you have
62 not told Git about does not remove that file.
63
64 File globbing matches across directory boundaries. Thus, given two
65 directories d and d2, there is a difference between using git rm 'd*'
66 and git rm 'd/*', as the former will also remove all of directory d2.
67
69 There is no option for git rm to remove from the index only the paths
70 that have disappeared from the filesystem. However, depending on the
71 use case, there are several ways that can be done.
72
73 Using “git commit -a”
74 If you intend that your next commit should record all modifications of
75 tracked files in the working tree and record all removals of files that
76 have been removed from the working tree with rm (as opposed to git rm),
77 use git commit -a, as it will automatically notice and record all
78 removals. You can also have a similar effect without committing by
79 using git add -u.
80
81 Using “git add -A”
82 When accepting a new code drop for a vendor branch, you probably want
83 to record both the removal of paths and additions of new paths as well
84 as modifications of existing paths.
85
86 Typically you would first remove all tracked files from the working
87 tree using this command:
88
89 git ls-files -z | xargs -0 rm -f
90
91
92 and then untar the new code in the working tree. Alternately you could
93 rsync the changes into the working tree.
94
95 After that, the easiest way to record all removals, additions, and
96 modifications in the working tree is:
97
98 git add -A
99
100
101 See git-add(1).
102
103 Other ways
104 If all you really want to do is to remove from the index the files that
105 are no longer present in the working tree (perhaps because your working
106 tree is dirty so that you cannot use git commit -a), use the following
107 command:
108
109 git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached
110
111
113 Only submodules using a gitfile (which means they were cloned with a
114 Git version 1.7.8 or newer) will be removed from the work tree, as
115 their repository lives inside the .git directory of the superproject.
116 If a submodule (or one of those nested inside it) still uses a .git
117 directory, git rm will move the submodules git directory into the
118 superprojects git directory to protect the submodule’s history. If it
119 exists the submodule.<name> section in the gitmodules(5) file will also
120 be removed and that file will be staged (unless --cached or -n are
121 used).
122
123 A submodule is considered up to date when the HEAD is the same as
124 recorded in the index, no tracked files are modified and no untracked
125 files that aren’t ignored are present in the submodules work tree.
126 Ignored files are deemed expendable and won’t stop a submodule’s work
127 tree from being removed.
128
129 If you only want to remove the local checkout of a submodule from your
130 work tree without committing the removal, use git-submodule(1) deinit
131 instead. Also see gitsubmodules(7) for details on submodule removal.
132
134 git rm Documentation/\*.txt
135 Removes all *.txt files from the index that are under the
136 Documentation directory and any of its subdirectories.
137
138 Note that the asterisk * is quoted from the shell in this example;
139 this lets Git, and not the shell, expand the pathnames of files and
140 subdirectories under the Documentation/ directory.
141
142 git rm -f git-*.sh
143 Because this example lets the shell expand the asterisk (i.e. you
144 are listing the files explicitly), it does not remove
145 subdir/git-foo.sh.
146
148 Each time a superproject update removes a populated submodule (e.g.
149 when switching between commits before and after the removal) a stale
150 submodule checkout will remain in the old location. Removing the old
151 directory is only safe when it uses a gitfile, as otherwise the history
152 of the submodule will be deleted too. This step will be obsolete when
153 recursive submodule update has been implemented.
154
156 git-add(1)
157
159 Part of the git(1) suite
160
161
162
163Git 2.18.1 05/14/2019 GIT-RM(1)