Skip to content

Instantly share code, notes, and snippets.

@cansik
Last active May 19, 2022 06:48
Show Gist options
  • Select an option

  • Save cansik/ad9bdc68d9513016e963f71f722d8a50 to your computer and use it in GitHub Desktop.

Select an option

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/zsf
echo "converting lfs to normal git repo..."
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 without lfs
export GIT_LFS_SKIP_SMUDGE=1
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
LFSFILE=`echo "$line" | awk -F" - " '{print $2}'`
LSFFILES+=( "$LFSFILE" )
done < "../$FILESTXT"
# remove git lfs
git lfs uninstall
git rm .gitattributes
# remove files from head and commit
for LFSFILE in ${LSFFILES[@]}; do
# maybe used --cached to not delete file locally
FILENAME=`echo "$LFSFILE" | rev | cut -d '/' -f 1 | rev`
git rm -f "*$FILENAME"
# git add "$LFSFILE" # use only when cached
done
git commit -m "removed lfs files from repository"
# 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
echo ""
echo ""
echo "FINISHED! now run 'git push' to upload it to the server!"
git lfs push origin master --all
git push --no-verify --force
@alonkrispin43
Copy link
Copy Markdown

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