Skip to content

Instantly share code, notes, and snippets.

@larsxschneider
Created June 28, 2015 17:12
Show Gist options
  • Save larsxschneider/a31fb77c7e9a9c9b81da to your computer and use it in GitHub Desktop.
Save larsxschneider/a31fb77c7e9a9c9b81da to your computer and use it in GitHub Desktop.
Git LFS test using BFG
#!/usr/bin/env bash
GIT_REMOTE="https://path/to/your/repo.git"
# Download BFG from here:
# https://github.com/rtyley/bfg-repo-cleaner/releases/download/git-lfs-alpha/bfg-1.12.4-SNAPSHOT-git-lfs-7242876.jar
GIT_BFG="/path/to/downloaded/bfg-1.12.4-SNAPSHOT-git-lfs-7242876.jar"
rm -rf bfg-upload
mkdir bfg-upload
pushd bfg-upload > /dev/null
# Generate repo
git init
git remote add origin $GIT_REMOTE
echo "commit 1, binary file A" > fileA.bin
git add .
git commit -m "commit 1"
echo "commit 2, binary file B" > fileB.bin
echo "commit 2, binary file C" > fileC.bin
git add .
git commit -m "commit 2"
# Rewrite history
java -Xmx1g -jar $GIT_BFG \
--convert-to-git-lfs -fi "*.{bin}" \
--private \
--no-blob-protection .git
git reset --hard
# Fix LFS object directories (this will be obsolete with the next BFG release I think)
for FILE in .git/lfs/objects/*; do
OBJ_ID=$(basename "$FILE")
OBJ_DIR=".git/lfs/objects/${OBJ_ID:0:2}/${OBJ_ID:2:2}"
mkdir -p "$OBJ_DIR"
mv "$FILE" "$OBJ_DIR/$OBJ_ID"
done
# Upload files manually to LFS
for FILE in .git/lfs/objects/*/*/*; do
OBJ_ID=$(basename "$FILE")
# TODO: We could pass multiple object IDs here to leverage batch upload
git lfs push --objectid origin $OBJ_ID
done
# Add LFS filter
# TODO: Is there a way to do this fast with BFG?
git filter-branch --tree-filter "echo '*.bin filter=lfs diff=lfs merge=lfs -crlf' > .gitattributes" HEAD
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push -f origin master
popd > /dev/null
# Clone the repo to test that everything worked!
rm -rf bfg-clone
git clone $GIT_REMOTE bfg-clone
pushd bfg-clone > /dev/null
cat fileA.bin
cat fileB.bin
cat fileC.bin
popd > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment