git 如何删除 .gitignore 中列出但仍在存储库中的文件?

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

How to remove files that are listed in the .gitignore but still on the repository?

gitignoregitignore

提问by Intrepidd

I have some files in my repository that should be ignored, i added them to the .gitignore but, of course, they are not removed from my repository.

我的存储库中有一些文件应该被忽略,我将它们添加到 .gitignore 但是,当然,它们没有从我的存储库中删除。

So my question is, is there a magic command or script using filter-branch that can rewrite my history and remove all these files easily? Or simply a command that will create a commit that will remove them ?

所以我的问题是,是否有使用 filter-branch 的魔法命令或脚本可以重写我的历史记录并轻松删除所有这些文件?或者只是一个将创建一个将删除它们的提交的命令?

回答by Samy Dindane

You can remove them from the repository manually:

您可以手动从存储库中删除它们:

git rm --cached file1 file2 dir/file3

Or, if you have a lot of files:

或者,如果您有很多文件:

git rm --cached `git ls-files -i --exclude-from=.gitignore`

But this doesn't seem to work in Git Bash on Windows. It produces an error message. The following works better:

但这在 Windows 上的 Git Bash 中似乎不起作用。它会产生一条错误消息。以下效果更好:

git ls-files -i --exclude-from=.gitignore | xargs git rm --cached  

Regarding rewriting the whole history without these files, I highly doubt there's an automaticway to do it.
And we all know that rewriting the history is bad, don't we? :)

关于在没有这些文件的情况下重写整个历史记录,我非常怀疑是否有一种自动的方法来做到这一点。
我们都知道重写历史是不好的,不是吗?:)

回答by gtatr

An easier way that works on any OS is to do

适用于任何操作系统的更简单方法是

git rm -r --cached .
git add .
git commit -m "Removing all files in .gitignore"

You basically readd all files, except the ones in the .gitignore

您基本上阅读了所有文件,除了 .gitignore 中的文件

回答by weiz

As the files in .gitignoreare not being tracked, you can use the git cleancommand to recursively remove files that are not under version control.

由于.gitignore中的文件没有被跟踪,您可以使用git clean命令递归删除不受版本控制的文件。

Use git clean -xdnto perform a dry run and see what will be removed.
Then use git clean -xdfto execute it.

使用git clean -xdn进行试运行,看看将被删除。
然后使用git clean -xdf它来执行它。

Basically, git clean -hor man git-clean(in unix) will give you help.

基本上,git clean -h或者man git-clean(在 unix 中)会给你帮助。

Be aware that this command will also remove new filesthat are not in the staging area.

请注意,此命令还将删除不在暂存区中的新文件

Hope it helps.

希望能帮助到你。

回答by Scott Crossen

I did a very straightforward solution by manipulating the output of the .gitignore statement with sed:

我通过使用 sed 操作 .gitignore 语句的输出做了一个非常简单的解决方案:

cat .gitignore | sed '/^#.*/ d' | sed '/^\s*$/ d' | sed 's/^/git rm -r /' | bash

cat .gitignore | sed '/^#.*/ d' | sed '/^\s*$/ d' | sed 's/^/git rm -r /' | bash

Explanation:

解释:

  1. print the .gitignore file
  2. remove all comments from the print
  3. delete all empty lines
  4. add 'git rm -r ' to the start of the line
  5. execute every line.
  1. 打印 .gitignore 文件
  2. 从印刷品中删除所有评论
  3. 删除所有空行
  4. 将“git rm -r”添加到行首
  5. 执行每一行。

回答by Saloua SAHNOUNE

In linux you can use this commande :

在 linux 中你可以使用这个命令:

for exemple i want to delete "*.py~" so my command should be ==>

例如,我想删除“*.py~”,所以我的命令应该是 ==>

find . -name "*.py~" -exec rm -f {} \;

find . -name "*.py~" -exec rm -f {} \;

回答by pensz

The git will ignore the files matched .gitignore pattern after you add it to .gitignore.

将匹配 .gitignore 模式的文件添加到 .gitignore 后,git 将忽略它。

But the files already existed in repository will be still in.

但是存储库中已经存在的文件将仍然存在。

use git rm files_ignored; git commit -m 'rm no use files'to delete ignored files.

用于git rm files_ignored; git commit -m 'rm no use files'删除被忽略的文件。