Regex To Match Git Repositories

If you’re using Git for version control, you may want to know how to match Git repository URLs.

My problem was when I wanted to create a regex for this purpose. It took me hours before I found what I believe to be the best regex code for tasks like these. With this tutorial, hopefully, you’ll save yourself some time and make your job easier.

/((git|ssh|http(s)?)|(git@[\w\.]+))(:(//)?)([\w\.@\:/\-~]+)(\.git)(/)?/g

Matches:

  • https://github.com/microsoft/PowerToys.git
  • [email protected]:microsoft/microsoft.git
  • ssh://user@yoursite/com:port/path/to/repo.git/

Non-matches:

See Also: