gitpre-commit-hookpre-commit

How can I run pre commit hooks again after running "git commit --no-verify"?


Let's say I committed some code to the remote repository but skipped the commit hooks with git commit --no-verify since it was a WIP branch. But now I want to run those pre commit hooks on the same commit using git commit --amend. How can I run the previously skipped commit hooks on a commit?

Thanks


Solution

  • If you make only one commit on the WIP branch, git commit --amend --no-edit can invoke the hooks.

    For a more general case in which you may have one or more than one commit, you can use git rebase and git commit --amend to invoke the hooks on each of them. Suppose the branch history is O-A-B-C-D(HEAD->WIP) and you want to run the hooks on ABCD.

    git switch $WIP
    git rebase -f O --exec 'git commit --amend --no-edit'
    

    The commits ABCD will be reapplied one by one, and after each apply git commit --amend --no-edit is called and invokes the hooks.