views:

2007

answers:

5

How can I convert a 'normal' Git repository to a bare one?

The main difference seems to be:

  • in the normal git repository you have a .git folder inside the repository containing all relevant data and all other files build your working copy

  • in a bar Git repository, there is no working copy and the folder (let's call it repo.git) contains the actual repository data

So, is it sufficient to do something like this?

cd repo
mv .git .. && rm -fr *
mv ../.git .
mv .git/* .
rmdir .git
cd ..; mv repo repo.git # renaming just for clarity

Or do I need to adapt any of Git's files that are/were in the .git folder?

+12  A: 

Your method looks like it would work; the file structure of a bare repository is just what is inside the .git directory. But I don't know if any of the files are actually changed, so if that fails, you can just do

git clone --bare /path/to/repo

You'll probably need to do it in a different directory to avoid a name conflict, and then you can just move it back to where you want. And you may need to change the config file to point to wherever your origin repo is.

jonescb
Wrong, this method is not the equivalent. Doing a clone doesn't preserve config options, which can be critical to proper operation such as if you use git-p4. Additionally, a clone destroys remotes, again with something like git-p4 you lose the p4/master branch when you clone, so the above approach is preferable.
nosatalian
But you can transfer config options easily by copying the respective parts of the config file. I'd still consider this method to be cleaner than copying and renaming files manually.
Philipp
I think the git people agree with jonescb and Philipp.
Daniel Yankowsky
+10  A: 

As far as I can see, you are only missing one last step, before you cd .. out of the directory:

git config --bool core.bare true
Jörg W Mittag
Thanks for the hint at `core.bare`. Now, after googling this option, I can confirm it: http://www.kernel.org/pub/software/scm/git-core/docs/git-config.html
Boldewyn
A: 

Oneliner for doing all of the above operations:

for i in `ls -A .`; do if [ $i != ".git" ]; then rm -rf $i; fi; done; mv .git/* .; rm -rf .git; git config --bool core.bare true

(don't blame me if something blows up and you didn't have backups :P)

Tarmo
+1  A: 

I think the following link would be helpful

GitFaq: How do I make existing non-bare repository bare?

xhh
Yes, that's quite what I searched, thanks! However, their second proposal (`git clone`) has the drawbacks that nosatalian mentioned above.
Boldewyn
A: 

i've read the answers and i have done this:

cd repos
mv .git repos.git
cd repos.git
git config --bool core.bare true # from another answer
cd ../
mv repos.git ../
cd ../
rm -rf repos/ # or delete using a file manager if you like

this will leave the contents of repos/.git as the bare repos.git

Dan D