Ensuring JIRA issue numbers in Git commit messages

We’re switching from SVN to Git and want to make sure every commit has a JIRA issue number. Right now we use a commit-msg hook to check for this but it doesn’t work when people clone the repo.

Here’s what we’ve tried:

#!/bin/bash
commit_msg=$(cat $1)
if [[ ! $commit_msg =~ PROJ-[0-9]+ ]]; then
  echo "Error: Commit message must include JIRA issue number"
  exit 1
fi

This works locally but not for everyone. We need JIRA to see all changes linked to issues.

Is there a better way to do this? Maybe something that sticks with the repo when it’s cloned? Or are we using Git wrong somehow?

Any ideas on how to make this work would be super helpful. Thanks!

Having worked on similar projects, I can suggest an alternative approach that’s been effective. Instead of relying solely on commit hooks, consider implementing a server-side solution using Git’s pre-receive hooks. These run on your Git server (e.g., GitLab, GitHub Enterprise) and can enforce commit message standards across all pushes.

You can configure the pre-receive hook to check for JIRA issue numbers in commit messages before accepting pushes. This ensures compliance regardless of individual developer setups. Additionally, integrating this check into your CI/CD pipeline adds an extra layer of verification.

To make it easier for developers, provide a commit template that includes a placeholder for the JIRA issue number. This can be set up in the repository’s .gitconfig file, which will be distributed to all clones.

Remember, the key is to combine technical solutions with clear communication and team buy-in for the best results.

I’ve faced a similar challenge when our team transitioned to Git. While commit hooks are useful, they don’t travel with the repository, as you’ve observed. In our case, we overcame this by adopting several complementary strategies.

We set up a custom Git template so that new clones automatically included our commit-msg hook. We also enhanced our CI/CD pipeline to reject pushes that lacked a proper JIRA issue reference, ensuring any oversight was caught before merging into the main branch.

Additionally, we created a Git alias that would prompt for the JIRA number and prepend it to the commit message automatically, a change that was easily embraced by the team. Regular training sessions further reinforced the necessity of linking commits with JIRA issues.

Together, these measures significantly improved our integration process and ensured better traceability without adding unnecessary complexity.

hey there! have u tried using git hooks on the server side? that way, it’ll check every push no matter where it comes from. u could set up a pre-receive hook that looks for the JIRA number pattern. it’s pretty easy to do and works great for our team. just a thought!