35

Problem

I had troubles trying to use git LFS, despite the many suggestions here on SO, on Git and GitHub's documentation, and on some Gists I'd run across.

My problem was as follows:

After performing the necessary steps:

git lfs install
git lfs track "<file of interest>"
git commit

I would still not have any files being tracked. If I performed

git lfs ls-files

it would be blank. If I went ahead & performed the push, the transaction would fail, saying that the files are too large. (As expected, but I was desperate.)

Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
  • 13
    I forgot to ```git lfs install``` it solved the issue for me – Ravin Sardal Jun 28 '16 at 22:54
  • @RavinSardal thanks! I actually had not made that mistake, however I found that this error I was having is now obsolete. I guess something within the LFS internals has been improved, such that having files tracked works more smoothly now. – Mike Williamson Jun 29 '16 at 04:06
  • To anyone who comes across this question, I do not think it is relevant anymore since Git LFS has improved significantly. I am not sure if it is more helpful to delete it or keep it here, but notice that I wrote the question back in 2016. – Mike Williamson Apr 19 '21 at 15:43

4 Answers4

34

New Solution

The original solution is from 2016. Tech progresses...

Seems the best solution now is

git lfs migrate

Thanks to @iff_or for alerting me to this (see comments).

Outdated Solution, don't use

I then discovered a few fixes, some of which seem to be bugs, some of which simply were not obvious to me.

  1. It seems that in order to use lfs with an existing repository, a hack or third party tool such as BFG's converter is needed.

    • I did not want to go that route, so I simply initialized a new repository locally, then did the challenge of hooking it back up to the real repo.
    • I created a new directory, then git init, etc.
      • In my case, the remote repository was GitHub. So I did all those proper hookups like git remote add origin git@github.com:<my_id>/<my_repo>.git
  2. Also, while Git's Training Video claims that you can simply specify a folder, such as "my_folder/", I could not get this to work. Therefore, I just cleverly used filename extensions to manage things.

    • For example, git lfs track "my_folder/" would not work for me, but git lfs track "*.zip" did work.
  3. I had no luck getting LFS files to be identified correctly unless I had first updated the .gitattributes file, and committed and pushed that new file to the remote server.

    • git lfs track "*.zip"
    • git add .gitattributes
    • git commit -m "Updated the attributes"
    • git push
    • git add my_large_file.zip
    • git lfs ls-files
      • And here I would ensure that I saw my_large_file.zip being tracked.
    • git commit -m "Now I am adding the large file"
    • git push

It's possible that some of the things work, and I was simply doing them wrong. However, following the witchcraft described above finally got LFS to work, so I thought I'd pass on these details for others, in case someone else had the same problem.

Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
  • 1
    Hey Mike, I was having all the same problems. I was able to resolve my issues by upgrading to the latest version of git-lfs. Unfortunately, there is no clear documentation for this, so I just downloaded 1.1.2 and ran the shell script again. `git lfs track "directory"` works for me now. – Matt Born Mar 03 '16 at 19:55
  • Hi @MattBorn, thanks for the info! I actually had *just* downloaded git-lfs, so I'm pretty sure that wasn't the problem. Regardless, I was able to solve mine, and I wrote the solution above in case others have problems, and as my own future documentation. – Mike Williamson Mar 08 '16 at 23:00
  • 1
    Good overview. I also can't get the folder tracking to work. Any idea why that might be? I can't really rely on extensions for my case. – Gabriel Oct 10 '16 at 15:15
  • Hi @Gabriel, sorry, I am not sure. When I wrote this original answer, Git LFS was quite new, and I felt like many of these "small" issues would eventually sort themselves out. If the folder tracking *still* isn't working, I'd recommend documenting the problem and submitting the bug to Git. I suspect / hope that LFS is far enough along that they are now starting to address these bugs. – Mike Williamson Oct 13 '16 at 16:57
  • 1
    Git LFS with github remote tracking *does not* track the file until the gitattribute file is pushed to the LFS remote server. This is true to new track globs as well. Always verify with `git lfs ls-files` before git push. – shadowbq Dec 28 '18 at 04:11
  • Thanks, @shadowbq ! Truth is, git lfs has changed so much and improved so much since I wrote this post that I think the question and answer have both become irrelevant. I'm not sure if the best practice according to SO policy is to leave this question & answer or delete the whole thing. But anyone stumbling upon it these days should probably ignore both the question and the answers. – Mike Williamson Jan 16 '19 at 16:54
  • mostly it worked, at the end it said .. http://.../.../abc.git/info/lfs/objects/batch from HTTP 502 – H S Rathore Sep 30 '19 at 11:43
  • 1
    @MikeWilliamson People can likely find this elsewhere on SO (e.g. [here](https://stackoverflow.com/a/65498452/2389827) or in the LFS docs, but it might be helpful to update your answer to recommend `git lfs migrate` rather than the third-party tool! – iff_or Jul 20 '22 at 19:33
23

To put files on an existing repo on lfs you can also do:

git lfs migrate import --include="*.mp3,*.pth"

(Replace .mp3 and .pth with the file extension you wish to put on lfs)

PeterD
  • 1,331
  • 12
  • 22
  • Good to know a proper way of migrating has now been established. :) – Mike Williamson Dec 29 '20 at 09:26
  • i think this works only if you are starting from a fresh repository and with the flarge files not already committed. in my case i had already committed the files and this did not work. – septerr Jul 13 '22 at 00:07
13

To track all files and sub directories of my_folder via LFS, you can do:

git lfs track "my_folder/**"

It worked for me.

Doing the following is not working as of now to track whole my_folder directory:

git lfs track "my_folder/" 

is not working

amarnath
  • 785
  • 3
  • 19
  • 23
Tejaswini
  • 351
  • 3
  • 10
2

As suggested in the comments by @Ravin Sardal, all I had to do to fix the issue was to run

git lfs install

in the base directory of the repository to setup large file support. After that

git lfs track "<file of interest>"

worked as expected and indeed git lfs ls-files listed the tracked files before the commit.

obachtos
  • 977
  • 1
  • 12
  • 30
  • `git lfs install` works for me, if you already add files, you should reset and re-add it to see it works. – Sailist May 10 '23 at 04:01