Search code examples
githookgithooksgit-post-receive

How to amend git commit message in post-receive hook


I need to modify commit messages in post-receive hook (add bug tracking id). Is there a graceful way to do it or do I have to script "git rebase -i" stuff?


Solution

  • You can't modify the commit message (or any other part of the commit) as the commit-ID is a cryptographic checksum of the contents of the commit: changing any part of the commit changes the checksum, which means it's not the same commit anymore. You can make a new, different commit (similar to rebase -i), or you can use notes, as already described.

    Your best bet is generally to get the comment adjusted before making the commit in the first place. Failing that, notes are designed to allow you to attach data after the fact. Making a new commit from the old one can be made to work, but will be annoying: the developer who pushes the commit will have to obtain the new commit created by the post-receive hook.