Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a sub branch?

Tags:

git

branch

My git repository has two branches, master and bug. I want to create a branch bug/rep. So I run :

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'.

How can I create a sub branch from the bug branch, like bug/rep? My git version is 2.3.2 on the Mac.

like image 866
jiexishede Avatar asked Nov 11 '15 14:11

jiexishede


People also ask

How do I create a sub branch on GitHub desktop?

Click on Create Repository. As the repository is created, you can also create a branch before you publish/push the changes to GitHub. Select New branch from the Branch menu. Call it to feature and click on Create branch.


2 Answers

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.

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 already a 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.

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.

like image 149
Daniel Kessler Avatar answered Sep 20 '22 12:09

Daniel Kessler


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


here origin is the master branch, added with

git add remote origin master https://github.com/<ur-name>/repoName.git

checkout the name of branches

git branch
like image 23
Rkmr039 Avatar answered Sep 19 '22 12:09

Rkmr039