Can I exclude some files from git-lfs?
Asked Answered
R

3

24

I created a repo with some image files. I have .gitignore and .gitattributes that I want to exclude from lfs. So I need to track all files by lfs but not .gitignore and .gitattributes.

I' m using Sourcetree and I tried that in .gitattributes:

.gitignore      text
.gitattributes  text
*               filter=lfs diff=lfs merge=lfs -text

But all of my files getting tracked by lfs.

Rosemaryrosemond answered 21/8, 2019 at 15:58 Comment(1)
Related question here: #59210953Alanson
B
29

I was looking for the same. I am using git version 2.26.2 and rkedge's answer did not work for me. I found this thread and ended up using:

*.dat         filter=lfs diff=lfs merge=lfs -text
excluded.dat  !filter !diff !merge text 

For files already commited, I had to untrack and re-commit them after modifying .gitattributes.

Braille answered 29/5, 2020 at 9:36 Comment(0)
A
26

.gitattributes works similarly to .gitignore in terms of precedence.

*               filter=lfs diff=lfs merge=lfs -text
.gitignore      filter= diff= merge= text
.gitattributes  filter= diff= merge= text

This will unset the filter, diff, merge for those files, and lfs won't track them.

Alanson answered 10/12, 2019 at 19:45 Comment(3)
This is the answer that should be accepted. I omitted the second line .gitignore filter= diff= merge= text in my repository where I use git to backup my photos.Annmarie
I had issues with this syntax using pre-commit-hooks. Using ! as suggested below solved the issueOvertask
After updating the .gitattributes file, how do I replace the local files with their dereferenced content (instead of the git-lfs reference)?Surfacetosurface
A
-3

Unlike a .gitignore file, a .gitattributes file doesn't have exclude patterns, so there isn't a way to specify that all files except for a few should have a filter (like Git LFS) applied.

You'll need to specify your files in a different way. If all your files are in a directory (say, foo), you can specify foo/**. If they're all images, you could specify a rule for each type of image (say, one for *.png, one for *.jpg, and one for *.gif).

As a final suggestion, if they all start with an alphabetical or numerical character, you could specify [A-Za-z0-9]*, which would exclude all of the dot files as well.

Antipope answered 21/8, 2019 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.