loading...
Cover image for Pair husky with Git LFS in your JavaScript project

Pair husky with Git LFS in your JavaScript project

mbelsky profile image Max Belsky ・2 min read

As you may know Git has a way to fire off custom scripts when certain important actions occur. This is hooks.

I have worked on a project which uses husky as tool to run formatters and linters on pre-comimit hooks. One day we have decided to cover our forms with screenshot tests to be sure that our changes do not break UI. We have needed to store binary files in our repo, so we choose Git LFS to make git operations like git pull and git clone faster. If you are not familiar with Git LFS check this awesome Atlassian’s guide.

This seemed like a good and simple solution. I’ve started work on integration Git LFS in our project. Its Getting Started looks so easy: just download and run three commands in your terminal. I did fail on this first:

$ git lfs install
Hook already exists: pre-push
…
To resolve this, either:
  1: run `git lfs update --manual` for instructions on how to merge hooks.
  2: run `git lfs update --force` to overwrite your hook.

Both suggested solutions could fix this issue, but not really. The second overwrites a few husky’s hooks:

  • post-checkout
  • post-commit
  • post-merge
  • pre-push

So husky won’t run scripts if you configured any of listed above.

The first is more compatible with husky. However there is still an issue: anyone who will clone that repo should merge hooks manually. That is why I come with one more solution.

Installation

$ rm -rf .git/hooks
$ git lfs install
$ mv .git/hooks ./lfs-hooks
# Uninstall this dependency to restore husky hooks with `npm install`
$ rm -rf node_modules/husky
$ npm install

At this moment husky hooks will be installed in .git/hooks and Git LFS hooks in ./lfs-hooks. Now you need to configure Git LFS hooks running with husky:

  "husky": {
    "hooks": {
      "post-checkout": "echo $HUSKY_GIT_STDIN | lfs-hooks/post-checkout $HUSKY_GIT_PARAMS",
      "post-commit": "echo $HUSKY_GIT_STDIN | lfs-hooks/post-commit $HUSKY_GIT_PARAMS",
      "post-merge": "echo $HUSKY_GIT_STDIN | lfs-hooks/post-merge $HUSKY_GIT_PARAMS",
      "pre-push": "echo $HUSKY_GIT_STDIN | lfs-hooks/pre-push $HUSKY_GIT_PARAMS"
    }
  },

Thanks @mattrabe for this snippet

Finish Git LFS installation with git lfs track <binary files> to set up .gitattributes.

Now save, commit and push. Your collaborators will not need anything to do to start with husky and Git LFS.

Note: When someone clones your repo first of all she need to remove .git/hooks directory because Git LFS creates some hooks by default and husky does not have an overwrite option yet.

$ rm -rf .git/hooks
$ npm install

That is all. Happy coding!

Posted on by:

Discussion

markdown guide
 

Hey Max, thanks for your article. Actually I was stumbling accross the exact same issue this week and your article helped a lot.

I got two further questions. First to the Husky-Params that you pass to the hooks. What are they used for inside the hook? I left them out and it still works fine for me.

Second, have you tried this only on Linux/MAC systems? Because transforming the hooks to executables will not work on Windows-machines, i suppose.

 

Hey,

Thanks for letting know about husky-params optionality. I'll try to make some research about that.

Yes, I've tried on mac only. What do you think may go wrong on windows? I have a PC so can make some checks.