Skip to main content
Code & Markup · In-Browser Regular Expression Lab

Git Commit Hash Regex — Short (7-Char) and Full (40-Char) Hashes

Matches Git commit SHA hashes: 7-character short commit IDs or 40-character full SHA-1 hashes.

Flags:
//i
Match Found
Load Sample:
Match Inspection & Details1 match
Match #1:7866615

Test Samples Verification Matrix

Expected Valid Samples (Should Match)

7866615 Pass
f21e094 Pass
4b825dc642cb6eb9a060e54bf8d69288fbee4904 Pass

Expected Invalid Samples (Must Reject)

12345 Rejected (Pass)
not-a-hash Rejected (Pass)
4b825dc642cb6eb9a060e54bf8d69288fbee4904z Rejected (Pass)
Code Implementation Generator
// JavaScript / TypeScript (ES6+)
const pattern = /^\b([a-f0-9]{7}|[a-f0-9]{40})$/i;
const text = "7866615";

const isMatch = pattern.test(text);
console.log("Is Match:", isMatch);

if (isMatch) {
  const matches = text.match(pattern);
  console.log("Matches:", matches);
}

Regular Expression Token Breakdown

Matches standard Git short hashes (7 hex chars) or complete repository commit hashes (40 hex chars). Each component of the regular expression serves a distinct syntactical purpose as mapped below:

Token PatternName / ComponentFunction & Rule
[a-f0-9]{7}Short HashStandard 7-char abbreviated Git commit.
[a-f0-9]{40}Full SHA-1Full 40-char commit hash.

Frequently Asked Questions

Does Git support SHA-256?
Yes, newer Git repositories support 64-char SHA-256 hashes.
Browse All 40 Patterns