Skip to main content
AllDevToolsHub
Back to all patterns

Git Branch Name Validation

Validation

Validates Git branch names per git check-ref-format rules.

/^(?!.*\.\.)(?!.*@\{)(?!.*\\)(?!.*[~^:?*\[\] ])(?!.*\.lock$)(?!\/|.*\/$)(?!.*\/\/)(?!.*\.$)[^\x00-\x1f\x7f]+$/

How it works

Git branch names have several restrictions: no consecutive dots, no @{, no backslash, no special characters (~^:?*[]), no spaces, cannot start or end with /, no consecutive /, cannot end with .lock or a dot. This pattern enforces the most common rules.

Test Cases

Should Match

  • main
  • feature/my-feature
  • release/v1.2.3

Should NOT Match

  • branch..name
  • branch name
  • branch~name

Quick Summary

Validates Git branch names per git check-ref-format rules. Rejects consecutive dots, spaces, special characters (~^:?*[]), and other patterns Git forbids. For definitive validation, run git check-ref-format --branch <name>.

Key Takeaways

Key Takeaways

  • No consecutive dots (..) in the name
  • No spaces, ~, ^, :, ?, *, [, or ] characters
  • Cannot start or end with / or contain //
  • For definitive validation, use: git check-ref-format --branch <name>
Use Cases

When to use it

  • Validating branch name inputs in CI/CD pipeline triggers
  • Checking branch names in Git workflow automation scripts
  • Enforcing branch naming conventions in pre-commit hooks
Watch out

Common Mistakes

  • Using spaces in branch names, use hyphens or slashes instead
  • Not running git check-ref-format for edge cases, this regex covers common cases but not all
FAQ

Git Branch Name Validation, Frequently Asked

What is the recommended branch naming convention?

Common conventions: feature/description, bugfix/issue-123, release/v1.2.0, hotfix/critical-fix. Use lowercase and hyphens.

How do I validate a branch name in a shell script?

git check-ref-format --branch "$branch_name" returns exit code 0 if valid.