Skip to content

Instantly share code, notes, and snippets.

@cnruby
Last active June 13, 2020 08:39
Show Gist options
  • Save cnruby/70ba34ebf25e6c7e7b914bf33a44dd8c to your computer and use it in GitHub Desktop.
Save cnruby/70ba34ebf25e6c7e7b914bf33a44dd8c to your computer and use it in GitHub Desktop.
BFG Repo-Cleaner: Remove files and folders from git history permanently(代码库历史文件和目录删除软件)
  • Remove files and folders from git history permanently(代码库历史文件和目录删除软件)
  • Removes large or troublesome blobs like git-filter-branch does, but faster, simpler, better and easier

Step 0: Get tool: BFG Repo-Cleaner

wget https://repo1.maven.org/maven2/com/madgag/bfg/1.13.0/bfg-1.13.0.jar

Step 1: download your Repo

REPO=<YOUR_REPO>
example:
  REPO=w3h1_cmake.git

git clone --mirror https://github.com/<YOUR_ACCOUNT>/$REPO
example:
  git clone --mirror https://github.com/cnruby/$REPO

Step 2: Remove History Files

RM_FILES=<REMOVE_FILES> && java -jar bfg-1.13.0.jar --delete-files $RM_FILE $REPO
example:
  RM_FILES=basic_139-07.mp4 && java -jar bfg-1.13.0.jar --delete-files $RM_FILES $REPO
  RM_FILES=basic_139-{02,03,04,05,06}.m4a && java -jar bfg-1.13.0.jar --delete-files $RM_FILES $REPO

Step 2: Remove History Folder

RM_FOLDER=<REMOVE_FOLDER> && java -jar bfg-1.13.0.jar --delete-folders $RM_FOLDER  --no-blob-protection $REPO
example:
  RM_FOLDER=_build && java -jar bfg-1.13.0.jar --delete-folders $RM_FOLDER  --no-blob-protection $REPO

Step 3: Updated state of your repo

cd $REPO
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push

optional Step 4, if INFOS exsits during 'git push': "deny updating a hidden ref"

cd ..
RM_FILES=<REMOVE_FILES> && java -jar bfg-1.13.0.jar --delete-files $RM_FILE $REPO
example:
  REF_PATH=refs/original && git for-each-ref --format='delete %(refname)' $REF_PATH | git update-ref --stdin

cd $REPO
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push

extra Step: 'SEARCH Files' or Check

git clone https://github.com/<YOUR_ACCOUNT>/$REPO $REPO-search
example:
  git clone https://github.com/cnruby/$REPO $REPO-search

cd $REPO-search

SEARCH_FILES=<SEARCH_FILES>
example:
  SEARCH_FILES="*.mov"
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch $SEARCH_FILES' --prune-empty --tag-name-filter cat -- --all
# RUN NO: 'git push'!
cd ..
rm -rf $REPO-search

References

REF_PATH=<REF_PATH> && git for-each-ref --format='delete %(refname)' $REF_PATH | git update-ref --stdin
example:
  REF_PATH=refs/original && git for-each-ref --format='delete %(refname)' $REF_PATH | git update-ref --stdin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment