Skip to content

Instantly share code, notes, and snippets.

@cansik
Last active May 19, 2022 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cansik/ad9bdc68d9513016e963f71f722d8a50 to your computer and use it in GitHub Desktop.
Save cansik/ad9bdc68d9513016e963f71f722d8a50 to your computer and use it in GitHub Desktop.
A script to remove all lfs files and commits of a repository.
#/bin/zsh
echo "converting lfs to normal git repo..."
if [ $# -eq 0 ]
then
echo "Use: rm-lfs.sh [http-git-url]"
exit
fi
REPO=$1
REPODIR=`echo "$REPO" | rev | cut -d '/' -f 1 | rev`
FILESTXT="$REPODIR-lfs.txt"
# prepare
if [ -d "$REPODIR" ]; then rm -Rf "$REPODIR"; fi
# clone
git lfs uninstall
git clone "$REPO" "$REPODIR"
# list all files
cd "$REPODIR"
git lfs ls-files --all > "../$FILESTXT"
# create lfs file index
LSFFILES=()
while IFS= read -r line
do
# caution, sometimes it's an asterix, sometimes a dash!
# https://stackoverflow.com/a/36585611/1138326
NORMLINE=`echo $line | sed 's/*/-/'`
echo "$NORMLINE"
LFSFILE=`echo "${NORMLINE#* - }"`
LSFFILES+=( "$LFSFILE" )
done < "../$FILESTXT"
# remove git lfs
git rm .gitattributes
# run bfg cleaner for each file
LSFFILENAMES=()
for LFSFILE in "${LSFFILES[@]}"; do
FILENAME=`echo "$LFSFILE" | rev | cut -d '/' -f 1 | rev`
LSFFILENAMES+=( "$FILENAME" )
done
echo "removing all files with a single command"
LFSARGS="{"$(IFS=,; printf '%s' "${LSFFILENAMES[*]}")"}"
echo $LFSARGS
bfg --delete-files "$LFSARGS"
# reflog
git reflog expire --expire=now --all && git gc --prune=now --aggressive
# remove files from head and commit
for LFSFILE in "${LSFFILES[@]}"; do
FILENAME=`echo "$LFSFILE" | rev | cut -d '/' -f 1 | rev`
# completely removes file
git rm -f "*$FILENAME"
# removes file from lfs an adds them to normal git
# git rm --cached "*$FILENAME"
# git add "*$FILENAME"
done
git commit -m "removed lfs files from repository"
echo ""
echo ""
echo "FINISHED! now run 'git push --force' to upload it to the server!"
echo "After that install git lfs again: 'git lfs install'"
echo "And remove git lfs-index from repo: 'rm -rf .git/lfs'"
read -p "Press enter to push changes!"
git push --force
# remove chunk
rm -rf .git/lfs
git lfs install
exit
@alonkrispin43
Copy link

did you check that this script delete all the lfs from all the branches ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment