解决Mac下SourceTree pre-commit 被跳过的问题

  • 在项目中添加了pre-commit的时候格式化代码的相关配置,然后发现提交后,代码没有被格式化,然后去查看提交时的log发现以下内容:
Can't find npx in PATH: /Applications/Xcode.app/Contents/Developer/usr/libexec/git-core:/Applications/SourceTree.app/Contents/Resources/bin:/usr/bin:/Applications/SourceTree.app/Contents/Resources/git_local/gitflow:/Applications/SourceTree.app/Contents/Resources/git_local/git-lfs:/usr/bin:/bin:/usr/sbin:/sbin
Skipping pre-commit hook
Completed successfully

或者

  • 在Mac下使用sourcetree提交代码,出现如下报错:
.git/hooks/pre-commit: line XXX: node: command not found

在 https://stackoverflow.com/questions/12881975/git-pre-commit-hook-failing-in-github-for-mac-works-on-command-line
中有问题分析:使用诸如SourceTree这样的gui,可以访问的环境变量不一样,需要在.git/hooks/pre-commit脚本中加入你的node环境变量

  • 首先打印你的node目录:
$ which node

比如(每个人的目录都不同)

/usr/local/opt/node/bin

  • 然后将目录加入到 你的git项目下 .git/hooks/pre-commit
# $PATH是已有目录
PATH="/usr/local/opt/node/bin:$PATH"

问题解决

参考:
Sourcetree在Mac下git pre-commit钩子无法使用node问题解决 https://www.jianshu.com/p/e70d735358eb

你可能感兴趣的:(解决Mac下SourceTree pre-commit 被跳过的问题)