18

I have post-merge hook configured in order to integrate (semi-manually) with another version control system. However sometimes I would like to avoid the the hook from running is there some way to bypass it for git merge?

I knew there is --no-verify command line parameter for git commit. That kind of solution would be perfect for my use case but seems like --no-verify is not working for git commit command.

Tim
  • 398
  • 2
  • 6
  • 13
  • You seem to be on the right track with `--no-verify`, q.v. [this SO post](http://stackoverflow.com/questions/7230820/skip-git-commit-hooks). – Tim Biegeleisen Oct 30 '15 at 06:24

5 Answers5

14

for those who came here because git merge --no-verify --continue doesn't work together - you can just disable hooks termporarily for the merge - e.g. for my current react project I just removed this part from my package.json - as file not staged it will not go into the merge commit

  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.{js,ts,tsx}": [
      "eslint --fix --rule '@typescript-eslint/no-non-null-assertion: error'",
      "prettier --write"
    ],
    "*.css": [
      "stylelint --fix",
      "prettier --write"
    ],
    "*.{json,yml,md,eslintrc,stylelintrc,babelrc}": [
      "prettier --write"
    ]
  },
godblessstrawberry
  • 4,556
  • 2
  • 40
  • 58
  • if anyone's using `pre-commit`, I had to enable innocuous hooks, otherwise pre-commit complained about an invalid, and the merge would fail. – Mattwmaster58 May 02 '23 at 13:30
14

You can temporarily disable git hooks by doing the following:

$ git config core.hooksPath # to see the current value
$ git config core.hooksPath '/dev/null'
$ git merge --continue
$ git config core.hooksPath '/value/you/had/before'

Adapted from this helpful answer.

Marcel Kooi
  • 290
  • 3
  • 8
  • 1
    Thank you! After the `merge --continue` I had tho use `git config --unset core.hooksPath` because my `hooksPath` first returned nothing. You could add this to your answer. – Ramon Dias Aug 06 '22 at 20:44
11

There is no flag to disable running a post-merge hook, but you could easily rig your own post-merge hook (which you control completely, since it's in your own repository) to obey an environment variable. For instance, a shell script might start with:

#! /bin/sh
warn() {
    echo "warning: $1" 1>&2
}
case ${SKIP_POST_MERGE_HOOK:-no} in
yes) exit 0;;
no) ;;
*) warn "mystery value ${SKIP_POST_MERGE_HOOK} for SKIP_POST_MERGE_HOOK";;
esac
... rest of code as usual ...

Now if you want to run git merge but bypass the hook (or run something like the git pull convenience script that runs git merge for you), simply set SKIP_POST_MERGE_HOOK=yes for the duration:

$ SKIP_POST_MERGE_HOOK=yes git merge ...
torek
  • 448,244
  • 59
  • 642
  • 775
3

A simple approach (without having to alter config files) that worked for me is:

  1. git merge main
  2. Fix merge conflicts, if any
  3. git add .
  4. git commit --no-verify
  5. git push

This is an example so you can definitely specify which files to add and add a commit message as well. I faced the same issue as the OP when trying to git merge --continue.

git version: 2.35.1

melher39
  • 31
  • 1
  • 3
1

I've done the trick by using the GIT_REFLOG_ACTION environment variable.

When using git pull, you'll find that GIT_REFLOG_ACTION=pull in your git hook.

Hence, you can do something like the following:

# if git pull, abort
if [[ $GIT_REFLOG_ACTION == "pull" ]]; then
  echo "git pull detected, aborting"
  exit 0
fi

I hope this can help!

r.stagi
  • 93
  • 1
  • 1
  • 6