Skip to content
Alex Feinstein edited this page Mar 31, 2020 · 3 revisions

LFS

Install

git lfs install does the following:

  • Adds pre-push hook to .git/hooks.
    post-checkout
    post-commit
    post-merge
    pre-push
  • Adds the following configurations to <repository>/.git/.gitconfig.
    [filter "lfs"]
        clean = git-lfs clean -- %f
        smudge = git-lfs smudge -- %f
        process = git-lfs filter-process
        required = true
  • Creates .git/lfs, which is a local cache.

Instead add this config to your global git config $HOME/.gitconfig.

    git config --global filter.lfs.required true
    git config --global filter.lfs.clean "git-lfs clean -- %f"
    git config --global filter.lfs.smudge "git-lfs smudge -- %f"
    git config --global filter.lfs.process "git-lfs filter-process"

You won’t have to initialize LFS every time you clone a repository.

Boost fetch speed

LFS has the batch mode for downloading files from a server. But it does not work on clone and checkout due to limitation of git’s smudge filters. You can skip LFS’s smudge filter and fetch LFS objects on demand.

For that:

  1. Change the smudge filter configuration:
    git config --global filter.lfs.smudge "git-lfs smudge --skip %f"
    git config --global filter.lfs.process "git-lfs filter-process --skip"
    
    # Or by using
    git lfs install --force --skip-smudge

Run git lfs env and be sure that the smudge filter is skipped.

  1. Fetch LFS objects after checkout or clone:
    # downloads objects with batch mode
    git lfs fetch
    
    # changes objects to binary files
    git lfs checkout

Cloning an existing Git LFS repository

    # downloads lfs files one by one
    git clone
    # downloads faster in batch
    git lfs clone
    
    # pull and download lfs
    git pull
    # will download files in batch
    git lfs pull
    
    git lfs fetch
    git lfs fetch --all
    git lfs fetch --recent
    # download Git LFS content for branches or tags updated in the last 10 days
    git config lfs.fetchrecentrefsdays 10
    
    git lfs push --all

Tracking files

    # Run from project root dir
    git lfs track "*.ogg"
    git lfs track "*.psd" --lockable
    
    # List tracked files
    git lfs track
    
    # You can edit this file manually
    cat .gitattributes
    *.ogg filter=lfs diff=lfs merge=lfs -text
    *.psd filter=lfs diff=lfs merge=lfs -text lockable
    
    # Verify which files are being tracked by LFS
    git lfs ls-files

Commit .gitattributes file

Locking

Locking API is still experimental, and not many providers implemented it.

    git lfs track "*.psd" --lockable

Once file patterns in .gitattributes are lockable, LFS will make them readonly on the filesystem.

    git lfs lock images/foo.psd
    
    git lfs unlock images/foo.psd
    
    git lfs unlock images/foo.psd --force

Prune

    # deletes **Old** files from cache
    git lfs prune

Old files is not

  • current commit
  • recent commit
  • not pushed changes

Recent commit is, by adding:

  • the value of the lfs.fetchrecentrefsdays property
  • the value of the lfs.pruneoffsetdays property (which defaults to three)

https://wac-cdn.atlassian.com/dam/jcr:fda8acc7-67bc-4af0-b047-f6a226aa68bb/08.svg?cdnVersion=918

    # Look what will be deleted
    git lfs prune  --dry-run —-verbose
    
    # Will verify if remote has files before prune
    git lfs prune --verify-remote
    
    git config --global lfs.pruneverifyremotealways true

    # Find filename by Hash ID
    git log --all -p -S <OID>
    
    # find a particular object by OID in HEAD
    $ git grep 3b6124b8b01d601fa20b47f5be14e1be3ea7759838c1aac8f36df4859164e4cc HEAD
    HEAD:Assets/Sprites/projectiles-spritesheet.png:oid sha256:3b6124b8b01d601fa20b47f5be14e1be3ea7759838c1aac8f36df4859164e4cc
    
    # find a particular object by OID on the "power-ups" branch
    $ git grep e88868213a5dc8533fc9031f558f2c0dc34d6936f380ff4ed12c2685040098d4 power-ups
    power-ups:Assets/Sprites/shield2.png:oid sha256:e88868213a5dc8533fc9031f558f2c0dc34d6936f380ff4ed12c2685040098d4

Include / Exclude

    # Exclude files during fetch
    git lfs fetch -X "Assets/**"
    
    # Include files
    git lfs fetch -I "*.ogg,*.wav"
    
    # Exclude and Include
    git lfs fetch -I "Assets/**" -X "*.gif"
    
    git config lfs.fetchinclude "Assets/**"
    git config lfs.fetchexclude "*.gif"

Credentials

Change your remotes’ protocols to ssh, so you will not need to use git’s credential-cache. Otherwise, you have to type login and password for every file you download from the GH media server.

Commited binary file?

  1. Remove LFS client configurations from .gitconfig. It can be checked easily by git lfs env
  2. Reset branch
  3. Add configuration back

Useful commands

    git lfs env
    GIT_TRACE=1 git lfs <cmd>
    git lfs logs last