[Git 4] Managing Large Binaries with Git LFS

How Git LFS works: the Git repository holds only small pointer files, the actual binaries go to LFS storage, and checkout downloads only the versions needed

Written by

in

Git is optimized for text files. But chip projects also have lots of large binary files: hard IP libraries, firmware images, memory initialization data, reference result files and so on.

Every time you commit a file, Git keeps a snapshot of it in the history forever. Edit and commit a 100MB binary ten times and the repository holds 1GB, which everyone who clones it has to download. Git LFS (Large File Storage) solves this.


How Git LFS works

Git LFS is an extension to Git. For the files you specify, it puts a small text pointer instead of the actual content into the Git repository and stores the actual content on a separate LFS server.

How Git LFS works: the Git repository holds only small pointer files, the actual binaries go to LFS storage, and checkout downloads only the versions needed
Figure 1. Only pointers of a few hundred bytes are committed to the Git repository; the actual binaries are stored separately in LFS storage. Clone and checkout download only the versions of the files you need right now.

A pointer file looks like this.

version https://git-lfs.github.com/spec/v1
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
size 104857600
Plain Git Git LFS
What goes into the repository The whole file (every version) A pointer (a few hundred bytes)
What clone downloads Every version of every file Pointers + only the versions needed now
Commit and push speed Slow with many large files Fast

As a user you just add, commit and push as usual, and LFS takes care of the rest behind the scenes.


Installation

# Ubuntu / Debian
sudo apt install git-lfs
# RHEL family (EPEL or the packagecloud repository)
sudo dnf install git-lfs
# macOS
brew install git-lfs

git lfs install          # once per account: registers the LFS hooks (filters) with Git
git lfs version

Using it

Choosing which files LFS manages

cd my-repo
git lfs track "*.bin"
git lfs track "*.gds"
git lfs track "ref_data/**"

track creates a .gitattributes file at the root of the repository.

*.bin filter=lfs diff=lfs merge=lfs -text
*.gds filter=lfs diff=lfs merge=lfs -text
ref_data/** filter=lfs diff=lfs merge=lfs -text

⚠️ You must commit and push .gitattributes too. Without it, other people's Git does not know which files belong to LFS.

git add .gitattributes firmware.bin
git commit -m "Track firmware binaries with LFS"
git push

Checking

git lfs ls-files          # files managed by LFS
git lfs status            # LFS files waiting to be committed
git lfs env               # LFS settings and server address

Downloading

With LFS installed, git clone and git pull download LFS files automatically. To get only the pointers and skip the large files for now:

GIT_LFS_SKIP_SMUDGE=1 git clone <url>     # pointers only
git lfs pull --include="firmware/*.bin"   # only what you need, later

Moving large files already committed into LFS

track applies only to future commits. Large files already in the history stay there. To clean up the history too, use git lfs migrate.

git lfs migrate info                              # which extensions take up the most space
git lfs migrate import --include="*.bin" --everything
git push --force --all                            # the history changes, so a force push is needed

⚠️ migrate import rewrites the commit history. Every commit hash changes, so do it only after everyone on the team has pushed their work and agreed, and everyone must clone again afterwards. If branches are protected, you have to allow force pushes temporarily.


LFS settings on GitLab (administrators)

On self-hosted GitLab, LFS is on by default. You only edit /etc/gitlab/gitlab.rb to turn it off or change where objects are stored. (See Running a self-hosted GitLab.)

# /etc/gitlab/gitlab.rb
gitlab_rails['lfs_enabled'] = true                          # false disables LFS for the whole server
gitlab_rails['lfs_storage_path'] = "/data/gitlab-lfs"       # where LFS objects are stored (default: /var/opt/gitlab/gitlab-rails/shared/lfs-objects)
sudo gitlab-ctl reconfigure

LFS objects pile up separately from the repositories, so be sure to include the LFS storage location in disk capacity planning and backups. You can turn LFS on and off per project under the project's Settings → General → Visibility, project features.


What should go into LFS in a chip project?

Good fit for LFS Why
Hard IP libraries and models (.lib, .db, .gds) Large, change occasionally, need versioning
Firmware and boot images (.bin, .hex) Build outputs, but sometimes a specific version must be pinned
Reference data (golden results, test vectors) Large and binary
Images and PDFs for documents Text diffs are meaningless
Should not go in Instead
Simulation results (FSDB, logs, coverage DBs) Generated fresh every time → .gitignore, or an artifact repository if needed
Intermediate build outputs (simv, csrc/) Can be rebuilt, so .gitignore
Data sets of several GB or more A separate file server or artifact repository

LFS is for "large binaries that need version control". Put in large files that do not need versioning and the LFS server just keeps growing.


Summary

  • Git LFS keeps the repository light by managing large files as pointers + separate storage.
  • git lfs install → git lfs track "pattern" → commit .gitattributes. After that, add, commit and push as usual.
  • Move files already committed with git lfs migrate import, but the history changes, so agree as a team first.
  • LFS is on by default in GitLab. Take care of the storage location and backups.

Further reading

Git (5 posts)

  1. Git Basics: From Installation to Branches, Conflicts and Merge Requests
  2. Practical Git Commands and Tips: stash, add -p, insteadOf
  3. Tidy Commit Messages with Gitmoji: the 20 Most Used
  4. Managing Large Binaries with Git LFS Reading now
  5. Exchanging Changes with diff and patch, Including the -p Option

See the full Tech Notes index

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *