简体   繁体   中英

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? For example, when i do $git commit -m "MODIFY" i want to hook's script modify my file in a certain way. Modification of file is not problem. Problem is how to make git hook to recognize specific message as trigger for modification.

You could use the post-commit hook.

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. Generally, this script is used for notification or something similar.

Below is a sample that can be placed in .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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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