简体   繁体   English

使用指定消息进行git commit时更改项目中的文件

[英]Change file in project when git commit with specified message

What is best way to provide that some of the git hooks change file in project when i make commit with specified message? 当我用指定的消息进行提交时,提供项目中某些git挂钩更改文件的最佳方法是什么? For example, when i do $git commit -m "MODIFY" i want to hook's script modify my file in a certain way. 例如,当我执行$git commit -m "MODIFY"我想挂钩的脚本以某种方式修改我的文件。 Modification of file is not problem. 修改文件不是问题。 Problem is how to make git hook to recognize specific message as trigger for modification. 问题是如何使git hook将特定消息识别为修改触发器。

You could use the post-commit hook. 您可以使用post-commit钩子。

https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks

After the entire commit process is completed, the post-commit hook runs. 整个提交过程完成后,将执行提交后挂钩。 It doesn't take any parameters, but you can easily get the last commit by running git log -1 HEAD. 它不带任何参数,但是您可以通过运行git log -1 HEAD轻松获得最后的提交。 Generally, this script is used for notification or something similar. 通常,此脚本用于通知或类似内容。

Below is a sample that can be placed in .git/hooks/post-commit . 以下是可以放在.git/hooks/post-commit的示例。 And make sure to make this an executable file 并确保将其设为可执行文件

#!/bin/bash
echo Running post-commit hook
git log -1 --pretty=oneline HEAD | grep "MODIFY" && touch somefile.txt

You could fetch the last commit message via log -1 HEAD & pass it through grep to match a pattern 您可以通过log -1 HEAD获取最后的提交消息,并将其通过grep传递以匹配模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM