8

How can I trigger a post-commit hook with the command git cherry-pick <commit>?

What I've tried:

  • I tried the command git commit -m '...' . It triggered the post-commit hook normally.
  • In the githooks document, there are no hooks related to cherry-pick.
  • After viewing the source code of Git, I found it uses git merge in some cases, and git commit in others. But I'm not sure when to use which command.

My questions are:

  1. Why don't post-commit hooks work when I use git cherry-pick?
  2. Is there a hook that cherry-pick will run?
dan1st
  • 12,568
  • 8
  • 34
  • 67
LongFeida
  • 83
  • 1
  • 4

1 Answers1

4

Why don't post-commit hooks work when I use git cherry-pick?

The post-commit hook is run after creating a commit.

However, cherry-pick does not really create a new commit with new information (from the user perspective) but copies another commit.

Is there a hook that cherry-pick will run?

Yes, the prepare-commit-msg should be run before commit is cherry-picked, even though the commit-msg hook is not executed.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Thanks, I want to check the file that has been commited by `git cherry-pick`, like the `post-commit` hook, it need to run after the entire commit process is completed. Is there any way to achieve it? – LongFeida May 20 '21 at 01:21
  • You could try to check if the file is staged. If it is staged, it will be committed. – dan1st May 20 '21 at 04:31
  • It can solve my problem. I truly appreciate your timely help! – LongFeida May 20 '21 at 09:34