Git add in pre-commit hook not staging file for commit

限于喜欢 提交于 2019-12-10 11:35:54

问题


I have written a pre-commit hook that compiles my project and adds the generated file to the commit.

This is a JavaScript project and I am using husky, but I have experimented with editing the .git/hooks/pre-commit as well and the file is not getting added to the commit. If I cancel the commit, I can see the file has been added, but for some reason this is not applying to the current commit.

My pre-commit hook looks something like:

const shell = require('shelljs');

shell.exec('yarn bundle');
shell.exec('git add dist');
shell.exit(0);

shelljs is just a library to execute cross-OS unix commands in node

I edited the .git/hooks/pre-commit to run git add dist and the file is still not added to the commit


回答1:


I don't think a git add can work in a pre-commit hook, made to inspect what is about to be committed, not to modify it.

You can follow an approach similar to "Can a Git hook automatically add files to the commit?" instead, which creates a separate additional commit.



来源:https://stackoverflow.com/questions/51778574/git-add-in-pre-commit-hook-not-staging-file-for-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!