GIT HOOK to check the "PROMOTE" keyword in the message when GIT MERGE is executed
I need to check the "PROMOTE" keyword in the message whenever a merge occurs with master and another branch
For example: if a git command: git merge -m "MERGE COMMIT 1 FROM BRANCH1 TO MASTER" f145cd536463e595f1e4306eeec6f92a9cd6c734
Merge should fail with the error "PROMOTE keyword not specified in the merge message"
git merge -m "PROMOTE COMMIT 1 FROM BRANCH1 TO MASTER" f145cd536463e595f1e4306eeec6f92a9cd6c734
The merge must be successful.
source to share
The problem with the command merge
is that it's not guaranteed to create a commit if you don't use --no-ff
. For example, you can get the following output from your command.
git merge -m "MERGE COMMIT 1 FROM BRANCH1 TO MASTER"
Updating f145cd5..cd6c734
Fast-forward (no commit created; -m option ignored)
See this post and this PDF for a great explanation of fast forward and no fast forward when merging. Some people think -no-ff
muddies history and it definitely violates git-bisect
and git blame
. Others --no-ff
, however, love it because it allows you to combine comments and visualizations of what happened in the trait branch.
If you choose to use --no-ff
, you can only set up git --no-ff
globally or on a per-branch basis. Use this command for configuration --no-ff
on the primary server only.
git config branch.master.mergeoptions "--no-ff"
Let's go back to the original problem.
Git supports hooks that can be used for validation and worker flow. Hooks can be run on a local client or server where developers change changes. A regex using a client hook commit-msg
like this one seems to be perfect. However, git does not have a pre-merge hook that you could use to reject the merge. See here for a list of hooks and their launch commands. Worse, you'll notice that it git merge
doesn't call any of the hooks, even if you force the commit with --no-ff
.
So you have two options. They are the same ones mentioned in this post which explores the topic even more without better solutions.
- Put an update chunk like this one on a git server where developers push. The problem with this approach is that it doesn't stop them from making bad comments. This approach just leaves bad comments from the server. Developers will have to do a lot of history fixing work before they can push to the server if they don't do it right the first time.
- Write (and distribute and maintain ...) a git-merge wrapper that will do this control at the local repo level. Michael J. Gruber has a pre-merged hook in his git fork here .
source to share