Code
Git commit SHA Regex
Git identifies every commit by a hash written in lowercase hexadecimal. Tools usually show a short form of 7 to 12 characters, while the full ID is 40 characters for SHA-1 repositories and 64 characters for the newer SHA-256 format. This pattern accepts those three lengths. It is useful for CI scripts that read commit references, release notes generators and chat bots that link commits. Lengths between 13 and 39 are rejected because git rarely prints them. The pattern cannot tell a commit hash from any other hex string of the same length, and it does not check that the commit exists.
Pattern (JavaScript)
/^(?:[0-9a-f]{7,12}|[0-9a-f]{40}|[0-9a-f]{64})$/Starts at the start of the line/string, then (one of the characters 0-9a-f, between 7 and 12 times OR one of the characters 0-9a-f, exactly 40 times OR one of the characters 0-9a-f, exactly 64 times), then the end of the line/string.
Examples
Matches
- ✓ a1b2c3d
- ✓ 3f786850e387550fdab836ed7e6dc881de23001b
- ✓ 9fceb02d0ae
- ✓ 0000000000000000000000000000000000000000000000000000000000000000
Does not match
- ✗ a1b2c3
- ✗ g1b2c3d
- ✗ A1B2C3D
- ✗ 3f786850e387550fdab836ed7e6dc881de23001b0
Try it
0 matches (JavaScript engine)
a1b2c3d
3f786850e387550fdab836ed7e6dc881de23001b
9fceb02d0ae
0000000000000000000000000000000000000000000000000000000000000000
a1b2c3
g1b2c3d
A1B2C3D
3f786850e387550fdab836ed7e6dc881de23001b0Code in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^(?:[0-9a-f]{7,12}|[0-9a-f]{40}|[0-9a-f]{64})$/g;
const text = "a1b2c3d";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^(?:[0-9a-f]{7,12}|[0-9a-f]{40}|[0-9a-f]{64})$')
text = 'a1b2c3d'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^(?:[0-9a-f]{7,12}|[0-9a-f]{40}|[0-9a-f]{64})$/';
$text = 'a1b2c3d';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^(?:[0-9a-f]{7,12}|[0-9a-f]{40}|[0-9a-f]{64})$");
String text = "a1b2c3d";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^(?:[0-9a-f]{7,12}|[0-9a-f]{40}|[0-9a-f]{64})$`)
text := "a1b2c3d"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^(?:[0-9a-f]{7,12}|[0-9a-f]{40}|[0-9a-f]{64})$");
var text = "a1b2c3d";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^(?:[0-9a-f]{7,12}|[0-9a-f]{40}|[0-9a-f]{64})$").unwrap();
let text = "a1b2c3d";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^(?:[0-9a-f]{7,12}|[0-9a-f]{40}|[0-9a-f]{64})$/
text = 'a1b2c3d'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why is uppercase hex rejected?
- Git always prints hashes in lowercase. Rejecting uppercase helps tell commit IDs apart from other hex values such as colour codes or keys. Add the i flag if your source changes the case.
- What length should a short SHA be?
- Git starts at 7 characters and grows the abbreviation as a repository gets larger to keep it unique. Large projects often show 10 to 12 characters, which is why the range stops at 12.
- What is the 64 character form?
- Git can use SHA-256 instead of SHA-1 for object IDs, which produces 64 hex characters. Few hosting services support it yet, but including it future-proofs the check.