【问题标题】:Run script before commit and include the update in this commit?在提交之前运行脚本并在此提交中包含更新?
【发布时间】:2015-08-03 07:09:47
【问题描述】:

我编写了一个脚本,通过扫描源代码生成Readme.md 文件(用于 GitHub)。每次在我进行新的提交之前,我都会手动运行这个脚本来更新Readme.md。如果这项工作能自动完成肯定会更好。

目前我正在使用pre-commit git hook,它只能部分工作。 Readme.md 文件得到更新,但更新不是此提交的一部分。我必须在下一次提交中包含它。

有没有办法运行这个脚本并使更新成为这个提交的一部分?

【问题讨论】:

    标签: git githooks


    【解决方案1】:

    根据this SO thread (Can a Git hook automatically add files to the commit?)git add 在最新版本的 git 中无法用于预提交挂钩。

    作为一种解决方法,您可以使用 pre-commit 和 post-commit 挂钩来生成您的 Readme.md,然后在您提交后使用 post-commit hook 提交它,然后用您的修改第二次提交。

    不是我的想法,请点击链接获取原始解释。

    @bitluck 在我链接的线程上回答:

    触摸文件 .commit 或其他东西。 (请务必将此添加到 .gitignore)

    #!/bin/sh 
    echo 
    touch .commit 
    exit
    

    如果 .commit 存在,您知道提交刚刚发生,但是 提交后尚未运行。因此,您可以在这里进行代码生成。 此外,测试 .commit 是否存在:

    • 添加文件
    • commit --amend -C HEAD --no-verify(避免循环)
    • 删除 .commit 文件

      #!/bin/sh
      echo
      if [ -a .commit ]
      then
        rm .commit
        git add yourfile
        git commit --amend -C HEAD --no-verify
      fi
      exit
      

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 2019-10-27
      • 1970-01-01
      • 2015-04-14
      相关资源
      最近更新 更多