Git LFS on Heroku

Git LFS allows you to version large files while storing them outside of your Git repository. Heroku doesn’t have built-in support for it, so a few additional steps are needed to make it work. Keep in mind that Heroku’s maximum slug size is 500 MB compressed.

This tutorial assumes you’re already using Git LFS and are ready to deploy your app to Heroku.

Getting Started

We’ll use two buildpacks: one for SSH keys and another for Git LFS.

heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-ssh-key.git
heroku buildpacks:add --index 2 https://github.com/raxod502/heroku-buildpack-git-lfs.git

Next, specify the location of the repo. Use the SSH url (not HTTPS).

heroku config:set HEROKU_BUILDPACK_GIT_LFS_REPO=git@github.com:user/repo.git

Authentication

GitHub, GitLab, and Bitbucket all support read-only access with SSH keys. Generate an SSH key pair:

ssh-keygen -f /tmp/heroku_rsa -N ""

Add the public key to your Git provider.

Provider From the repository page, go to...
GitHub Settings > Deploy Keys
GitLab Settings > Repository > Deploy Keys
Bitbucket Repository settings > Access keys

And copy and paste the output of:

cat /tmp/heroku_rsa.pub

Add the private key to your Heroku app.

heroku config:set BUILDPACK_SSH_KEY="$(cat /tmp/heroku_rsa)"

Deployment

Finally, use the --no-verify flag to deploy.

git push heroku master --no-verify

Otherwise, it’ll fail with:

batch response: Repository or object not found: https://git.heroku.com/app.git/info/lfs/objects/batch
Check that it exists and that you have proper access to it

Set up an alias to save some typing.

alias gpp="git push heroku master --no-verify"

We’ve now successfully deployed a repo with Git LFS to Heroku.

Published October 31, 2020


You might also enjoy

Scaling the Monolith

16 New ML Gems for Ruby

Dokku on DigitalOcean


All code examples are public domain.
Use them however you’d like (licensed under CC0).