git 暂存已删除的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12373733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Staging Deleted files
提问by Andrew Tomazos
Say I have a file in my git repository called foo
.
假设我的 git 存储库中有一个名为foo
.
Suppose it has been deleted with rm
(not git rm
). Then git status will show:
假设它已被rm
(not git rm
)删除。然后 git status 将显示:
Changes not staged for commit:
deleted: foo
How do I stage this individual file deletion?
如何暂存此单个文件删除?
If I try:
如果我尝试:
git add foo
It says:
它说:
'foo' did not match any files.
采纳答案by peflorencio
Since Git 2.0.0, git add
will also stage file deletions.
自 Git 2.0.0 起,git add
还将暂存文件删除。
< pathspec >…
Files to add content from. Fileglobs (e.g. *.c) can be given to add all > matching files. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to update the index to match the current state of the directory as a whole (e.g. specifying dir will record not just a file dir/file1 modified in the working tree, a file dir/file2 added to the working tree, but also a file dir/file3 removed from the working tree. Note that older versions of Git used to ignore removed files; use --no-all option if you want to add modified or new files but ignore removed ones.
<路径规范>…
要从中添加内容的文件。可以给出 Fileglobs(例如 *.c)来添加所有 > 匹配的文件。还可以给出一个前导目录名(例如 dir 来添加 dir/file1 和 dir/file2)来更新索引以匹配整个目录的当前状态(例如,指定 dir 将记录而不仅仅是一个文件 dir/file1 修改在工作树中,一个文件 dir/file2 添加到工作树中,还有一个文件 dir/file3 从工作树中删除。请注意,旧版本的 Git 过去常常忽略删除的文件;如果您使用 --no-all 选项想要添加修改过的或新的文件但忽略删除的文件。
回答by Wooble
Use git rm foo
to stage the file for deletion. (This will also delete the file from the file system, if it hadn't been previously deleted. It can, of course, be restored from git, since it was previously checked in.)
使用git rm foo
阶段性删除该文件。(这也会从文件系统中删除文件,如果它之前没有被删除。当然,它可以从 git 恢复,因为它之前被签入。)
To stage the file for deletion without deleting it from the file system, use git rm --cached foo
要暂存要删除的文件而不将其从文件系统中删除,请使用 git rm --cached foo
回答by Sailesh
Even though it's correct to use git rm [FILE]
, alternatively, you could do git add -u
.
即使使用是正确的git rm [FILE]
,或者,您也可以使用git add -u
.
According to the git-add
documentation:
根据git-add
文档:
-u --update
Update the index just where it already has an entry matching [FILE]. This removes as well as modifies index entries to match the working tree, but adds no new files.
If no [FILE] is given when -u option is used, all tracked files in the entire working tree are updated (old versions of Git used to limit the update to the current directory and its subdirectories).
-u --更新
更新索引已经有匹配 [FILE] 的条目的位置。这将删除和修改索引条目以匹配工作树,但不添加新文件。
如果使用 -u 选项时未给出 [FILE],则更新整个工作树中的所有跟踪文件(旧版本的 Git 用于将更新限制为当前目录及其子目录)。
Upon which the index will be refreshed and files will be properly staged.
将刷新索引并正确暂存文件。
回答by Ian Mackinnon
To stage all manually deleted files you can use:
要暂存所有手动删除的文件,您可以使用:
git rm $(git ls-files --deleted)
To add an alias to this command as git rm-deleted
, run:
要将别名添加到此命令git rm-deleted
,请运行:
git config --global alias.rm-deleted '!git rm $(git ls-files --deleted)'
回答by chet corey
to Add all ready deleted files
添加所有已删除的文件
git status -s | grep -E '^ D' | cut -d ' ' -f3 | xargs git add --all
thank check to make sure
感谢检查以确保
git status
you should be good to go
你应该很高兴去
回答by gnana jeyam95
You can use this command
你可以使用这个命令
git add `git ls-files --deleted`
回答by GameScripting
You can use
您可以使用
git rm -r --cached -- "path/to/directory"
git rm -r --cached -- "path/to/directory"
to stage a deleted directory.
暂存已删除的目录。
回答by orion elenzil
for those using git 2.x+ in powershell:
对于那些在 powershell 中使用 git 2.x+ 的人:
foreach ($filePath in (git ls-files --deleted)) { git add "$filePath" }