22

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.

Max
  • 396
  • 4
  • 13
  • 1
    Related question here: https://stackoverflow.com/questions/59210453/remove-git-lfs-link-to-file-and-add-it-to-git-directly/ – rkedge Dec 10 '19 at 21:13

3 Answers3

22

.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.

rkedge
  • 512
  • 3
  • 12
  • 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. – ramwin Mar 03 '21 at 02:33
  • I had issues with this syntax using pre-commit-hooks. Using ! as suggested below solved the issue – Daniel Pinyol Jan 16 '22 at 20:44
  • After updating the `.gitattributes` file, how do I replace the local files with their dereferenced content (instead of the `git-lfs` reference)? – wpcarro Jun 15 '22 at 18:08
22

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.

xiay
  • 855
  • 8
  • 19
-2

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.

bk2204
  • 64,793
  • 6
  • 84
  • 100