Search code examples
gitbfg-repo-cleaner

How to delete folder from git history permanently using BFG


I am using BFG to delete two of my folders. They were tracked from the inception of the repo. The reason to delete was those folder contains binary and other txt file that we no longer need. But When I try to delete those two folder, one gets deleted but the other one still ligers around.

I created my dummy repo and did some commits to recreate the problem. I assume myRepo.git is clean repo to begin with.

Function I used to delete folders is : java -jar bfg-1.13.0.jar --no-blob-protection --delete-folders "{system1, system2}" myRepo.git

#!/bin/bash


BUILD(){
  git clone https://github.com/xxxx/myRepo.git
  cd myRepo
  echo "Jpt test1" > jpt1.txt
  echo "Jpt test2" > jpt2.txt
  echo "Jpt test3" > jpt3.txt

  git add jpt1.txt jpt2.txt jpt3.txt
  git commit -m "first commit"
  git push origin master
  ######
  mkdir system1
  cd system1
  mkfile 14m outputfile1.out
  mkfile 14m outputfile2.out
  echo "Jpt test1" > sysjpt1.txt
  echo "Jpt test2" > sysjpt2.txt
  echo "Jpt test3" > sysjpt3.txt
  cd ..
  ######
  mkdir system2
  cd system2
  mkfile 14m outputfile1.out
  mkfile 14m outputfile2.out
  cd ..

  git add system1 system2
  git commit -m "tracking large file"
  git push origin master
  cd ..

  ##### Call function BFG which does BFG stuff. 
  BFG

}

BFG(){
  # run bfg and let git clean history 

  git clone --mirror https://github.com/xxxx/myRepo.git

  java -jar bfg-1.13.0.jar --no-blob-protection --delete-folders "{system1, system2}" myRepo.git 

  cd myRepo.git
  git reflog expire --expire=now --all && git gc --prune=now --aggressive
  git push 

  cd ..
  mkdir test_new
  cd test_new
  git clone https://github.com/xxx/myRepo.git
  cd myRepo
  ls


}

BUILD

When I clone after cleaning and do ls on it. I get jpt1.txt jpt2.txt jpt3.txt system2. See how system2 folder is still there.


Solution

  • As you tested from my suggestions in the comments above, the problem is with the space in "{system1, system2}". When that expression is processed, it expands it to "system1" and " system2", with a space in front of system2, which is not what you want.

    You can run the process in two commands, once with system1 and once with system2, or simply remove the space, and things will work.

    Interestingly, the expansion of {a,b} appears to be done by bfg itself, not by bash: the quotes tell bash to pass that string literally, so although this looks like bash syntax it actually isn't a bash expansion.