Git - Remove All of a Certain Type of File from the Repository

╄→尐↘猪︶ㄣ 提交于 2019-12-04 02:19:17

You simply have to run this in order to remove all your jars from the index:

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.

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.

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:

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.

The easiest way I've found is to use the 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.

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 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>.

With Git 2.24 (Q4 2019), git filter-branch is deprecated.

The equivalent would be, using newren/git-filter-repo, and its example section:

cd repo
git filter-repo --path-glob '*.jar' --invert-paths

That will remove any jar file from the repository history.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!