如何删除从.git中删除的文件?

5
当我使用mvrm或其他工具删除(或更改名称)文件时,当我运行git status时,该文件将显示为已删除:
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   deleted:    ../src/main/..../myFile.java

在创建提交之前,对于每个文件执行git rm <file>很麻烦,特别是终端中没有为不存在的文件提供自动补全功能。有没有更简单的方法从Git跟踪的文件集中删除已删除的文件?谢谢

我通常只是将其复制粘贴到文本编辑器中,然后查找和替换以一次性批处理它。 - Barry Carlyon
5个回答

6
我相信git add -u可以实现你的需求,根据文档描述:
-u
--update
  Only match <filepattern> against already tracked files in the index 
  rather than the working tree. That means that it will never stage new
  files, but that it will stage modified new contents of tracked files
  and that it will remove files from the index if the corresponding file
  in the working tree have been removed.

Reference: http://git-scm.com/docs/git-add


3
当您删除一些文件时,请记得在提交中添加“a”参数:
$ git commit -am 'Message'

"

"a"将自动从代码库中删除文件

"

2

是的,git add -u可以解决问题(它会将所有修改/删除更新到您的索引中)。如果您已经使用新文件名添加了文件,则甚至可以在git status中看到重命名。


1

这不完全是您要求的,但您可以尝试使用git commit -a。根据文档:

Tell the command to automatically stage files that have been modified
and deleted, but new files you have not told git about are not affected.

1

我的回答并没有真正解决这个具体的问题,但是...

请注意,与其他某些版本控制系统不同,在Git中,git rm命令会从索引和工作树中删除文件,除非使用--cached命令行选项告诉它不要这样做。因此,建议您首先使用git rm删除已跟踪的文件:这样您就可以让文件名自动完成,并且无需处理索引状态与工作树的同步。


有时我会在文本编辑器中重命名文件,虽然文件可能深藏于目录树中,但输入git rm和完整路径可能需要更长时间。 Translated text: 真实的情况是,有时候我会在文本编辑器内重命名文件。由于该文件可能处于深层次的目录结构中,所以键入“git rm”和完整文件路径可能需要更多时间。 - axel22
在你的情况下是 git mv,但是除此之外我同意。 - kostix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接