confused about creating nested branches in git

假装没事ソ 提交于 2019-12-05 03:51:06

TL;DR version: just git checkout -b develop/feature initially (you'll need to not have a branch named develop for this to work though!).


Branches don't really "nest" for a ridiculously simple reason: a branch name (like newFeature) merely stands in for some commit-ID, i.e., some SHA-1 "true name" of some commit.

(In this respect, a branch name is the same as a tag, like "v2.3".)

The thing that makes (local) branch names special—what makes them different from any other reference—is that "git checkout" will let you get "on a branch" by writing the name of the branch in git's HEAD file, and, once you have done that, making a new commit will automatically update the branch-name so that it points to the new commit you just made.

(Remote branch names can't be gotten "on" this way, but will also change their target SHA-1 as needed. I mention this only because the next paragraph mentions "remote branches" or "remote-tracking branches", which you'll see in git branch -r output.)

Branch names are, however, sorted by git branch --list (after sorting by "kind", i.e., grouping all local branches together first, then all remote branches). This means you can have the names grouped together: just create the names as develop/anotherFeature and develop/newFeature originally. The slash is, in this case, merely a part of the name.

The problem here is that git originally implemented1 these branch names by putting them in directories containing files. On the systems that support git, you can't have a directory named develop and a file named develop at the same time.2 So if you have a branch named develop, git may have created a file (.git/refs/heads/develop, specifically), which then prevents it from creating a directory (.git/refs/heads/develop, again) containing the file (newFeature) containing the SHA-1 of the commit that the branch currently identifies.


1While git now also uses a flat file (.git/packed-refs) to store branch-to-SHA-1 mappings, it still also uses directories of files, and must make sure that you don't create a name that must serve as both directory and file.

2Personally I think it makes sense for a file system name entity to work as both directory and file: this would be a way to, for instance, store all the ELF sections of an executable as files within the directory that is the executable program, or handle the things that MacOS does for app bundles. But this violates all kinds of rules for the way things must work in POSIX, so it would require a major redesign of the file system name space(s), and would be more suitable as a follow-on to Plan 9 (for instance) than as a Unix-ish variant.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!