git 如何创建子分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33652953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a sub branch?
提问by jiexishede
My git repository has two branches, master
and bug
. I want to create a branch bug/rep
. So I run :
我的 git 存储库有两个分支,master
和bug
. 我想创建一个分支bug/rep
。所以我运行:
git branch bug/rep
This gives the error:
这给出了错误:
error: unable to resolve reference refs/heads/bug/rep: Not a directory fatal: Cannot lock the ref 'refs/heads/bug/rep'.
错误:无法解析引用 refs/heads/bug/rep:不是目录致命:无法锁定引用“refs/heads/bug/rep”。
How can I create a sub branch from the bug
branch, like bug/rep
?
My git version is 2.3.2 on the Mac.
如何从bug
分支创建子分支,例如bug/rep
?我的 git 版本在 Mac 上是 2.3.2。
回答by Daniel Kessler
In git, branches correspond to actual files in a hierarchy in the .git
subdirectory. If you create a branch named bug/sub
, git will first create a folder .git/refs/heads/bug
(if it doesn't already exist) and within that folder it will create a file named sub
which is the branch. Branch names are presumed to resolve within .git/refs/heads
, so refs/heads
+ bug/sub
resolves to a real place in the file system.
在 git 中,分支对应于.git
子目录中层次结构中的实际文件。如果您创建一个名为 的分支bug/sub
,git 将首先创建一个文件夹.git/refs/heads/bug
(如果它尚不存在),然后在该文件夹中创建一个名为sub
该分支的文件。假定分支名称在 内解析.git/refs/heads
,因此refs/heads
+bug/sub
解析到文件系统中的真实位置。
The trouble that you're encountering comes from your file system. Since you already have a branch called bug
, in .git/refs/heads
there is alreadya file named bug
. Creating the sub branch as outlined above would require the file system to create a new folder named bug
, but it can't because there is already a file there named bug
.
您遇到的问题来自您的文件系统。由于您已经有一个名为 的分支bug
,.git/refs/heads
因此其中已经有一个名为bug
. 如上所述创建子分支需要文件系统创建一个名为 的新文件夹bug
,但它不能,因为那里已经有一个名为bug
.
In brief, you're free to create a hierarchy of branches, but higher-level nodes of your hierarchy can't be branches in their own right.
简而言之,您可以自由创建分支层次结构,但是层次结构的更高级别节点本身不能成为分支。
回答by Rkmr039
for creating a new sub branch git checkout -b <sub-branch-name>
// sub branch will automatically created at the time of push
for pushing file on sub branch created earlier git push -u origin <sub-branch-name>
-u for setting upstream parameter
用于创建新的子分支git checkout -b <sub-branch-name>
// 子分支会在推送时自动创建
用于推送先前创建的子分支上的文件git push -u origin <sub-branch-name>
-u 用于设置上游参数
here origin is the master branch, added with
这里 origin 是 master 分支,添加了
git add remote origin master https://github.com/<ur-name>/repoName.git
checkout the name of branches
签出分支名称
git branch