Reputation: 11
I want to edit the default merge message of Git, I've a server side hook declared, that validates if the commit message begins with 7 numbers (Eg. "1234567 - solve the bug 3").
Sometimes we do a local merge between 2 branches, so, the merge generates a default commit message without the numbers like this: "Merge branch 'newBranch' into newBranch2"
If the developer doesn't see the auto merge message, and do another commit(s) after the merge, when he'll push the branch to the assembla the server decline the push cause the merge message is wrong.
How can I change the prefix of the auto commit message to "0000000 - " without "--amend" and without "git merge -m "?
Upvotes: 0
Views: 3960
Reputation: 11
merge.ff not work.
I did the solution using hook script (prepare-commit-msg) in the hooks directory. The script verify the message content has the word "merge" and if it don't has "0000000" (zeros is the default that we're using in merge messages instead of the ticket number)
#!/bin/sh
#
# Automatically add zeros in the merge commit message if it has not the zeros.
#
COMMIT_EDITMSG=$1
MERGE_MSG="0000000 - "
MERGE=$(cat $COMMIT_EDITMSG|grep -i 'merge'|wc -l)
NUMEROS=$(cat $COMMIT_EDITMSG|grep -i '000000'|wc -l)
if [ $MERGE -ne 0 ] && [ $NUMEROS -eq 0 ] ; then
sed -i.bak -e "1s/^/$MERGE_MSG /" $1
fi
So, if a developer forget to put the numbers in the automatic commit messages, when he try to push the code to assembla, the server will not block his commit because of the message.
Upvotes: 0
Reputation: 2856
Take a loook at --edit
flag for git merge
: https://www.kernel.org/pub/software/scm/git/docs/git-merge.html
--edit
--no-edit
Invoke an editor before committing successful mechanical merge to further edit the auto-generated merge message, so that the user can explain and justify the merge. The --no-edit option can be used to accept the auto-generated message (this is generally discouraged). The --edit option is still useful if you are giving a draft message with the -m option from the command line and want to edit it in the editor.
Older scripts may depend on the historical behaviour of not allowing the user to edit the merge log message. They will see an editor opened when they run git merge. To make it easier to adjust such scripts to the updated behaviour, the environment variable GIT_MERGE_AUTOEDIT can be set to no at the beginning of them.
update
To simplify you may add alias:
git config --global alias.emerge 'merge --edit'
and then just use git emerge <branch>
instead of git merge --edit <branch>
update 2
Maybe you don't need to 'be able to edit default commits msg' but to create separate commit even for fast-forward merged (this is considered to be a good practice to do a separate commit even for fast-forwards).
The you just need to set merge.ff
option to false
.
Run git config merge.ff false
(for single repo) or git config --global merge.ff false
(globally for all repos).
merge.ff
By default, git does not create an extra merge commit when merging a commit that is a descendant of the current commit. Instead, the tip of the current branch is fast-forwarded. When set to false, this variable tells git to create an extra merge commit in such a case (equivalent to giving the --no-ff option from the command line). When set to only, only such fast-forward merges are allowed (equivalent to giving the --ff-only option from the command line).
(https://www.kernel.org/pub/software/scm/git/docs/git-config.html)
Once this performed each git merge
command will create separate commit and user will be dropped to message editor
Upvotes: 1