Skip to content

Instantly share code, notes, and snippets.

@christian-korneck
Last active May 31, 2023 16:02
Show Gist options
  • Save christian-korneck/6d14d53b7e4da84aca74c218defac3b4 to your computer and use it in GitHub Desktop.
Save christian-korneck/6d14d53b7e4da84aca74c218defac3b4 to your computer and use it in GitHub Desktop.
git lfs - track all binary files in a folder with git-lfs - `lfsbintrack`
# as demo data, let's use a copy of the alpine rootfs (which contains both binary and text files)
$ wget https://dl-cdn.alpinelinux.org/alpine/v3.18/releases/aarch64/alpine-minirootfs-3.18.0-aarch64.tar.gz
$ tar xf alpine-minirootfs-3.18.0-aarch64.tar.gz
$ ls
/bin
/etc
...

# make this dir a git repo and enable git-lfs
$ git init
$ git lfs install

# add all binary files to the git-lfs tracking
# (uses `binaryornot` from https://github.com/christian-korneck/binaryornot)
$ find . -type d -name '.git' -prune -o -type f -exec bash -c 'binaryornot "$0" >/dev/null; [ $? == 5 ] && /bin/true || /bin/false' {} \; -exec git lfs track {} \;


# use git as usual
git add .
git commit -m "Initial commit."
# (optional: git push)

# now all files are added to git, but only binary files are tracked by git-lfs:
$ git lfs ls-files
652343b174 * bin/busybox
684ec2affa * lib/apk/db/scripts.tar
27e313622c * lib/ld-musl-aarch64.so.1
...
@christian-korneck
Copy link
Author

bash alias (also works on git bash on Windows):

alias lfsbintrack='/bin/find . -type d -name ".git" -prune -o -type f -exec /bin/bash -c "binaryornot \"\$0\" >/dev/null; [ \$? == 5 ] && /bin/true || /bin/false" {} \; -exec git lfs track {} \;'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment