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

git pre-commit hook example
git commit-msg hook examples
git post-checkout hook example
python git hooks
add pre-commit hook to repo
useful git hooks
make git hook executable
git pre tag hook

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.


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

githooks - Hooks used by Git, Git hooks are scripts that Git executes before or after events such as: commit, Update your code, make a commit and push, and your code can be running in  The hooks are all stored in the hooks subdirectory of the Git directory. In most projects, that’s .git/hooks.When you initialize a new repository with git init, Git populates the hooks directory with a bunch of example scripts, many of which are useful by themselves; but they also document the input values of each script.


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.

Git Hooks, Method used to create the ref. Possible values: - auto-merge deployment api - blob edit - branch merge  $ pre-commit install --hook-type pre-merge-commit pre-commit installed at .git/hooks/pre-merge-commit The hook fires after a merge succeeds but before the merge commit is created. Note that you need to be using at least git 2.24 which added support for the pre-merge-commit hook.


Another nice workaround would be to add a shell script, call it like you want, then add these lines to the script:

git() {
    if [ "$1" == "merge" ]; then
        echo "seems to work like a charme"
    fi
    command git "$@"
}

git "$@"

Then make an

alias git="./my-pre-merge-script.sh"

Then you are good to go. You just added your own pre-merge hook. I know, that you do not have access to whatever arguments git would pass to a real pre-merge hook, but you can prepare files or whatever you want to prepare for merge now; I personally am very happy with this approach: I spent 2 or 3 whole days to find something for pre-merge, then I had to go with the pre-commit-msg which I did not find accurate enough for my needs. This solves all my problems. Hope this helps anybody in the future.

Creating a pre-receive hook script, For example, you may want to run some automated tests that make sure the commit doesn't break any existing functionality. No arguments are passed to the pre-  pre-commit This hook is invoked by git commit, and can be bypassed with --no-verify option. Since the hook can easily be bypassed, it seems the answer to your question is "no". Also, since the .git/hooks directory is not cloned, there does not seem to be a mechanism to push it to the client.


Git Hooks, a pre-merge hook using bash scripts, without having to write my own and the second using git hooks within the repositories themselves. An introductory guide and resource for Git hooks. Learn how to use pre-commit hooks, post-commit hooks, post-receive hooks, and more. Created by Matthew Hudson, a programmer experimenting with combining Git + WebHooks + Webpipes.


Solved: Implementing a workflow with a pre-merge hook, [v5,3/4] git-merge: honor pre-merge-commit hook expanded githooks documentation entry * clarified that hook should write messages to stderr * squashed test  Git got the pre-push hook in the 1.8.2 release. Pre-push hooks are what I needed along with pre-commit hooks. Apart from protecting a branch, they can also provide extra security combined with pre-commit hooks. And for an example on how to use (taken and adopted and enhanced from this nice entry)


[v5,3/4] git-merge: honor pre-merge-commit hook, At Liquid Light we have a git hook which tidies up after a merge. Written by Mike Street on 28th March 2019 The .git/hooks folder comes pre-filled with some example hooks you can use, all prepended with a .sample file format. If you wish  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.