Удаление каталогов в истории git с помощью BFG

Если вы столкнулись с тем, что вам надо удалить целиком каталог из истории git, то это может быть проблемой, так как BFG не поддерживает слеши в путях.
Но есть способ обойти это ограничение. Собираем список всех Id-шников у файлов в каталоге и далее этот список скармливаем в BFG на очистку.
git rev-list --all --objects -- path/to/the/directory/to/delete | git cat-file --batch-check='%(objectname) %(objecttype) %(rest)' | grep -Pe '^\w+ blob' | cut -d' ' -f1 > ./to-delete.txt
java -jar bfg.jar --no-blob-protection --strip-blobs-with-ids ./to-delete.txt
Ну и финальным шагом не забываем пробежаться скриптом для очистки мусора
git reflog expire --expire=now --all && git gc --prune=now --aggressive
The principle is simple: create a list of object IDs to strip, and input that to BFG. This means that if an object is referenced through a different path it will be nuked nonetheless.
git rev-list --all --objects -- path/to/the/directory/to/delete
This will list all objects in the subdirectory referenced in all commits which modify the given path. The format isobjectid filepath
.You should run this command to check its output matches what you’d expect.git cat-file --batch-check='%(objectname) %(objecttype) %(rest)'
This will qualify the object with its type. It will turn the previous formatobjectid filepath
intoobjectid type filepath
.grep -Pe '^\w+ blob'
This will filter out non-blob objects.cut -d' ' -f1 > ./to-delete.txt
This will extract the object ID and redirect the output into theto-delete.txt
file.java -jar bfg.jar --no-blob-protection --strip-blobs-with-ids ./to-delete.txt
This runs BFG, giving it the list of objects to remove.