Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I write a pre-merge hook in Git?

The question says it all. Is there a way to perform an action before a merge? I'm guessing there's a way to make use of a pre-commit hook, but I'm not quite sure.

like image 437
joshin4colours Avatar asked Sep 30 '13 19:09

joshin4colours


People also ask

What is pre merge in git?

pre-merge-commitIt takes no parameters, and is invoked after the merge has been carried out successfully and before obtaining the proposed commit log message to make a commit. Exiting with a non-zero status from this script causes the git merge command to abort before creating a commit.

How do you do a pre-commit hook?

Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.

Can you commit pre-commit hooks?

pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. Confusingly, there is also a Python package called pre-commit which allows you to create and use pre-commit hooks with a way simpler interface.

Where are git pre-commit hooks?

All git hooks are stored in the . git/hooks/ directory under your project root. A pre-commit hook is just an executable file stored in this folder with the magic name pre-commit .


2 Answers

You can try using the prepare-commit-msg hook. The second argument will be merge "if the commit is a merge or a .git/MERGE_MSG file exists". A non-zero exit status will abort the commit.

I don't think this will work with a fast-forward merge, since there won't be a commit message.

More info on hooks: https://www.kernel.org/pub/software/scm/git/docs/githooks.html#_prepare_commit_msg

like image 61
Kousha Avatar answered Sep 28 '22 20:09

Kousha


With Git 2.24 (Q4 2019), no need for script wrapper, or prepare-message hook.

A new "pre-merge-commit" hook has been introduced.

See commit bc40ce4, commit 6098817, commit a1f3dd7 (07 Aug 2019) by Michael J Gruber (mjg).
See commit f78f6c7 (07 Aug 2019) by Josh Steadmon (steadmon).
(Merged by Junio C Hamano -- gitster -- in commit f76bd8c, 18 Sep 2019)

git-merge: honor pre-merge-commit hook

git-merge does not honor the pre-commit hook when doing automatic merge commits, and for compatibility reasons this is going to stay.

Introduce a pre-merge-commit hook which is called for an automatic merge commit just like pre-commit is called for a non-automatic merge commit (or any other commit).

The documentation now includes:

pre-merge-commit

This hook is invoked by git-merge.
It takes no parameters, and is invoked after the merge has been carried out successfully and before obtaining the proposed commit log message to make a commit.
Exiting with a non-zero status from this script causes the git merge command to abort before creating a commit.

The default 'pre-merge-commit' hook, when enabled, runs the 'pre-commit' hook, if the latter is enabled.

This hook is invoked with the environment variable GIT_EDITOR=: if the command will not bring up an editor to modify the commit message.

If the merge cannot be carried out automatically, the conflicts need to be resolved and the result committed separately (see git-merge).
At that point, this hook will not be executed, but the 'pre-commit' hook will, if it is enabled.

like image 43
VonC Avatar answered Sep 28 '22 19:09

VonC