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?
An empty repository cannot have a branch, branches are pointers to commits. So you first have to commit something in the empty repository before the branch can be created. You can either commit the code from the other repository, or just an empty file, create your branch and then commit the code.
In your Github fork, you need to keep your master branch clean, by clean I mean without any changes, like that you can create at any time a branch from your master. Each time that you want to commit a bug or a feature, you need to create a branch for it, which will be a copy of your master branch.
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'...
We must create something in the directory, then the master branch could appear, why this limitation? It's unbelievable designing logic. 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.
The git checkout command accepts a -b argument that acts as a convenience method that will create the new branch and immediately switch to it. above command creates a new_feature_branch from the master and immediately switches to it. Was this post helpful? Let us know if you liked the post.
To create a bare repository, navigate to the chosen directory in bash (for linux users) or command prompt (for windows users) and type: The file structure of the bare repository should look like this: Note: This is the exact same file structure of .git folder in non-bare repository
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With