Last active
May 19, 2022 06:48
-
-
Save cansik/ad9bdc68d9513016e963f71f722d8a50 to your computer and use it in GitHub Desktop.
A script to remove all lfs files and commits of a repository.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
did you check that this script delete all the lfs from all the branches ?