DD
DevDash

Regex for Git URL

Regex Pattern

^(?:git|https?|ssh):\/\/[^\s]+\.git$|^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+:[^\s]+\.git$

Git repository URL (HTTPS, SSH, or git:// protocol)

Quick Answer

The regex pattern for git url is `^(?:git|https?|ssh):\/\/[^\s]+\.git$|^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+:[^\s]+\.git$`. Git repository URL (HTTPS, SSH, or git:// protocol). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
https://github.com/user/repo.git✓ Matches
git@github.com:user/repo.git✓ Matches
git://github.com/repo.git✓ Matches
https://github.com/user/repo✗ No match
not-a-git-url✗ No match
ftp://example.com✗ No match

Code Examples

javascript

const regex = /^(?:git|https?|ssh):\\/\\/[^\s]+\.git$|^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+:[^\s]+\.git$/;
const isValid = regex.test(value);

python

import re
pattern = r'^(?:git|https?|ssh):\/\/[^\s]+\.git$|^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+:[^\s]+\.git$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^(?:git|https?|ssh):\/\/[^\s]+\.git$|^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+:[^\s]+\.git$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^(?:git|https?|ssh):\\/\\/[^\s]+\.git$|^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+:[^\s]+\.git$/', $value)) {
    echo "valid";
}

java

String pattern = "^(?:git|https?|ssh):\\/\\/[^\\s]+\\.git$|^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+:[^\\s]+\\.git$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.