60

How can I add just a single file to GIT LFS?

Most examples show adding a pattern that specifies which files to add to LFS. However, I wish to add single file. If, for example, I do

git lfs track "file.bin"

this will track all files named file.bin regardless of what directory they are in.

I considered adding an exclusion filter (! pattern) to .gitattributes so exclude all directories but that is not supported.

The best I've done so far is to track the file pattern for the file I want to add, add the file and then remove the tracking of that file pattern. This is a little fiddly. Is there a better way?

I want to express the file pattern $ROOT_OF_GIT_REPO/file.bin but am lacking a way to express the $ROOT_OF_GIT_REPO part.

user2746401
  • 3,157
  • 2
  • 21
  • 46

3 Answers3

39

Use:

git lfs track --filename [file path]

Source: git lfs track --help

--filename

Treat the arguments as literal filenames, not as patterns. Any special glob characters in the filename will be escaped when writing the .gitattributes file.

This option has been available since v2.9.0 of the LFS extension (pull request). You can check your local version: git lfs version.

Pamphile
  • 1,566
  • 12
  • 15
24

Include the path to the file, instead of just the filename.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • How can I specify a path of a file that's in the root directory of the repository? – user2746401 Aug 17 '18 at 17:39
  • 4
    With a slash. `/file.txt` – Mark Adelsberger Aug 17 '18 at 18:31
  • I'm afraid that does not work. The file pattern `/file.bin` matches any file named `file.bin` including in sub directories. I feel I need a special character to mark that the file pattern should start with the root of the GIT repo – user2746401 Aug 20 '18 at 07:16
  • @user2746401 : No, it doesn't. Not according to the docs, and not according to my tests (which I just repeated yet again in response to your comment, with the same result). The "special character" you need to know is `/`. – Mark Adelsberger Aug 20 '18 at 12:38
  • Which versions of Git and Git LFS are you using? I'm afraid /file.txt still matches more broadly. Can you point me towards the doc on `/`? – user2746401 Sep 07 '18 at 14:28
  • 2
    https://git-scm.com/docs/gitignore - "A leading slash matches the beginning of the pathname. For example, '/*.c' matches 'cat-file.c' but not 'mozilla-sha1/sha1.c'." – Mark Adelsberger Sep 07 '18 at 14:57
  • 2
    Oh, and https://git-scm.com/docs/gitattributes - "The rules by which the pattern matches paths are the same as in .gitignore files (see gitignore[5]), with a few exceptions..." (Don't worry, the exceptions don't affect what we're talking about.) – Mark Adelsberger Sep 07 '18 at 15:00
  • I suggest you post what observation is it that makes you think this information is incorrect, because it's far more likely that you've drawn conclusions from a misleading observation than that the git docs (and repeated tests to verify the behavior) are wrong. – Mark Adelsberger Sep 07 '18 at 15:02
  • I played around a little more and it was my incorrect observations. @Mark Adelsberger thanks for sticking with the question! Could you add your comment to your answer so I can mark it correct? – user2746401 Sep 10 '18 at 10:42
  • Remember to put (.extension type) too – Frostmourne Nov 06 '19 at 12:55
9

You can specify any filename or path in your repository. You aren't limited to using file extensions. You could also track a specific directory with git lfs track 'assets/*' or an entire directory tree with git lfs track 'assets/**/*'. You could also track individual files specifically with git lfs track path/to/file which will track only that file.

AlexanderKomarov
  • 400
  • 5
  • 11