I need to check for "PROMOTE" keyword in message whenever there is a merge with master and other branch
Eg: If the git command is:
git merge -m "MERGE COMMIT 1 FROM BRANCH1 TO MASTER" f145cd536463e595f1e4306eeec6f92a9cd6c734
The merge should exit with a error "PROMOTE key word not set in merge message"
git merge -m "PROMOTE COMMIT 1 FROM BRANCH1 TO MASTER" f145cd536463e595f1e4306eeec6f92a9cd6c734
The merge should be successful.
The problem with the
merge
command is that it is not guaranteed to create a commit unless you use--no-ff
. For example you might get the following result from your command.See this post and this PDF for a great explanation of fast-forward vs no fast-forward when merging. Some people think
-no-ff
muddies history and it definitely breaksgit-bisect
andgit blame
. However others love--no-ff
because it allows merge comments and visualization of what happened in a feature branch.If you decide to use
--no-ff
, you can configure git to to only do--no-ff
globally or on a per branch basis. Use this command to configure--no-ff
on master only.Let's get back to your original problem.
Git supports hooks that can be used for verification and work flow. Hooks can be run at the local client or server where developers push changes. A regular expression using the client hook
commit-msg
, like this one, seems perfect. However git does does not have a pre-merge hook that you could use to reject the merge. See here for a list of hooks and their triggering commands. Even worse you will notice that thegit merge
does not trigger any of the hooks even if you force a commit with--no-ff
.So you have two choices. They are the same as those mentioned in this post which explores the topic even further with no better solutions.