简体   繁体   English

git使用git rm命令删除不在.gitignore中的文件

[英]git removing files that are not in .gitignore with git rm command

I have a structure like so 我有这样的结构

SDI
|-.gitignore
\-webapp

In the SDI/webapp/app directory, when I run git rm -r --cached . 在SDI / webapp / app目录中,当我运行git rm -r --cached . , I see files like this removed ,我看到这样的文件已删除

rm 'webapp/app/views/MyStuff/myGroups.html'
rm 'webapp/app/views/MyStuff/userAdd.html'
rm 'webapp/app/views/MyStuff/userEdit.html'
rm 'webapp/app/views/auth/Secure/login.html'
rm 'webapp/app/views/errors/404.html'
rm 'webapp/app/views/errors/500.html'
rm 'webapp/app/views/main.html'
rm 'webapp/app/views/tags/auth/check.tag'
rm 'webapp/app/views/tags/generic.html'
rm 'webapp/app/views/tags/text.html'

My .gitignore contents are as follows 我的.gitignore contents如下

eclipse/
tmp/
bin/
tools/play-1.2.5/framework/pym/

Why is it removing ALL my files? 为什么要删除我的所有文件? I do have git config file.mode false as the only thing unique on this repository compared to other ones I have done in the past. 与过去我做过的其他事情相比,我确实将git config file.mode false作为该存储库中唯一的东西。

  • In does not actually remove the files, it only remove the changes from the index. 在实际上不删除文件中,它仅从索引中删除更改。 Your working space remains untouched. 您的工作空间保持不变。
  • It does this, because you tell it to do so. 这样做是因为您告诉它这样做。

http://www.kernel.org/pub/software/scm/git/docs/git-rm.html http://www.kernel.org/pub/software/scm/git/docs/git-rm.html

Remove files from the index , or from the working tree and the index. 从索引或工作树和索引中删除文件 git rm will not remove a file from just your working directory. git rm不会仅从您的工作目录中删除文件。 (There is no option to remove a file only from the working tree and yet keep it in the index; use /bin/rm if you want to do that.) The files being removed have to be identical to the tip of the branch, and no updates to their contents can be staged in the index, though that default behavior can be overridden with the -f option. (没有选择仅从工作树中删除文件,而是将其保留在索引中的选项;如果要这样做,请使用/ bin / rm。)要删除的文件必须与分支的尖端相同,尽管可以使用-f选项覆盖默认行为,但无法在索引中暂存对其内容的更新。 When --cached is given, the staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index. 给定--cached时,暂存的内容必须与分支的尖端或磁盘上的文件匹配,从而允许仅从索引中删除文件。

and

-r Allow recursive removal when a leading directory name is given. -r在给出前导目录名称时,允许递归删除。

and

--cached Use this option to unstage and remove paths only from the index. --cached使用此选项可取消登台并仅从索引中删除路径。 Working tree files, whether modified or not, will be left alone. 无论是否已修改,工作树文件都将被保留。

I don't know, what you want to achieve (because you didn't tell), but the command does exactly, what it should and in the way it should. 我不知道您想要实现什么(因为您没有告诉),但是该命令可以准确地执行它应该执行的操作以及执行的方式。

.gitignore "just" tells git to not add changes to the index, when calling git add recursively (eg git add . , or git commit -a ), or show them in git status (and probably other tools, but thats the most common use cases). .gitignore “ just”告诉git,当递归调用git add (例如git add .git commit -a ),或者以git status显示它们(可能还有其他工具,但不常见),不要向索引添加更改用例)。 Because changes to this files shouldn't be staged anyway, git rm doesn't affect them. 因为无论如何都不应该对此文件进行更改,所以git rm不会影响它们。

It is not removing your files, it is unstaging them. 它不是在删除文件,而是在取消暂存文件。 Your ignored files are ignored. 您忽略的文件将被忽略。 So don't worry about that. 所以不用担心。 If you want to remove your ignored files, you can 如果要删除忽略的文件,可以

git clean -xdf

UPDATE: 更新:

A safer way to do this (as mentioned in the comments) in case you do have a file you care about is to 如果您确实有需要的文件,更安全的方法(如评论中所述)是

git stash -u && git clean -xdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM