Download Cheat sheet PDF 12 pages · syntax, editors, patterns, Unicode, performance, debugging
Pattern

Regex for Git branch name

Valid Git branch name — alphanumeric and limited symbols.

The Git branch name regex is ^(?!\/|.*([\/]\.|\/\/|@\{|\\))[^\040\177 ~^:?*\[]+\/?(?<!\.lock|[\/.])$ — copy it below, or open it in the explainer for a token-by-token breakdown.

The pattern

^(?!\/|.*([\/]\.|\/\/|@\{|\\))[^\040\177 ~^:?*\[]+\/?(?<!\.lock|[\/.])$
Try in explainer → Download cheat sheet ↓

What it matches

  • main
  • feature/new-ui
  • release/v1.2
  • hotfix-123

What it doesn't match

  • /leading
  • double..dots
  • has spaces
  • .hidden

Notes & gotchas

Git refname rules: no leading slash, no spaces or special chars (~^:?*[\), no .lock suffix, no double dots, no @{, etc. This is a reasonable approximation; full rules are more complex.

Code in your language

Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.

Open in explainer →

Token-by-token breakdown

Every part of the pattern, left to right:

TokenMeaning
^start of string (or line in multiline mode)
(?!start of a negative lookahead — NOT followed by
\/literal “/”
|OR — try the alternative
.*zero or more of any character
(start of a capturing group
[\/]any of: “/”
\.literal “.”
|OR — try the alternative
\/literal “/”
\/literal “/”
|OR — try the alternative
@literal text “@”
\{literal “{”
|OR — try the alternative
\\literal “\”
)end of group
)end of group
[^\040\177 ~^:?*\[]+one or more: any character except “0”, “4”, “1”, “7”, “ ”, “~”, “^”, “:”, “?”, “*”, “[”
\/?optional (zero or one): literal “/”
(?<!start of a negative lookbehind — NOT preceded by
\.literal “.”
lockliteral text “lock”
|OR — try the alternative
[\/.]any of: “/”, “.”
)end of group
$end of string (or line in multiline mode)

About this pattern

Developer-oriented patterns help with code analysis, log parsing, and data transformation. Use these in build scripts, CI tools, IDE search-and-replace, and code review utilities.

Quick usage in different languages

This exact pattern — with the correct escaping and idioms for each language:

  • JavaScript: /^(?!\\/|.*([\\/]\.|\\/\\/|@\{|\\))[^\040\177 ~^:?*\[]+\\/?(?<!\.lock|[\\/.])$/.test(value)
  • Python: re.match(r"^(?!\/|.*([\/]\.|\/\/|@\{|\\))[^\040\177 ~^:?*\[]+\/?(?<!\.lock|[\/.])$", value)
  • Java: Pattern.compile("^(?!\\/|.*([\\/]\\.|\\/\\/|@\\{|\\\\))[^\\040\\177 ~^:?*\\[]+\\/?(?<!\\.lock|[\\/.])$").matcher(value).matches()
  • C# / .NET: Regex.IsMatch(value, @"^(?!\/|.*([\/]\.|\/\/|@\{|\\))[^\040\177 ~^:?*\[]+\/?(?<!\.lock|[\/.])$")
  • Go: regexp.MustCompile(`^(?!\/|.*([\/]\.|\/\/|@\{|\\))[^\040\177 ~^:?*\[]+\/?(?<!\.lock|[\/.])$`).MatchString(value)
  • Ruby: /^(?!\\/|.*([\\/]\.|\\/\\/|@\{|\\))[^\040\177 ~^:?*\[]+\\/?(?<!\.lock|[\\/.])$/.match?(value)
  • PHP: preg_match('~^(?!\/|.*([\/]\.|\/\/|@\{|\\))[^\040\177 \~^:?*\[]+\/?(?<!\.lock|[\/.])$~', $value)

The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.

Common pitfalls

  • Anchored to the whole string. This pattern uses ^ and $, so it requires the entire input to match. To find it inside a longer text, drop the anchors and use the global (g) flag.
  • Greedy “.*”. A greedy .* / .+ can match more than intended. Use a lazy version (.*?) or a negated class ([^…]) to stop at the right place.
  • Escape it correctly per language. In Java and JavaScript strings each backslash must be doubled (\\d); in Python, Go, and C# use raw/verbatim strings so the backslashes survive.
  • Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the git branch name against a source of truth (database, API, or checksum) where it matters.

Related patterns

More patterns in the Developer category:

See also

Browse all 300 patterns in the library, or open this regex in the interactive explainer to see a token-by-token breakdown, test against custom input, and generate code in seven languages.


← Back to all patterns