52

(All done in poshgit on Windows 8):

git init --bare test-repo.git
cd test-repo.git

(Folder is created with git-ish files and folders inside)

git status

fatal: This operation must be run in a work tree (Okay, so I can't use git status with a bare repo; makes sense I guess)

git branch

(Nothing, it seems the bare repo doesn't contain any branches. Do I have to add them from a cloned repo?)

cd ..
mkdir test-clone
cd test-clone
git clone ../test-repo.git

(I get a warning about cloning an empty repository)

cd test-repo

(The prompt changes to indicate I am on the master branch)

git branch

(Shows no results - eh?)

git branch master

fatal: Not a valid object name: 'master'

Um. So how do I create the master branch in my bare repo?

jub0bs
  • 46,795
  • 22
  • 148
  • 157
David
  • 14,678
  • 20
  • 80
  • 145

4 Answers4

74

A bare repository is pretty much something you only push to and fetch from. You cannot do much directly "in it": you cannot check stuff out, create references (branches, tags), run git status, etc.

If you want to create a new branch in a bare Git repository, you can push a branch from a clone to your bare repo:

# initialize your bare repo
$ git init --bare test-repo.git

# clone it and cd to the clone's root directory
$ git clone test-repo.git/ test-clone
Cloning into 'test-clone'...
warning: You appear to have cloned an empty repository.
done.
$ cd test-clone

# make an initial commit in the clone
$ touch README.md
$ git add . 
$ git commit -m "add README"
[master (root-commit) 65aab0e] add README
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

# push to origin (i.e. your bare repo)
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /Users/jubobs/test-repo.git/
 * [new branch]      master -> master
jub0bs
  • 46,795
  • 22
  • 148
  • 157
  • Didn't work for me. test-clone>git commit -m "add README" On branch master Initial commit Untracked files: README.md nothing added to commit but untracked files present – Lawrence DeSouza Dec 18 '19 at 02:20
  • 1
    @LawrenceDeSouza You most likely forgot to `git add .`. – jub0bs Dec 18 '19 at 09:54
22

A branch is just a reference to a commit. Until you commit anything to the repository, you don't have any branches. You can see this in a non-bare repository as well.

$ mkdir repo
$ cd repo
$ git init
Initialized empty Git repository in /home/me/repo/.git/
$ git branch
$ touch foo
$ git add foo
$ git commit -m "new file"
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 foo
$ git branch
* master
chepner
  • 389,128
  • 51
  • 403
  • 529
12

You don't need to use a second repository - you can do commands like git checkout and git commit on a bare repository, if only you supply a dummy work directory using the --work-tree option.

Prepare a dummy directory:

$ rm -rf /tmp/empty_directory
$ mkdir  /tmp/empty_directory

Create the master branch without a parent (works even on a completely empty repo):

$ cd your-bare-repository.git

$ git checkout --work-tree=/tmp/empty_directory --orphan master
Switched to a new branch 'master'                  <--- abort if "master" already exists

Create a commit (it can be a message-only, without adding any files, because what you need is simply having at least one commit):

$ git commit -m "Initial commit" --allow-empty --work-tree=/tmp/empty_directory 

$ git branch
* master

Clean up the directory, it is still empty.

$ rmdir  /tmp/empty_directory

Tested on git 1.9.1. (Specifically for OP, the posh-git is just a PowerShell wrapper for standard git.)

kubanczyk
  • 3,448
  • 30
  • 45
-1

By default there will be no branches listed and pops up only after some file is placed. You don't have to worry much about it. Just run all your commands like creating folder structures, adding/deleting files, commiting files, pushing it to server or creating branches. It works seamlessly without any issue.

https://git-scm.com/docs

Shivraaz
  • 209
  • 2
  • 3
  • 1
    A *bare repository* doesn't work that way. https://git-scm.com/docs/git-clone#git-clone---bare – jub0bs Aug 13 '18 at 11:27