Notes

Delete files forever from a git repo on Github or Bitbucket

delete personal/private files from a repo that is publicly available and you have accidentally pushed files that shouldn’t be out there like SSH keys, passwords, configs etc. Do this using BFG Repo-Cleaner to alter the history of your Git repo

Edit on GitHub

Git & Github
3 minutes

tl;dr

1git clone --mirror REPO_URL
2java -jar ~/Downloads/bfg-1.13.0.jar --delete-files FILE_NAME MY_REPO.git
3cd MY_REPO.git
4git reflog expire --expire=now --all && git gc --prune=now --aggressive
5git push # this will force update all refs as well because you cloned with --mirror

 1# see if java is installed
 2echo $(/usr/libexec/java_home)
 3
 4# download and install Java
 5# https://java.com/en/download/mac_download.jsp
 6# jre-8u191-macosx-x64.dmg
 7
 8# to use Java runtime and command line tools you need to install Java Developer Kit
 9# install JDK
10# https://www.oracle.com/technetwork/java/javase/downloads/index.html
11# jdk-11.0.1_osx-x64_bin.dmg
12
13# download BFG Repo-Cleaner
14# https://rtyley.github.io/bfg-repo-cleaner/
15
16# check that BFG works 
17java -jar ~/Downloads/bfg-1.13.0.jar
18
19# mirror the repo
20# clone a fresh copy of your repo, using the --mirror flag
21git clone --mirror git@bitbucket.org:aamnah/deleteme.git
22
23# use bfg
24java -jar ~/Downloads/bfg-1.13.0.jar --delete-files id_rsa deleteme.git
25
26# go to the repo, cleanup, and push changes
27cd deleteme.git
28git reflog expire --expire=now --all && git gc --prune=now --aggressive
29git push # this will force update all refs as well because you cloned with --mirror

So i pushed some files that were personal to a public Github repo. Nothing too damaging but still stuff i wouldn’t want to be included on a tech blog.

For the sake of this tutorial let’s just assume that i committed SSH keys, and NO you don’t want the public to have your keys..

1java -jar ~/Downloads/bfg-1.13.0.jar --delete-files id_rsa deleteme.git

Replace all passwords listed in a file (prefix lines ‘regex:’ or ‘glob:’ if required) with ***REMOVED*** wherever they occur in your repository :

1$ bfg --replace-text passwords.txt  deleteme.git

Help

 1# java -jar ~/Downloads/bfg-1.13.0.jar
 2bfg 1.13.0
 3Usage: bfg [options] [<repo>]
 4
 5  -b, --strip-blobs-bigger-than <size>
 6                           strip blobs bigger than X (eg '128K', '1M', etc)
 7  -B, --strip-biggest-blobs NUM
 8                           strip the top NUM biggest blobs
 9  -bi, --strip-blobs-with-ids <blob-ids-file>
10                           strip blobs with the specified Git object ids
11  -D, --delete-files <glob>
12                           delete files with the specified names (eg '*.class', '*.{txt,log}' - matches on file name, not path within repo)
13  --delete-folders <glob>  delete folders with the specified names (eg '.svn', '*-tmp' - matches on folder name, not path within repo)
14  --convert-to-git-lfs <value>
15                           extract files with the specified names (eg '*.zip' or '*.mp4') into Git LFS
16  -rt, --replace-text <expressions-file>
17                           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***'.
18  -fi, --filter-content-including <glob>
19                           do file-content filtering on files that match the specified expression (eg '*.{txt,properties}')
20  -fe, --filter-content-excluding <glob>
21                           don't do file-content filtering on files that match the specified expression (eg '*.{xml,pdf}')
22  -fs, --filter-content-size-threshold <size>
23                           only do file-content filtering on files smaller than <size> (default is 1048576 bytes)
24  -p, --protect-blobs-from <refs>
25                           protect blobs that appear in the most recent versions of the specified refs (default is 'HEAD')
26  --no-blob-protection     allow the BFG to modify even your *latest* commit. Not recommended: you should have already ensured your latest commit is clean.
27  --private                treat this repo-rewrite as removing private data (for example: omit old commit ids from commit messages)
28  --massive-non-file-objects-sized-up-to <size>
29                           increase memory usage to handle over-size Commits, Tags, and Trees that are up to X in size (eg '10M')
30  <repo>                   file path for Git repository to clean