在 Git 分支名称中使用斜杠字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2527355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using the slash character in Git branch name
提问by VonC
I'm pretty sure I saw somewhere in a popular Git project the branches had a pattern like "feature/xyz".
我很确定我在一个流行的 Git 项目中的某个地方看到分支有一个像“feature/xyz”这样的模式。
However when I try to create a branch with the slash character, I get an error:
但是,当我尝试使用斜杠字符创建分支时,出现错误:
$ git branch labs/feature
error: unable to resolve reference refs/heads/labs/feature: Not a directory
fatal: Failed to lock ref for update: Not a directory
Same problem for (my initial attempt):
同样的问题(我最初的尝试):
$ git checkout -b labs/feature
How does one create a branch in Git with the slash character?
如何在 Git 中创建一个带有斜杠字符的分支?
回答by VonC
Are you sure branch labs
does not already exist (as in this thread)?
你确定分支labs
不存在(如在这个线程中)?
You can't have both a file, and a directory with the same name.
You're trying to get git to do basically this:
% cd .git/refs/heads % ls -l total 0 -rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 labs -rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 master % mkdir labs mkdir: cannot create directory 'labs': File exists
You're getting the equivalent of the "cannot create directory" error.
When you have a branch with slashes in it, it gets stored as a directory hierarchy under.git/refs/heads
.
您不能同时拥有同名的文件和目录。
你试图让 git 基本上做到这一点:
% cd .git/refs/heads % ls -l total 0 -rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 labs -rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 master % mkdir labs mkdir: cannot create directory 'labs': File exists
你得到相当于“无法创建目录”的错误。
当您有一个带有斜杠的分支时,它会作为目录层次结构存储在.git/refs/heads
.
回答by Jakub Nar?bski
It is possible to have hierarchical branch names (branch names with slash). For example in my repository I have such branch(es). One caveat is that you can't have both branch 'foo' and branch 'foo/bar' in repository.
可以有分层分支名称(带斜杠的分支名称)。例如,在我的存储库中,我有这样的分支。一个警告是,您不能在存储库中同时拥有分支“foo”和分支“foo/bar”。
Your problem is not with creating branch with slash in name.
您的问题不在于创建名称中带有斜杠的分支。
$ git branch foo/bar error: unable to resolve reference refs/heads/labs/feature: Not a directory fatal: Failed to lock ref for update: Not a directory
The above error message talks about 'labs/feature' branch, not 'foo/bar' (unless it is a mistake in copy'n'paste, i.e you edited parts of session). What is the result of git branch
or git rev-parse --symbolic-full-name HEAD
?
上面的错误消息谈论的是“labs/feature”分支,而不是“foo/bar”(除非它是copy'n'paste 中的错误,即您编辑了部分会话)。git branch
或的结果是git rev-parse --symbolic-full-name HEAD
什么?
回答by elbkind
Sometimes that problem occurs if you already have a branch with the base name.
如果您已经有一个带有基本名称的分支,有时会出现该问题。
I tried this:
我试过这个:
git checkout -b features/aName origin/features/aName
Unfortunately, I already had a branch named features
, and I got the exception of the question asker.
不幸的是,我已经有一个名为 的分支features
,我得到了提问者的例外。
Removing the branch features
resolved the problem, the above command worked.
删除分支features
解决了问题,上述命令有效。
回答by flori
In my case, I forgot that there was already an unused labs
branch. Deleting it solved the problem:
就我而言,我忘记了已经有一个未使用的labs
分支。删除它解决了问题:
git branch -d labs
git checkout -b labs/feature
Explanation:
解释:
Each namecan only be a parent branch ora normal branch, not both. Thats why the branches labs
andlabs/feature
can't exists both at the same time.
每个名称只能是父分支或普通分支,不能同时是两者。这就是为什么分支labs
和labs/feature
不能同时存在的原因。
The reason for this behaviour is that the branches are stored in the file system and there you also can't have a file labs
and a directory labs
at the same level.
这种行为的原因是分支存储在文件系统中,在那里你也不能在同一级别拥有文件labs
和目录labs
。
回答by crazyscot
I could be wrong, but I thought that slashes only appeared in branch names when they related to a remote repo, for example origin/master
.
我可能是错的,但我认为斜线只出现在分支名称与远程存储库相关时,例如origin/master
.