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

标签 git branch

我很确定我在一个流行的 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 不能是现有分支,如ddruganovthe 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/

相关文章:

git - 为什么 Git 引用没有文件扩展名

git - 在不克隆的情况下比较两个 Git 分支

mercurial - 在 Mercurial 中切换分支时如何防止文件自动 merge ?

初始提交的 GitHubs 流程

linux - 使用 Git 存储密码有多安全?

git - Git 中的 HEAD、工作树和索引有什么区别?

git - 从远程服务器 pull 某个分支

node.js - 从非 Master 分支拉取 NPM 私有(private)模块

git - 为什么 merge 删除后Git特性分支仍然可见?

python - .gitignore 符号中的 "py[cod]"和 "pyc"有什么区别?