【问题标题】:Git: Update git status part of commit message after pre-commit hookGit:在预提交挂钩后更新提交消息的 git 状态部分
【发布时间】:2016-04-02 06:13:01
【问题描述】:

我正在运行一个预提交挂钩,它可能会将新文件添加到提交中。但是,尽管这在技术上按预期工作,但提交消息的 git status 部分并未反映此更改。

例如,我在提交中添加文件x,所以我的git status 预先给出以下内容:

repo>git status
On branch development
Your branch is up-to-date with 'origin/development'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   x

现在,假设我的 git 钩子确定文件 y 也需要提交。虽然之后我可以看到y 确实是为提交准备的,但我的空提交消息(在执行预提交挂钩之后)给出了以下内容:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch development
# Your branch is up-to-date with 'origin/development'.
#
# Changes to be committed:
#       new file:   x
#

虽然我期待

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch development
# Your branch is up-to-date with 'origin/development'.
#
# Changes to be committed:
#       new file:   x
#       new file:   y
#

确实,在中止提交后,我得到以下git status:

repo>git status
On branch development
Your branch is up-to-date with 'origin/development'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   x
        new file:   y

换句话说,我如何在预提交挂钩中“刷新”我的提交消息的自动生成部分?

编辑: 我的预提交挂钩目前如下所示。

#!/bin/sh
gulp --production

vendor/bin/phpunit

rc=$?
if [[ $rc != 0 ]] ; then
    echo "phpunit failed - commit denied"
    exit $rc
fi

git add public

gulp 执行编译 SCSS 之类的操作,并可能将内容移动到我的 public/ 目录。如果发生这种情况,我想将这些文件添加到提交中,因此最后是 git add public 指令。 因此,即使这些文件确实被添加到提交中,提交消息的git status 部分也不会反映这一点。

【问题讨论】:

  • 能贴出pre-commit-hook的代码吗?
  • 我已经找到了这个问题,但并不完全适用于我的问题,因为我在执行任何提交后挂钩之前编写我的提交消息
  • 啊,我明白了。我希望有人可能有(或有)一个解决方法,但我想我不得不这样做

标签: git commit githooks pre-commit-hook


【解决方案1】:

似乎 git commit-msg 中的 git status 部分是在 pre-commit-hook 运行之前创建的。因此,尽管它不会显示在 COMMIT-MSG 中,但它仍然会被提交。这似乎是 git 的一个小错误。

Can a Git hook automatically add files to the commit?

【讨论】:

    猜你喜欢
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    相关资源
    最近更新 更多