DEV Community

Cover image for Pre-push Commit Message Validation on Github (Enterprise)
Daniel Kraus
Daniel Kraus

Posted on • Updated on

Pre-push Commit Message Validation on Github (Enterprise)

If you are looking for commit message validation on GitHub's Marketplace (or other places on the Internet), e.g., to enforce conventional commits, most solutions are based on GitHub Actions such as GsActions/commit-message-checker. While this does the job, I believe using GitHub Actions for commit message validation has two downsides:

  1. Since validation happens as part of a workflow, it consumes runner minutes.
  2. And because it is a workflow, the check happens post-push (from a Git server perspective).

If you are using GitHub Enterprise (Cloud, GHEC or Server, GHES), there is actually a GitHub-native solution: metadata restrictions.

Metadata restricions can be part of a branch or tag ruleset. That is, such a ruleset can be defined on the repository or organization level. In the latter case, you can also use custom properties to only target a subset of repositories (e.g., those of a specific team).

In any case, when you create the ruleset, you have to scroll to the bottom of the page to "Restrictions" and check "Restrict commit metadata", where it says:

Restrict commit author email addresses, committer email addresses, commit message content, and other metadata

Then click on "Add restriction", which opens the following dialog:

GitHub metadata restriction dialog

For "Requirement", you can choose among the following options:

  1. Must start with a matching pattern
  2. Must end with a matching pattern
  3. Must contain a matching pattern
  4. Must match a given regex pattern
  5. Must not start with a matching pattern (negation of 1.)
  6. Must not end with a matching pattern (negation of 2.)
  7. Must not contain a matching pattern (negation of 3.)
  8. Must not match a given regex pattern (negation of 4.)

Afterwards, click "Add" and activate (or evaluate) the ruleset – that's it!

I think this a great way to validate commit messages pre-push, GitHub-natively, without consuming runner minutes. And hopefully, this will someday also be part of GitHub's free tier. 🤞

Top comments (0)