在 Git 分支名称中使用斜杠字符
我很确定我在一个流行的 Git 项目中的某个地方看到分支有类似“feature/xyz”的模式。
但是,当我尝试使用斜杠字符创建分支时,出现错误:(
$ 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
我的初次尝试)同样的问题:
$ git checkout -b labs/feature
How does one create abranch in Git with the斜杠字符?
I'm pretty sure I saw somewhere in a popular Git project the branches had a pattern like "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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您确定分支
labs
尚不存在吗(如 这个线程)?请注意,
labs
不能是现有分支,如 ddruganov在评论中指出:如上所述在“
git Push:refs/heads/my/subbranch
存在,无法创建”中:Are you sure branch
labs
does not already exist (as in this thread)?Note that
labs
must not be an existing branch, as ddruganov points out in the comments:As explained in "
git push: refs/heads/my/subbranch
exists, cannot create":可以有分层分支名称(带斜杠的分支名称)。例如,在我的存储库中,我有这样的分支。需要注意的是,存储库中不能同时拥有分支“foo”和分支“foo/bar”。
您的问题不在于创建名称中带有斜杠的分支。
上面的错误消息谈论“labs/feature”分支,而不是“foo/bar”(除非复制粘贴时出现错误,即您编辑了会话的部分内容)。
git Branch
或git rev-parse --symbolic-full-name HEAD
的结果是什么?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.
Your problem is not with creating branch with slash in name.
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
orgit rev-parse --symbolic-full-name HEAD
?我忘记了我已经有一个未使用的 Labs 分支。删除它解决了我的问题:
解释:
每个名称只能是父分支或普通分支,不能同时是两者。这就是为什么分支
labs
和labs/feature
不能同时存在。原因:分支存储在文件系统中,并且文件
labs
和目录labs
不能处于同一级别。I forgot that I had already an unused
labs
branch. Deleting it solved my problem:Explanation:
Each name can only be a parent branch or a normal branch, not both. Thats why the branches
labs
andlabs/feature
can't exists both at the same time.The reason: Branches are stored in the file system and there you also can't have a file
labs
and a directorylabs
at the same level.有时,如果您已经有一个具有基本名称的分支,就会出现此问题。
我尝试了这个:
不幸的是,我已经有一个名为
features
的分支,并且我得到了提问者的例外。删除分支
features
解决了问题,上述命令有效。Sometimes that problem occurs if you already have a branch with the base name.
I tried this:
Unfortunately, I already had a branch named
features
, and I got the exception of the question asker.Removing the branch
features
resolved the problem, the above command worked.分支名称区分大小写,因此如果发布经理在 Azure 中创建了文件夹,则文件夹名称应完全匹配。很难学会向经理提出这个问题。
Branch name is case sensitive and so if there are folders created in Azure by your release manager, folder names should match exactly. Learned it hard way raising it with manager.
如果有人遇到这个问题,他们无法签出分支,例如:
features/23
,因为它说分支features
存在,但您无法在Github - 可能是您有一个名为features
的本地分支导致的。使用 git Branch -D features 删除本地分支,然后您应该能够使用 git checkout features/23 签出,并且它应该按预期工作。
In case someone has this issue where they're not able to checkout to a branch for eg:
features/23
because it says branchfeatures
exists, but you cannot find it on Github - it might be that you have a local branch namedfeatures
which is causing it.Delete the local branch with
git branch -D features
and then you should be able to checkout withgit checkout features/23
and it should work as expected.只是遇到了同样的问题,但我找不到冲突的分支了。
就我而言,回购协议之前有“foo”分支,但现在不再有,我尝试从远程创建和签出“foo/bar”。
正如我所说,“foo”不再存在,但问题仍然存在。
最后,分支“foo”仍然在.git/config文件中,删除后一切正常:)
just had the same issue, but i could not find the conflicting branch anymore.
in my case the repo had and "foo" branch before, but not anymore and i tried to create and checkout "foo/bar" from remote.
As i said "foo" did not exist anymore, but the issue persisted.
In the end, the branch "foo" was still in the .git/config file, after deleting it everything was alright :)
我从 Visual Studio Git Changes 中执行此操作,
如前所述。我犯了创建开发分支的错误。
主要的
dev
认为以后我可以称它们为 dev/somebranchname。
解决方案是删除 dev,然后创建 dev/somebranchname。下一个开发分支将被称为 dev/nextbranchname。
这与 Visual Studio 集成得很好,其中 dev 成为文件夹视图,然后分支排列起来
I do this from Visual Studio Git Changes
As previously mentioned. I made the misstake of creating a dev branch.
main
dev
thinking that later I could just call them dev/somebranchname.
Solution was to delete dev and then create the dev/somebranchname. Next dev branch will be called dev/nextbranchname.
This integrates very well with Visual Studio where dev becomes a folder view and the branches line up after that