Files are still (not) in GIT LFS after changing .gitattributes
Asked Answered
N

2

6

When using GIT LFS, .gitattributes controls which files are managed by GIT LFS and which ones are not. According to the documentation, all it takes to move files between GIT LFS and regular GIT storage is to change .gitattributes.

I have modified .gitattributes in a Bitbucket repository, but git lfs ls-files still lists the same files as before, including files that should no longer be managed by GIT LFS and not including files that should now be managed by GIT LFS. Also, committing and pushing the change in .gitattributes is suspiciously fast.

Is there a command that I can (or have to) run to update the GIT LFS status of all files and move them to the correct storage?

Nerval answered 27/7, 2017 at 11:59 Comment(3)
Where did you read that it is enough to edit .gitattributes to move files in and out of lfs?Gastroscope
You need to remove the tracked files from your index, and then re-add them, after asking git lfs to start tracking that file and/or those types of files. Editing .gitattributes won't move anything. .gitattributes is used to tell git and git lfs which files should be added as lfs and not just tracked normally when adding those files.Gastroscope
What if the files are not in the index but already committed and pushed?Nerval
C
6

Well, it seems that git lfs have to be used like git lfs track something.bin, and manually change .gitattributes won't really add exist files to LFS.

If you manually modify the .gitattributes file, my solution is:

After modify the file, run these commands:

git rm --cached -r .
git add -A

It may take a while if there is a lot of files. But it does the trick.

Candlefish answered 16/10, 2020 at 13:15 Comment(1)
Every time I do any lfs related commands, it just resets .gitattributes to what it was before doing this.Frerichs
L
3

The issue is that git lfs track actually does two things:

  1. It adds a line to the .gitattributes file
  2. It updates the file's timestamp to ensure that it will be converted

When manually editing the .gitattributes file, you only do step 1. As the file does not change, Git will not trigger LFS to convert the file.

To solve this, you can force Git to re-add the file, and thereby convert it to an LFS pointer:

  • git add --renormalize --all to check all files in the repository
  • or, e.g., just run git add --renormalize "**.ext" to only check files with a specific extension

(source)

Lactoflavin answered 12/7, 2023 at 15:2 Comment(4)
Does this also work for files that are already in GIT-LFS, or only for files that are about to be added and in the index?Nerval
If I understand your question correctly, you ask about files that were previously versioned regularly in Git (so not as LFS pointers) and that you are now wanting to track via LFS. Then yes, it works the same, although there is a simpler way to achieve that: git lfs migrate import --no-rewrite "<your-file>".Lactoflavin
No, the opposite. But the problem exists both ways.Nerval
So you're asking about removing files from LFS then? This can be achieved much the same way, just using the export functionality: git lfs migrate export "<your-file>"Lactoflavin

© 2022 - 2024 — McMap. All rights reserved.