Remote Software Engineer at Stripe and cellist based out of Ontario. Previously at GitLab. Fascinated with building usable, delightful software.
October 19, 2019 | 2 minutes to read
How many different special characters can you jam into a Git ref name (i.e. the full name of a tag or a branch) before Git will complain?
The answer is… a lot.
Here’s a list of ref names that Git considers valid, ranging from pedestrian to ludicrous:
a/bA/Ba/b/c/da/b.c0/1/2!/"#%&'{}+,-.<>;=@]_`{|}🙂🚀😂🇺🇸💩🇨🇦💯👍❤️/🤦(╯°□°)/╯︵┻━┻¯|_(ツ)_/¯The opposite is a little less flashy, but equally useful. All of these are invalid ref names:
a: At least one / is requireda/.b: Slash-separated components can’t begin with a .a/b.: No ending with a .a/.lock: No ending with .locka/b..c: Consecutive dots (..) are not alloweda/~^:?*[\: None of the characters after a/ are alloweda/b/: No ending with a slash/a/b: No beginning with a slasha/b//c: No consecutive slashesa/@{: The @{ sequence is not allowedThere’s some nuance to this second list - some of these rules can be relaxed in special situations. For the complete specification, check out the documentation for the check-ref-format command.
/!Right! When you create a branch named my-feature, Git actually creates a ref named refs/heads/my-feature.
This is a bit of a tangent, but what Git is actually doing under the hood is creating a new file named my-feature inside your repo’s .git directory at .git/refs/heads/my-feature. You can see this for yourself by opening up .git/refs/heads in a file explorer. Understanding this makes it more obvious why the / character is required.
If all you do with Git is pull, commit, push, and maybe the occasional rebase, you can ignore these edge cases. Just keep using nice, simple, boring names like my-feature-branch or v1.2.
However, if you’re building a tool that interacts with Git refs, you might want to throw a few of the crazier names listed above into your test cases.
In fact, this was my motivation for compiling these lists. I built a new GitLab Issue search feature that involved Git tag names, and I needed to make sure it handled any tag name the user threw at it. I was hoping to find something like the Big List of Naughty Strings for Git refs, but I couldn’t find anything beyond Git’s technical documentation.
I wouldn’t be surprised! Please let me know by opening an issue!
The “exploding head” icon was created by Anniken & Andreas from the Noun Project.
Other posts you may enjoy:
November 25, 2024 | 11 minutes to read
October 14, 2024 | 3 minutes to read
May 31, 2024 | 6 minutes to read
June 26, 2023 | 14 minutes to read
January 25, 2022 | 6 minutes to read