Git - 从存储库中删除所有特定类型的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38880436/
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
Git - Remove All of a Certain Type of File from the Repository
提问by Limited Atonement
How do I remove all of a certain type of file from the Repository? I'm using
如何从存储库中删除所有特定类型的文件?我正在使用
git filter-branch --index-filter 'git rm -rf --cached **/*.jar'
Either git is not expanding globs, or it isn't expanding **
in the way I'm expecting.
要么 git 没有扩展 globs,要么没有**
以我期望的方式扩展。
采纳答案by Limited Atonement
git filter-branch --index-filter 'git rm -rf --cached **/*.jar'
should work, but it's a bit silly because git globs (*
) match path separators. So, **/*.jar
is equivalent to *.jar
.
应该可以工作,但它有点傻,因为 git globs ( *
) 匹配路径分隔符。所以,**/*.jar
等价于*.jar
。
This also means that */a*.jar
matches dir1/abc/dir2/log4j.jar
. If you want to match something like **/a*.jar
(all jars whose name starts with a
in any directory), you should use find. Here's a command to remove any jars whose names start with a
or b
, and any jars in dir1/dir2
, and any .txt file in any directory:
这也意味着*/a*.jar
匹配dir1/abc/dir2/log4j.jar
. 如果您想匹配类似的内容**/a*.jar
(名称以a
任何目录开头的所有 jar ),您应该使用 find。这是删除名称以a
或开头的b
所有 jar dir1/dir2
、 中的所有 jar以及任何目录中的任何 .txt 文件的命令:
git filter-branch --index-filter 'git rm -rf --cached "*.txt" "dir1/dir2/*.jar" $(find -type f -name "a*.jar" -o -name "b*.jar")'
References: pathspec
section of git help glossary
.
参考资料:pathspec
部分git help glossary
。
回答by CodeWizard
You simply have to run this in order to remove all your jars from the index:
您只需运行它即可从索引中删除所有 jars:
git rm -r --cached **/*.jar
Run this command from your root directory of the project and it will clean up and will remove all your file only from the staging area.
从项目的根目录运行此命令,它将清理并仅从暂存区删除所有文件。
回答by JoshOfAllTrades
The easiest way I've found is to use the BFG Repo-Cleaner
我发现的最简单的方法是使用BFG Repo-Cleaner
The instructions on the project page are clear. The command you would use is something like:
项目页面上的说明很清楚。您将使用的命令类似于:
bfg --delete-files "*.jar" my-repo.git
BFG will clean the history of the repo of all files ending in the .jar extension. You can then inspect the result before pushing it back to the server.
BFG 将清除所有以 .jar 扩展名结尾的文件的 repo 历史记录。然后,您可以在将结果推送回服务器之前检查结果。
回答by VonC
With Git 2.24 (Q4 2019), git filter-branch
is deprecated.
对于 Git 2.24(2019 年第四季度),git filter-branch
已弃用。
The equivalent would be, using newren/git-filter-repo
, and its example section:
等效的将是, using newren/git-filter-repo
,及其示例部分:
cd repo
git filter-repo --path-glob '*.jar' --invert-paths
That will remove any jar file from the repository history.
这将从存储库历史记录中删除任何 jar 文件。
回答by Viktor K
Although it's not a git command, but for those who are interested in how to accomplish that on linux machine, you can use
虽然它不是一个 git 命令,但是对于那些对如何在 linux 机器上完成它感兴趣的人,你可以使用
git ls-files | grep "\.sh$" | { while IFS= read -r line; do git rm --cached "$line"; done }
Here we list all of files in git index and forward that output to grep command to filter only .sh
files and than for each file we perform git rm --cached <file>
.
在这里,我们列出 git index 中的所有文件,并将该输出转发给 grep 命令以仅过滤.sh
文件而不是我们执行的每个文件git rm --cached <file>
。