在 Git 分支名称中使用斜杠字符

发布于 2024-08-26 08:41:53 字数 408 浏览 3 评论 0原文

我很确定我在一个流行的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夜深人未静 2024-09-02 08:41:53

您确定分支 labs 尚不存在吗(如 这个线程)?

不能同时拥有同名的文件和目录。

你试图让 git 基本上做到这一点:

% cd .git/refs/heads
% ls-l
总计 0
-rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 实验室
-rw-rw-r-- 1 jhe jhe 41 2009-11-14 23:51 大师
% mkdir 实验室
mkdir:无法创建目录“labs”:文件存在

您收到相当于“无法创建目录”的错误。
当你有一个带有斜杠的分支时,它会被存储为
.git/refs/heads 下的目录层次结构。


请注意,labs 不能是现有分支,如 ddruganov评论中指出

 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存在,无法创建”中:

  • 如果分支 b 存在,则不能创建名为 b/anything 的分支。
  • 同样,如果分支 dev/b 存在,则无法创建 dev/b/c。

这是 git 内部限制。

Are you sure branch labs does not already exist (as in 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.


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'

As explained in "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.

北城半夏 2024-09-02 08:41:53

可以有分层分支名称(带斜杠的分支名称)。例如,在我的存储库中,我有这样的分支。需要注意的是,存储库中不能同时拥有分支“foo”和分支“foo/bar”。

您的问题不在于创建名称中带有斜杠的分支。

$ 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

上面的错误消息谈论“labs/feature”分支,而不是“foo/bar”(除非复制粘贴时出现错误,即您编辑了会话的部分内容)。 git Branchgit 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.

$ 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?

尤怨 2024-09-02 08:41:53

我忘记了我已经有一个未使用的 Labs 分支。删除它解决了我的问题:

git branch -d labs
git checkout -b labs/feature

解释:

每个名称只能是父分支普通分支,不能同时是两者。这就是为什么分支 labs labs/feature 不能同时存在。

原因:分支存储在文件系统中,并且文件 labs 和目录 labs 不能处于同一级别。

I forgot that I had already an unused labs branch. Deleting it solved my problem:

git branch -d labs
git checkout -b labs/feature

Explanation:

Each name can only be a parent branch or a normal branch, not both. Thats why the branches labs and labs/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 directory labs at the same level.

伏妖词 2024-09-02 08:41:53

有时,如果您已经有一个具有基本名称的分支,就会出现此问题。

我尝试了这个:

git checkout -b features/aName origin/features/aName

不幸的是,我已经有一个名为 features 的分支,并且我得到了提问者的例外。

删除分支 features 解决了问题,上述命令有效。

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.

Removing the branch features resolved the problem, the above command worked.

心病无药医 2024-09-02 08:41:53

分支名称区分大小写,因此如果发布经理在 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.

九八野马 2024-09-02 08:41:53

如果有人遇到这个问题,他们无法签出分支,例如: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 branch features exists, but you cannot find it on Github - it might be that you have a local branch named features which is causing it.

Delete the local branch with git branch -D features and then you should be able to checkout with git checkout features/23 and it should work as expected.

听风念你 2024-09-02 08:41:53

只是遇到了同样的问题,但我找不到冲突的分支了。

就我而言,回购协议之前有“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 :)

千紇 2024-09-02 08:41:53

我从 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文