Gerry Eng
Gerry Eng

Reputation: 758

How to create a branch in a bare repository in Git

I current have a bare repo thats acts as a central repo for my team. The bare repo currently only have a branch "master". How can I create more branches on the bare repo?

Upvotes: 30

Views: 26477

Answers (3)

arcyqwerty
arcyqwerty

Reputation: 10695

To create a new branch (locally) called branchname

git branch branchname

Then to sync it with the remote repository like GitHub (if applicable)

git push origin branchname

And to use it for development / make the branch the active branch

git checkout branchname

Upvotes: 5

Dmitry Egorov
Dmitry Egorov

Reputation: 9650

git update-ref refs/heads/new_branch refs/heads/master

In that bare repository if you have direct access to it. You may supply any reference (a tag for instance) or a commit in the last argument.

Below is a test script:

$ mkdir non-bare-orig

$ cd non-bare-orig/

$ git init
Initialized empty Git repository in D:/Temp/bare-branch/non-bare-orig/.git/

$ touch file1

$ git add --all && git commit -m"Initial commit"
[master (root-commit) 9c33a5a] Initial commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file1

$ touch file2

$ git add --all && git commit -m"Second commit"
[master 1f5673a] Second commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file2

$ git tag some_tag

$ touch file3

$ git add --all && git commit -m"Third commit"
[master 5bed6e7] Third commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file3

$ cd ../

$ git clone --bare non-bare-orig bare-clone
Cloning into bare repository 'bare-clone'...
done.

$ cd bare-clone/

$ git update-ref refs/heads/branch1 refs/heads/master

$ git update-ref refs/heads/branch2 some_tag

$ git update-ref refs/heads/branch3 9c33a5a

$ git branch -vv
  branch1 5bed6e7 Third commit
  branch2 1f5673a Second commit
  branch3 9c33a5a Initial commit
* master  5bed6e7 Third commit

Upvotes: 15

KingCrunch
KingCrunch

Reputation: 132071

Usually you don't create branches directly in the bare repository, but you push branches from one work repository to the bare

git push origin myBranch

Update: Worth to mention

Like Paul Pladijs mentioned in the comments with

git push origin localBranchName:remoteBranchName

you push (and create, if not exists) your local branch to the remote with a different branch name, that your local one. And to make it complete with

git push origin :remoteBranchName

you delete a remote branch.

Upvotes: 23

Related Questions