如何使用BFG清理本地Git存储库

叶浩荡
2023-12-01

这是您可以在执行提交之后并推送到原始位置之前清理本地git repo的方法。

前期工作

1.下载BFG.jar
https://rtyley.github.io/bfg-repo-cleaner/ https://repo1.maven.org/maven2/com/madgag/bfg/1.13.0/bfg-1.13.0.jar

2.创建目录结构

mkdir - p ~/.binaries/

3.将bfg移到适当位置并对其进行chmod

mv bfg -1.13 .0 .jar ~/.binaries/ && chmod 777 ~/.binaries/bfg -1.13 .0 .jar

4.设置BASH_PROFILE

echo ' alias bfg= "java -jar ~/.binaries/bfg-1.13.0.jar" ' >> ~ /.bash_profile
echo "export PATH=/Users/$(whoami)/.binaries:$PATH" >> ~ /.bash_profile
echo ' alias bfg= "java -jar ~/.binaries/bfg-1.13.0.jar" ' >> ~ /.bash_profile
echo "export PATH=/Users/$(whoami)/.binaries:$PATH" >> ~ /.bash_profile

5.采购

source ~/.bash_profile

BFG清洗仓库

现在我们该怎么办。

我将创建一个新文件夹,然后在该文件夹中创建一个git repo。
这与进行git clone相同...

mkdir testing-bfg
cd testing-bfg/
git init 

现在我要制作一个文件并添加一些内容

vi README.md
# do things
# do things that are bad
PASSWORD=my_password
AWS_TOKEN=1234

将其添加到我的git仓库

git add  README.md 

提交文件的仓库

git commit - a -m "I did an initial commit and it is bad"  

我打算尝试去掌握,或者当我意识到自己做了一件坏事时不要这样做

git push  origin master
*** pre-receive hook fires and  stops me from pushing,  so the push  fails.

要么

我不推送,但是提交已经在我的历史记录中,我需要清理它。

这就是您如何解决它。

为了使BFG正常运行,您不能从git repo的源代码中运行它,至少需要一个文件夹。

cd ../
vi ~/my-mess
#this file is now located at your home folder as denoted by the ~/   thingy
#my-mess file
# these are the strings that you want out of your repo and commit history
AWS_TOKEN=X1V34
PASSWORD=

现在我们有一个肮脏的git testing-bfg位于testing-bfg

我们还有一个文本文件,该文件位于~/my-mess带有“我们要从存储库中删除的内容”

我们将运行别名为bfg的bfg命令,但您也可以在没有别名java -jar bfg-1.13.0.jar情况下执行此java -jar bfg-1.13.0.jar

--no-blob-protection告诉bfg重写我在所有分支中的当前历史记录并进行清理。

bfg --replace-text ~ /my-mess testing-bfg/ --no-blob-protection

现在,根据git,您的文件将处于“已修改”状态。
这是因为您的本地文件中仍然包含BAD字,但是您的提交历史记录却没有。

因此,它被视为已修改。

现在,检查并更改本地文件以更正您的耻辱,并进行新的提交并继续前进。

PROFIT

From: https://hackernoon.com/how-to-cleanup-your-local-git-repository-using-bfg-io1123yis

 类似资料: