在 Git 分支名称中使用斜杠字符 [英] Using the slash character in Git branch name
问题描述
我很确定我在一个流行的 Git 项目中的某个地方看到过分支有一个类似于feature/xyz"的模式.
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
如何在 Git 中创建带有斜杠字符的分支?
How does one create a branch in Git with the slash character?
推荐答案
Are you sure branch labs
不存在(如 这个线程)?
Are you sure branch labs
does not already exist (as in this thread)?
不能同时拥有同名的文件和目录.
你试图让 git 基本上做到这一点:
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
您将获得相当于无法创建目录"的信息错误.
当你有一个带有斜杠的分支时,它会被存储为.git/refs/heads
下的目录层次结构.
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 在评论中指出:
Note that labs
must not be an existing branch, as ddruganov points out in 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
中所述,存在,无法创建":
As explained in "git push: refs/heads/my/subbranch
exists, cannot create":
- 如果分支 b 存在,则不能创建名为 b/anything 的分支.
- 同样,如果分支 dev/b 存在,则无法创建 dev/b/c.
这是 git 内部限制.
This is a git internal limitation.
这篇关于在 Git 分支名称中使用斜杠字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!