Regex for Git Branch Name
Regex Pattern
^(?!.*\.\.)(?!.*\/\/)(?!\/)[a-zA-Z0-9/_.-]+(?<!\.)(?<!\/)(?<!\.lock)$Valid git branch name following git-check-ref-format rules
Quick Answer
The regex pattern for git branch name is `^(?!.*\.\.)(?!.*\/\/)(?!\/)[a-zA-Z0-9/_.-]+(?<!\.)(?<!\/)(?<!\.lock)$`. Valid git branch name following git-check-ref-format rules. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| main | ✓ Matches |
| feature/my-feature | ✓ Matches |
| release/v1.0.0 | ✓ Matches |
| fix/bug-123 | ✓ Matches |
| ..invalid | ✗ No match |
| branch..name | ✗ No match |
| /leading-slash | ✗ No match |
| name.lock | ✗ No match |
Code Examples
javascript
const regex = /^(?!.*\.\.)(?!.*\\/\\/)(?!\\/)[a-zA-Z0-9\/_.-]+(?<!\.)(?<!\\/)(?<!\.lock)$/; const isValid = regex.test(value);
python
import re
pattern = r'^(?!.*\.\.)(?!.*\/\/)(?!\/)[a-zA-Z0-9/_.-]+(?<!\.)(?<!\/)(?<!\.lock)$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^(?!.*\.\.)(?!.*\/\/)(?!\/)[a-zA-Z0-9/_.-]+(?<!\.)(?<!\/)(?<!\.lock)$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^(?!.*\.\.)(?!.*\\/\\/)(?!\\/)[a-zA-Z0-9\/_.-]+(?<!\.)(?<!\\/)(?<!\.lock)$/', $value)) {
echo "valid";
}java
String pattern = "^(?!.*\\.\\.)(?!.*\\/\\/)(?!\\/)[a-zA-Z0-9/_.-]+(?<!\\.)(?<!\\/)(?<!\\.lock)$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
UUID v4
UUID version 4 (randomly generated)
UUID (Any Version)
Any RFC 4122 UUID (versions 1-5)
JWT Token
JSON Web Token (header.payload.signature)
Hex String
Any-length hexadecimal string
Base64 String
Standard base64 encoded string
Alphanumeric Username
Username with letters, digits, underscores (3-16 chars)