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

时间:2012-07-14 23:53:59

标签: git

当我使用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跟踪的文件集中删除已删除的文件?

由于

5 个答案:

答案 0 :(得分:4)

我相信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.

参考:http://git-scm.com/docs/git-add

答案 1 :(得分:3)

删除某些文件时,请记得在commit中添加“a”参数:

$ git commit -am 'Message'

“a”将自动从存储库中删除文件

答案 2 :(得分:2)

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

答案 3 :(得分: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.

答案 4 :(得分:1)

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

请注意,与Git中的某些其他VCS不同,git rm命令会从索引工作树中删除文件,除非另有说明(使用--cached命令-line选项)。因此,建议您首先使用git rm删除跟踪的文件:您将完成文件名的完成工作,无需将索引的状态与工作树同步。

相关问题