Git LFS and husky prepush hook?

See original GitHub issue

I just tried to husky a prepush script and nothing happened. Looking at .git/hooks/pre-push I see that I already have a hook there:

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/pre-push.\n"; exit 2; }
git lfs pre-push "$@"

Running node node_modules/husky/bin/install.js doesn’t help:

husky
setting up hooks
skipping pre-push hook (existing user hook)
done

I guess that’s good, as I don’t want Git LFS to break and get some large files in the repo without noticing…

Is there a husky solution for having a prepush while also using git-lfs?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:36
  • Comments:41 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
mattrabecommented, May 17, 2019

To summarize suggestions by @jmchambers and @Fandekasp, the following works for me and keeps the hooks under version control as desired:

  1. Run git lfs update --manual
  2. Copy each of the scripts the above command spits out into new files under version control. NOT into the .git/hooks/ dir, but instead into a new dir in your repo at hooks/. Name them hooks/pre-push-lfs, hooks/post-checkout-lfs, etc
  3. Make those hook files executable: chmod +x hooks/*-lfs
  4. Point husky to the files you just saved in package.json:
  "husky": {
    "hooks": {
      ...your other hooks...
      "pre-push": "echo $HUSKY_GIT_STDIN | hooks/pre-push-lfs $HUSKY_GIT_PARAMS",
      "post-checkout": "echo $HUSKY_GIT_STDIN | hooks/post-checkout-lfs $HUSKY_GIT_PARAMS",
      "post-commit": "echo $HUSKY_GIT_STDIN | hooks/post-commit-lfs $HUSKY_GIT_PARAMS",
      "post-merge": "echo $HUSKY_GIT_STDIN | hooks/post-merge-lfs $HUSKY_GIT_PARAMS"
    }
  }

Yes, this does mean that if those hooks for LFS ever change that your hook files would need to be updated to stay in sync, but if you look at those hooks files they are pretty inert - they just call the git lfs commands that must possess the actual business logic for the hooks. So I would estimate that is a minor issue - and would be easily solved after a future breaking change by repeating the here-entailed process of saving the hooks from git lfs update --manual to files, easily enforced by noticing that running git lfs update spits out an error after said future update, just like the first time you ran into this which brought you here…

13reactions
KidsKillacommented, Jul 26, 2017

What would probably solve the issue: ability to add hooks code instead of replacing it. For example:

"postinstall": "git lfs update --force && cd ./node_modules/husky && npm run install -- --append"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Pair husky with Git LFS in your JavaScript project
$ git lfs install Hook already exists: pre-push … To resolve this, either: 1: run `git lfs update --manual` for instructions on how...
Read more >
"Hook already exists: pre-push" after adding new git hook
In a nutshell, you need to install the LFS hooks first, move them to a separate directory and then install the Husky hooks....
Read more >
Getting Started with Git Hooks and Husky | Tower Blog
In this fun tutorial, let's see how Git hooks work by creating 5 different hooks with Husky, a popular JavaScript package.
Read more >
Developers - Git LFS and husky prepush hook? - - Bountysource
I just tried to husky a prepush script and nothing happened. Looking at .git/hooks/pre-push I see that I already have a hook there:...
Read more >
Git Hooks | Atlassian Git Tutorial
If you take a look inside .git/hooks , you'll find the following files: applypatch-msg.sample pre-push.sample commit-msg.sample pre-rebase.sample ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found