So I mainly use Mercurial for my projects and I decided to sit down and learn all Git’s shenanigans to discover if I’m in the right side (at least for me).
I’m learning that git uses namespaces for branches and I would like to know (in your experience) what is the point of namespaces?. In Mercurial I could just set the convention of naming branches like so:
<branch name> //for "origin" branches
<username>/<branch name> //for user branches
I imagine that there is more to it than just the name (aside from avoiding naming conflicts), but what do I get out of this seemingly more complicated (not necessarily bad) model?
Git doesn’t “use” namespaces in the sense like they are required. It offers the completely optional ability for a user to use namespaces if they want them. You can use the exact same naming convention as you did in mercurial if you want, and I imagine that’s what the vast majority of people do.
What namespaces buy you is the ability to combine multiple repositories into one, but be able to only show branches from the one virtual repository. Within the namespace, you can still name the branches using whatever method you prefer. Personally, I don’t really see the benefit over just using multiple repositories, but obviously someone thought it might be useful sometimes.
Git has a ton of features, of which maybe 3-5% see daily usage by the typical developer. The trick to getting along with git, is for those features you don’t want/like/understand, don’t worry about them.
Git doesn’t use namespaces for branches.
A legal Git ref (like a branch) is allowed to look like a filepath as
seen on Unix systems like Linux, which means that e.g.:
feature/xyz
is allowedfeature/zzz/subtask1
Are legal refs.
While these are not:
/
feature//zxy
So you may use this permissible syntax to implement informal
“namespaces”. If you want. But it’s not some kind of feature.[1]
All you have to keep in mind that if you create a branch like:
test/featurex
Then you can never create a branch named test
:
$ git co -b test
fatal: cannot lock ref 'refs/heads/test': 'refs/heads/test/featurex' exists; cannot create 'refs/heads/test'
Just like you couldn’t create a directory test
and also a regular file
test
in the same directory.
Example
The current Git maintainer uses the prefix <initials>/
for all topic
branches in his repo. For example (fetched today, 2023-04-16):
ab/avoid-losing-exit-codes-in-tests
ab/bisect-cleanup
ab/bundle-wo-args
ab/cache-api-cleanup
ab/cache-api-cleanup-users
[snip]
cw/ci-whitespace
cw/doc-pushurl-vs-url
[snip]
Note: don’t confuse this with gitnamespaces
Don’t confuse this “namespace” branching practice with gitnamespaces
which apparently[2] allows a single (remote/server) repository to be
used to serve refs from multiple repositories (because the refs are
stored under refs/namespaces/[…]
):
Git supports dividing the refs of a single repository into multiple
namespaces, each of which has its own branches, tags, and HEAD. Git
can expose each namespace as an independent repository to pull from
and push to, while sharing the object store, and exposing all the refs
to operations such as git-gc(1).
Notes
- And you may as well use
--
,&
or whatever else (legal character)
as the “namespace separator”. But/
does look nicer. 😉 man gitnamespaces
, Git 2.40.0
Never done something serious with mercurial, so I can miss something.
I’m using namespaces in git branch naming pretty much, but I’m never inserting my name into the name of the branch.
For each task that are describe in task tracker we are creating a separate branch, with number of the ticket and short description of the bug. At this point there are no spaces yet:
remotes/origin/275-test-new-urls
remotes/origin/283-fullscreen
remotes/origin/299-current-menu-item
remotes/origin/301-users-api
remotes/origin/306-range-window-resize
remotes/origin/307-dropdown-init-onload
Then, say, you realize that some task need additional branches for experimenting. These branches are de-facto children to “main” task branch, i.e. after completion the should (if should at all) be merged to that very task. Here come namespaces:
remotes/origin/283-fullscreen-buggy-animation
remotes/origin/283-fullscreen.refactor
I’ve seen that slash is also used for namespacing, but personally I prefer dots, since using slashes can produce some issues.