简体   繁体   English

如何根据我的 .gitignore 文件在 Git 中取消跟踪文件?

[英]How can I untrack files in Git according to my .gitignore file?

I have tracked many many files early, but I don't want Git to track them any more from now on.我很早就跟踪了很多文件,但我不希望 Git 从现在开始跟踪它们。

Can I untrack those files according to a .gitignore file?我可以根据.gitignore文件取消跟踪这些文件吗?

There are too many files and they are separated in many different directories, so it is not practical to remove them one-by-one, instead, I hope they can be untracked according to patterns in a .gitignore file.文件太多,分散在很多不同的目录中,一个一个的删除是不切实际的,希望可以根据.gitignore文件中的模式不跟踪。

You need to remove the files from the index.您需要从索引中删除文件。

git rm -r --cached . 

and then add然后添加

git add .

Finally commit:最后提交:

git commit -a -m "Untrack ignored files!"

You can stop tracking already-tracked files by staging a deletion of the file in the index:您可以通过暂存索引中的文件删除来停止跟踪已跟踪的文件:

git rm --cached path/to/ignored/file

... and committing. ...并承诺。

With --cached this won't actually delete the file in your working copy.使用--cached这实际上不会删除工作副本中的文件。 (I'm always paranoid and copy them somewhere safe first, anyway.) (无论如何,我总是偏执并首先将它们复制到安全的地方。)

您可以使用以下命令获取所有被忽略文件的列表:

 git ls-files --others -i --exclude-standard

使用git rm删除您不再希望跟踪的文件,然后将这些文件添加到.gitignore ,它们将不再出现在您的未暂存更改列表中。

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

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