当文件已经从 fs 中删除时,从 git index 中删除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6090732/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:30:45  来源:igfitidea点击:

Delete files from git index when they are already deleted from fs

gitfilesystems

提问by Bogdan Gusiev

I have a bunch of files deleted from the fs and listed as deleted in git status.

我从 fs 中删除了一堆文件,并在git status.

How can I stage this changes faster then running git rmfor each file?

如何在git rm为每个文件运行之前更快地进行此更改?

回答by Mark Longair

You can do this with:

你可以这样做:

git ls-files --deleted -z | xargs -0 git rm

Whenever this question is asked, people suggest git add -u, but the problem with that answer is that it also stages other modifications in your working copy, not just deletions. That might be OK in many situations, but if you want to just stage the deletion of files that have been deleted from the working copy, the suggestion I've made is more precise.

每当提出这个问题时,人们都会建议git add -u,但该答案的问题在于它还会在您的工作副本中进行其他修改,而不仅仅是删除。在许多情况下这可能没问题,但是如果您只想暂存已从工作副本中删除的文件的删除,我提出的建议更准确。

There's actually a section of the git rmdocumentationthat discusses how to do what you want - I believe that the command suggested in the "Other ways" section is equivalent to what I've suggested here.

实际上有一部分git rm文档讨论了如何做你想做的事 - 我相信“其他方式”部分中建议的命令与我在这里建议的相同。

回答by KingCrunch

Use the -u-flag: man git-add

使用-u-flag:man git-add

git add -u .

回答by GameScripting

You can use git rm --cached "path/to/file"to stage a single deleted file.

您可以使用git rm --cached "path/to/file"暂存单个已删除的文件。

Use git rm -r --cached -- "path/to/directory"to stage a complete deleted directory.

使用git rm -r --cached -- "path/to/directory"上演了一出完整删除的目录。

回答by stack72

git add -A

git add -A

will do the job for you

会为你做这项工作

回答by Ionu? G. Stan

git commit -awould stage deleted files (as well as modified) and prompt you for your commit message. I usually execute git commit -av(verbose) to also see diffs of modified files.

git commit -a将暂存已删除的文件(以及已修改的)并提示您输入提交消息。我通常执行git commit -av(详细)以查看修改文件的差异。

From the manual page:

手册页

by using the -a switch with the commit command to automatically "add" changes from all known files (i.e. all files that are already listed in the index) and to automatically "rm" files in the index that have been removed from the working tree, and then perform the actual commit;

通过在 commit 命令中使用 -a 开关自动“添加”所有已知文件(即索引中已列出的所有文件)的更改,并自动“rm”索引中已从工作树中删除的文件,然后执行实际提交;