官網(wǎng)
https://rtyley.github.io/bfg-repo-cleaner/
最新版需要確保本地的java為Jdk8+
背景(我為什么要使用這個工具)
最近開發(fā)中有一個需求票编,就是讓我從一個倉庫clone代碼下來拷姿,然后提交到另一個倉庫凡桥,需求確實不算難仔涩,理想情況:從一個地址clone下來掖疮,然后push到另一個地址盼玄,結(jié)束≈雀裕現(xiàn)實:↗↘↗↘↘↗
在往另一個倉庫push的時候爆阶,發(fā)現(xiàn)另一個倉庫的服務(wù)器不支持這么大的文件(500多M)的庫上傳。
所以我在詢問過負(fù)責(zé)人之后溶褪,告訴我可以把附件刪除币旧,只保留代碼即可,但是手動刪除之后猿妈,倉庫還是那么大吹菱,因為在倉庫的歷史中還是有這些附件的記錄的,所以就需要把這些附件從歷史中進(jìn)行刪除彭则。
工具的使用
1鳍刷、上官網(wǎng)下載BFG Repo-Cleaner的客戶端(jar包)
2、使用--mirror克隆一個新的倉庫
$ git clone --mirror git://example.com/some-big-repo.git
--mirror 后為倉庫地址俯抖,可以用https或者ssh
在進(jìn)行操作之前输瓜,最好備份下這個庫,操作出了問題用這個備份庫即可芬萍,不用再重新下載尤揣。
下載好的庫的格式:
ps:在啟動客戶端時,報錯:不是一個有效的庫(is not a invalid repository)柬祠,就按上面的方式重新clone北戏。
3、刪除不需要的文件
將下載好的jar放到下載好的git庫的同級目錄
刪除大于100M的文件
$ java -jar bfg.jar --strip-blobs-bigger-than 100M some-big-repo.git
BFG將更新您的提交以及所有分支和標(biāo)記瓶盛,此時還沒有物理刪除最欠。使用gc去除git認(rèn)為多余的數(shù)據(jù)(上面調(diào)用命令刪除的文件)示罗。
$ cd some-big-repo.git
$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
調(diào)用上面的命令后惩猫,文件將徹底刪除(everywhere)。
4蚜点、將代碼提交到遠(yuǎn)程倉庫
確保文件沒有任何問題之后轧房,將代碼提交到遠(yuǎn)程倉庫,這將影響遠(yuǎn)程服務(wù)器上所有的refs绍绘。
$ git push
如果有必要奶镶,讓其他人刪除原來的倉庫,因為原來的倉庫中還是有臟數(shù)據(jù)陪拘,有可能會再次提交到遠(yuǎn)程倉庫厂镇。
5、其他命令
1.刪除所有的名為'id_dsa'或'id_rsa'的文件
$ java -jar bfg.jar --delete-files id_{dsa,rsa} my-repo.git
2.刪除所有大于50M的文件
$ java -jar bfg.jar --strip-blobs-bigger-than 50M my-repo.git
3.刪除文件夾下所有的文件
$ java -jar bfg.jar --delete-folders doc my-repo.git
6左刽、注意
刪除文件后別忘了gc命令捺信,工具不會清除最近一次提交的文件內(nèi)容,如果需要刪除欠痴,使用
--no-blob-protection迄靠,官方不推薦秒咨。在刪除前最好確保最新的提交為干凈的。
7掌挚、詳細(xì)命令
bfg 1.13.0
Usage: bfg [options] [<repo>]
-b, --strip-blobs-bigger-than <size>
strip blobs bigger than X (eg '128K', '1M', etc)
-B, --strip-biggest-blobs NUM
strip the top NUM biggest blobs
-bi, --strip-blobs-with-ids <blob-ids-file>
strip blobs with the specified Git object ids
-D, --delete-files <glob>
delete files with the specified names (eg '*.class', '*.{txt,log}' - matches on file name, not path within repo)
--delete-folders <glob> delete folders with the specified names (eg '.svn', '*-tmp' - matches on folder name, not path within repo)
--convert-to-git-lfs <value>
extract files with the specified names (eg '*.zip' or '*.mp4') into Git LFS
-rt, --replace-text <expressions-file>
filter content of files, replacing matched text. Match expressions should be listed in the file, one expression per line - by default, each expression is treated as a literal, but 'regex:' & 'glob:' prefixes are supported, with '==>' to specify a replacement string other than the default of '***REMOVED***'.
-fi, --filter-content-including <glob>
do file-content filtering on files that match the specified expression (eg '*.{txt,properties}')
-fe, --filter-content-excluding <glob>
don't do file-content filtering on files that match the specified expression (eg '*.{xml,pdf}')
-fs, --filter-content-size-threshold <size>
only do file-content filtering on files smaller than <size> (default is 1048576 bytes)
-p, --protect-blobs-from <refs>
protect blobs that appear in the most recent versions of the specified refs (default is 'HEAD')
--no-blob-protection allow the BFG to modify even your *latest* commit. Not recommended: you should have already ensured your latest commit is clean.
--private treat this repo-rewrite as removing private data (for example: omit old commit ids from commit messages)
--massive-non-file-objects-sized-up-to <size>
increase memory usage to handle over-size Commits, Tags, and Trees that are up to X in size (eg '10M')
<repo> file path for Git repository to clean