How to create a branch in a bare repository in Git

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

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

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.

Tags:

Git