1

Pro Git book states that I need to create repo inside existing directory first and then clone it as 'bare'. I've read this article about why use 'bare' repository and it seems that I need to treat it as 'origin' and it should be used to pull from and push to. What I don't understand is that why I need to create regural repository first and then clone it as 'bare'? Why can't I create 'bare' repo intiatilly and then commit files from directory there?

And what if I remove the directory from which 'bare' repo was created? Will it affect somehow 'bare' repository?

0

3 Answers 3

2

You can create a bare respository easily enough:

$ git init --bare <directory>

And if you cloned it, you can delete the original repository once cloned. The origin remote in the cloned repository will be unreachable, so you will probably want to remove it afterwards.

Not that remotes are very useful in bare repositories anyway...

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! I've figured out that the repository you clone from becomes 'origin' in git remote list :)
And if you cloned it, you can delete the original repository once cloned. - I guess this could be a good addition to first option listed in the post by @Nevik Rehnel
You can, but you don't have to, @Maximus. You could just use the initial repo as a normal working clone.
Some bare repos have useful remotes, when they are being used as read-only caches. For instance, $LargeCompany might have the "real" repo at $HQ but have read-only copies at $SiteB, $SiteC, etc., with the copies updated frequently (perhaps even automatically on pushes to $HQ) and individual engineers at $SiteB cloning from the $SiteB copy, to avoid a lot of traffic having to flow over the pipe to $HQ.
Thanks guys for your eleboration!
2

This might be a slight misunderstanding of the starting situation.

You can create a bare repo like any other, by using git init --bare. This will create the repository without any contents.

However, without having a clone to communicate with, you have no way of getting files into it, because bare repos have no working directory.

If you want to put your existing files into a bare repo, you have two ways of going about that:

  1. Create a normal repo and commit your files to it, then clone it into a bare repo (as described in your question)
  2. Create a bare repo, clone it to a normal repo, commit your files to the normal repo, then push to the bare repo.

Leaving out the adding of a remote to the original non-bare in variant 1., you can see that variant 2. has more steps.

3 Comments

Ok, thanks! I guess this is a misunderstanding :). I guess I'm still lost as to why I need bare repo. Is there any good article on the web or question on stackoverflow that explains it?
Thanks! I guess now I've figured it out. I don't need to start with normal repo if I'm going to use 'bare' repo only for push/pull operations and actual commits will be made on local non-bare repositories.
Or 3. Create a bare repo, go to a normal repo, add the bare as remote, and push from there. This is what happens when you make a new repo on github and follow their instructions to push an existing repo.
2

Why can't I create 'bare' repo intiatilly and then commit files from directory there?

Because you cannot commit files into a bare repo.

Normally, if you create a repository (using git init or git clone), git will create the repository structure (the .git subdir) and a working tree, that is a directory where the current commit's files are checked out.

A bare repo is simply a repo without an associated working tree. So there's no way to commit files into it, because there is no working tree to put files into for committing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.