Skip to content

Instantly share code, notes, and snippets.

@everttrollip
Last active November 18, 2024 17:42
Show Gist options
  • Save everttrollip/198ed9a09bba45d2663ccac99e662201 to your computer and use it in GitHub Desktop.
Save everttrollip/198ed9a09bba45d2663ccac99e662201 to your computer and use it in GitHub Desktop.
Removing git lfs from (any) repository

So, it has been an interesting journey, but time to remove git-lfs. Here follows a summary of the approach I used to safely remove git-lfs,

  • commit & push everything
  • create a branch, something like fix/remove-lfs
  • remove hooks git lfs uninstall
  • remove lfs stuff from .gitattributes (open file, delete content - don't delete the file!)
  • list all lfs files, git lfs ls-files
  • run git rm --cached for each file
    • if your list is big, copy the contents into a file.txt
    • make sure you remove the number and asterik on each line, you only want the paths to the files
    • while read line; do git rm --cached "$line"; done < files.txt
  • run git add for each file
    • if you have a file.txt
    • while read line; do git add "$line"; done < files.txt
  • run a git status and make sure all the files were added properly
  • commit everything
git add .gitattributes
git commit -m "unlfs"
git push
  • check that no lfs files left with git lfs ls-files
  • remove any lfs, rm -rf .git/lfs

Once your branch (fix/remove-lfs) is merged into develop, your team doesn't need to do anything other than simply pulling and checking out the new state of the world, their repository will work as-expected without git lfs installed. If git lfs is still installed, simply let them uninstall it: git lfs uninstall

Credits: Fedor, Taylor, Chase, Simon

@nuc
Copy link

nuc commented Nov 4, 2020

git lfs ls-files | cut -f 3 -d ' ' | xargs git rm --cached instead of creating a files.txt.

@bytemain
Copy link

git-lfs/git-lfs#3026 (comment)

@kezakool :

easiest way to do it that worked for me :
first commit & push everything
then

git lfs uninstall

then manually remove lfs stuff from .gitattributes

git lfs untrack '<pattern>' (in my case, all files "*.*")
git add --renormalize .
git commit -m 'Restore file contents that were previously in LFS' ```

@danisztls
Copy link

danisztls commented Apr 6, 2021

git-lfs uninstall --local && echo " " > .gitattributes && git add --renormalize . && git commit -m "Restore sanity"

@pavelsolis
Copy link

Does this line is all that is needed to solve the whole issue? Or does it substitute some steps? I am new and just want to avoid making my problem bigger.

@danisztls
Copy link

@pavelsolis Yes, AFAIK. It had two typos, which I fixed. But make a backup beforehand if possible. You can copy the dir and if things go wrong, delete the messy one and rename the copy.

@pavelsolis
Copy link

Thanks! I tried it as git-lfs and git lfs, in both cases the output was 'lfs' is not a git command. Instead of installing lfs, what worked was to remove .git/lfs and .git/hooks/pre-push.

@danisztls
Copy link

danisztls commented May 12, 2021

You must have it installed to run it. But all git-lfs uninstall --local does is remove .git/hooks/pre-push. I think it will create problems if you didn't do the re-normalize stuff as you will have pointers to stuff that does not exist anymore. After pushing to origin, have you tried to clone the repo and see if things are alright?

@nishalsach
Copy link

Hi, I tried doing this from top to bottom, and I think I messed something up: I removed Git LFS, but I still have pointers instead of the original files. I'm not really sure how to fix this. git lfs ls-files returns nothing, which I guess means LFS isn't tracking these files anymore. Does that mean my data is gone forever and I have pointers to nothing?

@anthonypena97
Copy link

anthonypena97 commented Mar 2, 2022

This really helped point the way! Thank you!

@strickvl
Copy link

Just to add to this, if you are tracking a ton of files, the following little Python script will create a new filenames.txt that that removes the number and asterisk from each line:

with open("filenames.txt", "w") as f:
    f.write("")

with open("files.txt", "r") as f2:
    for line in f2:
        with open("filenames.txt", "a") as f:
            f.write(line.split(" ")[-1])

@TheDragonNidhogg
Copy link

Thank you so much for your help. I have been trying to do this for over a week and your guide finally let get rid of lfs.

@tigerhawkvok
Copy link

Created a Python script to do this:

https://gist.github.com/tigerhawkvok/adb7905a2423312f237da1953d6a8eeb

Should work on all platforms (including Windows), and can generate a dry-run script for you to inspect. (Note the dry-run script won't actually edit your .gitattributes or your .git/lfs folder, it'll just show you the new contents of .gitattributes and verify the to-be-removed path)

@inyourface34456
Copy link

How do I fix this error:

E: Conflicting values set for option Signed-By regarding source https://packagecloud.io/github/git-lfs/ubuntu/ focal: /usr/share/keyrings/gitlfs-archive-keyring.gpg != /etc/apt/keyrings/github_git-lfs-archive-keyring.gpg
E: The list of sources could not be read.
E: Conflicting values set for option Signed-By regarding source https://packagecloud.io/github/git-lfs/ubuntu/ focal: /usr/share/keyrings/gitlfs-archive-keyring.gpg != /etc/apt/keyrings/github_git-lfs-archive-keyring.gpg
E: The list of sources could not be read.

This was caused after I ran the install script (obtained using curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash). I need to use apt again, and I do not know how to fix it.

@LecDoit
Copy link

LecDoit commented Jul 23, 2024

i am not able to uninstall LFS this is crap

@Nosenzor
Copy link

Nosenzor commented Oct 9, 2024

i am not able to uninstall LFS this is crap

Same

@angadsinghsandhu
Copy link

same lol

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