35

I have a range of commits that fail the tests that are committed with them. I need to interactive-rebase across these commits; but the hooks are failing, and causing each commit to screw up, forcing me to manually git commit -n each step.

Is there a way to automate this? git rebase --interactive --no-verify doesn't do what I'd expect.

1
  • 1
    have you found a solution to this problem? Sep 19, 2018 at 15:56

4 Answers 4

5

I stumbled upon the same problem, but the only answer I found required modifying the hook itself.

Here the author suggests filtering this situation using the branch name:

#!/bin/bash
BRANCH_NAME=$(git branch | grep '*' | sed 's/* //') 

if [[ $BRANCH_NAME != *"no branch"* ]]
then
  # your regularly scheduled hook
fi

I changed the condition a little bit, as my git output looks like (no branch, rebasing some-branch-name). It does not solve my specific case, but maybe someone will find this question and use this approach in future.

1
  • 1
    that's pretty neat! perhaps REBASING=$(git branch | grep '\* (no branch') would be already enough for checking on the if statement as if [ -z "$REBASING" ] ...
    – zanona
    Nov 26, 2020 at 9:34
1

I like this solution from https://blog.swwomm.com/2018/06/skip-pre-commit-hook-on-git-rebase-or.html:

Add this to the pre-commit hook script:

if [ "$NO_VERIFY" ]; then
    echo 'pre-commit hook skipped' 1>&2
    exit 0
fi

and then update NO_VERIFY when running the interactive rebase:

NO_VERIFY=1 git rebase -i master # or `git merge some-branch` or whatever
2
  • 1
    Or more simply NO_VERIFY=1 git rebase -i master (in one line) so that the variable is not required to be emptied afterwards.
    – Lenormju
    May 15, 2023 at 12:50
  • Would it be possible to detect --no-verify parameter instead?
    – Qwerty
    Jan 16 at 15:16
1

Here's another very quick and non-invasive workaround which doesn't require modifying your existing hooks:

git config core.hooksPath .no-hooks

Once you're done, don't forget to restore the hooks. If you're using the global default hooks path of $GIT_DIR/hooks then you can just revert this override:

git config --unset core.hooksPath

or if you're using something like husky then you'll need:

git config core.hooksPath .husky
0

Variation on the answer given by @s6mike:

Observed that there are already some GIT env vars in the hook context so for my case when wanting to avoid hooks explicitly for rewording a commit without having to set a new env var

if [[ "${GIT_REFLOG_ACTION}" =~ "rebase".*"reword" ]]; then
    exit 0
fi
2
  • Where do I put this?
    – Qwerty
    Jan 16 at 15:15
  • 1
    @Qwerty inside the relevant (pre-commit) hook, before running the actual check. But note that I stated that this is for the reword case. That is if I'm only editing commit message please don't waste my time re-linting/testing code
    – nhed
    Jan 16 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .