Setup and Config
Getting and Creating Projects
Basic Snapshotting
Branching and Merging
Sharing and Updating Projects
Inspection and Comparison
Patching
Debugging
External Systems
Server Admin
Guides
- gitattributes
- Command-line interface conventions
- Everyday Git
- Frequently Asked Questions (FAQ)
- Glossary
- Hooks
- gitignore
- gitmodules
- Revisions
- Submodules
- Tutorial
- Workflows
- All guides...
Administration
Plumbing Commands
-
2.50.0
2025-06-16
- 2.43.1 → 2.49.0 no changes
-
2.43.0
2023-11-20
- 2.34.1 → 2.42.4 no changes
-
2.34.0
2021-11-15
- 2.32.1 → 2.33.8 no changes
-
2.32.0
2021-06-06
- 2.26.1 → 2.31.8 no changes
-
2.26.0
2020-03-22
- 2.16.6 → 2.25.5 no changes
-
2.15.4
2019-12-06
-
2.14.6
2019-12-06
-
2.13.7
2018-05-22
- 2.1.4 → 2.12.5 no changes
-
2.0.5
2014-12-17
概述
git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--pathspec-from-file=<file> [--pathspec-file-nul]] [--] [<pathspec>…]
描述
从索引中,或从工作树和索引中删除与pathspec匹配的文件。git rm`不会只从你的工作目录中删除一个文件。(没有选项可以只从工作树中删除一个文件,但在索引中保留它;如果你想这样做,就用
/bin/rm`)。被删除的文件必须与分支的顶端相同,而且它们的内容不能在索引中进行更新,尽管可以用`-f`选项覆盖这一默认行为。 当给定`--cached`时,缓存的内容必须与分支的顶端或磁盘上的文件相匹配,允许文件只从索引中被删除。当使用稀疏检查时(见git-sparse-checkout[1]),`git rm`将只删除稀疏检查模式内的路径。
选项
-
<路径规范>...
-
要删除的文件。 可以给一个领先的目录名(例如,
dir`删除`dir/file1`和`dir/file2
)来删除该目录下的所有文件,并递归所有子目录,但这需要明确给出`-r`选项。该命令只删除Git已知的路径。
文件球化可以跨越目录边界进行匹配。 因此,给定两个目录`d`和`d2`,使用`git rm d*`和`git rm d/*`是有区别的,因为前者会同时删除`d2`目录的所有内容。
For more details, see the <pathspec> entry in gitglossary[7].
-
-f
-
--force
-
覆盖最新的检查。
-
-n
-
--dry-run
-
实际上不要删除任何文件。 相反,只是显示它们是否存在于索引中,否则会被命令删除。
-
-r
-
当给出一个领先的目录名称时,允许递归删除。
-
--
-
此选项可用于分离命令行选项和文件列表(当文件名可能被误认为命令行选项时非常有用)。
-
--cached
-
使用这个选项,只从索引中解开阶段并删除路径。 工作树文件,无论是否被修改过,都将被单独留下。
-
--ignore-unmatch
-
即使没有文件匹配,也以零状态退出。
-
--sparse
-
允许更新稀疏结帐锥之外的索引条目。通常,
git rm
拒绝更新其路径不适合稀疏结帐锥的索引条目。有关更多信息,请参见 git-sparse-checkout[1]。 -
-q
-
--quiet
-
git rm
通常会为每个删除的文件输出一行(以rm
命令的形式)。这个选项会抑制输出。 -
--pathspec-from-file=<file>
-
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
(see git-config[1]). See also--pathspec-file-nul
and global--literal-pathspecs
. -
--pathspec-file-nul
-
只有在使用
--pathspec-from-file
选项时才有意义。指定路径元素用 NUL 字符分隔,所有其他字符都按字面意思(包括换行符和引号)表示。
删除从文件系统中消失的文件
`git rm`没有选项可以从索引中只删除已经从文件系统中消失的路径。然而,根据不同的使用情况,有几种方法可以做到这一点。
使用`‘git commit -a’'
如果你想让你的下一次提交记录工作树中所有被跟踪的文件的修改,并记录所有用`rm`从工作树中删除的文件(而不是`git rm`),请使用`git commit -a`,它将自动注意并记录所有的删除。 你也可以通过使用`git add -u`在不提交的情况下获得类似的效果。
使用`‘git add -A’'
当接受一个供应商分支的新代码投放时,你可能想同时记录删除路径和增加新路径以及修改现有路径的内容。
通常情况下,你会先用这个命令从工作目录树上删除所有被追踪的文件:
git ls-files -z | xargs -0 rm -f
然后解开工作树上的新代码。另外,你也可以通过 "rsync "把修改的内容放到工作树上。
之后,记录工作目录树中所有的删除、添加和修改的最简单方法是:
git add -A
参见 git-add[1]。
子模块
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, as 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 submodules git directory into the superprojects 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.
如果你只想从你的工作树中移除一个子模块的本地签出而不提交移除,使用git-submodule[1] `deinit`代替。也请看 gitsubmodules[7] 了解关于子模块移除的细节。
漏洞
每次超级项目更新删除一个已填充的子模块(例如,在删除前后的提交之间切换时),一个陈旧的子模块检出将保留在旧的位置。只有在使用gitfile时,删除旧目录才是安全的,否则子模块的历史也会被删除。当递归子模块更新被实施后,这个步骤将被淘汰。
GIT
属于 git[1] 文档