git hooks bash — getting the commit message

I am writing a git hook, to run on a commit to the main branch. I need it to parse and look for some text in the the commit message. How do I reference the commit message in my bash script?

Also I would like to know how to run the hook only on commits to the main branch. so developers can quash there local commits and commit to the main branch when they have fixed a bug or something.

  • Git diff parse using grep shell command
  • How to git pull for multiple repos on windows?
  • Bash: How to catch git command failure?
  • Git bash won't let me pull
  • SVN post-commit hook sending a message back to client
  • Find all files except those which is under version control systems (git or SVN)
  • Please and thank you.

  • Bash function to find all Git commits in which a file (whose name matches a regex) has *changed*
  • Git branching strategy for Agile project
  • Svn or Git client (or plugin) to find missing or broken files
  • git error - failed to push some refs (pack-objects died with strange error)
  • Git checkout without committing
  • Problem with git hook for updating site
  • 2 Solutions collect form web for “git hooks bash — getting the commit message”

    The answer depends slightly on what you’re trying to do with the commit message. There are three hooks you could be asking about:

    • prepare-commit-msg is run immediately after the default message is prepared, before the user edits it. The first argument is the name of the file with the commit message. (The second argument indicates where the message came from.)

    • commit-msg is run after the commit message is edited/finalized, but before the commit takes place. If you want to fail the commit if the user’s commit message is bad, or to modify the message, you want this, and the single argument is the name of the file with the commit message.

    • post-commit is run after the commit. It has no arguments, but you can of course get the message from git log -n 1 HEAD (probably with --format=format:%s%n%b or some such). If all you want to do is look for something in the message, and notify based on it, you should use this.

    All of this material is taken from the githooks manpage

    As for running it only on the main branch, all you need is something like:

    if [ "$(git symbolic-ref HEAD)" == "refs/head/master" ]; then
        # do your stuff

    With following command:

    cat $1

    will print your commit message in commit-msg

    Git Baby is a git and github fan, let's start git clone.