Git在pre-commit钩子中添加而不是要提交的登台文件 [英] Git add in pre-commit hook not staging file for commit

查看:89
本文介绍了Git在pre-commit钩子中添加而不是要提交的登台文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个预提交挂钩,用于编译我的项目并将生成的文件添加到提交中.

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

这是一个JavaScript项目,我正在使用husky,但是我也尝试编辑.git/hooks/pre-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只是一个用于在节点中执行跨OS Unix命令的库

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

我编辑了.git/hooks/pre-commit以运行git add dist,但该文件仍未添加到提交中

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

推荐答案

我认为git add不能在

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.

您可以采用类似于" Git挂钩可以自动向提交中添加文件吗?"的方法,创建一个单独的附加提交.

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

这篇关于Git在pre-commit钩子中添加而不是要提交的登台文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆