Git commit hash regex pattern

Copy and test a JavaScript regex for Git commit hashes, with examples and a one-click link to the regex tester.

Git commit hash regex pattern

Validate whole value

Use anchors to check whether the complete value matches.

Regex: ^[0-9a-f]{7,40}$

Flags: i

Search in text

Find matching fragments inside a larger block of text.

Regex: \b[0-9a-f]{7,40}\b

Flags: gi

Examples that match

  • a1b2c3d
  • 0f4c2a1b9d6e

Examples that do not match

  • xyz1234
  • abc123