我很确定我在一个流行的 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
如何在 Git 中创建带有斜杠字符的分支?
最佳答案
您确定分支 labs
不存在(如 this thread 中所示)吗?
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
.
请注意,labs
不能是现有分支,如ddruganov在 the comments 中指出:
git switch -c 19023-commerce/19033-commerce-view 19023-commerce
# Fails with:
fatal: cannot lock ref 'refs/heads/19073-commerce-view/99999-test-branch':
'refs/heads/19073-commerce-view' exists;
cannot create 'refs/heads/19073-commerce-view/99999-test-branch'
如“git push: refs/heads/my/subbranch
exists, cannot create”中所述:
- If branch b exists, no branch named b/anything can be created.
- Likewise, if branch dev/b exists, dev/b/c cannot be created.
This is a git internal limitation.
关于git - 在 Git 分支名称中使用斜杠字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2527355/