Git bare repository

Written by Rashmi Yadav on

Bare and Non bare repository

Git is a very powerful version control system to maintain our code. We can share and keep history of our code so effectively.

We have a remote repository which is actually called bare repository and the local one called non bare repository. So Git bare repository is a shared repository where we push our code. It does not have files and directories like non bare repository and not even .git so its not for development or to work on.

And also you cannot run git status/pull/push/fetch commands in bare repository.

A remote repository should always be a bare repository.

You can clone your remote repository with bare option

git clone --bare [your remote repositry url]

Here you will see that there are no files and directories as we have in our development or working repository.

Lets try it out on your local system.

Create a Git bare repository

mkdir test_bare
cd test_bare_repo
git init --bare

When we do ls -lart you will see something like

bare repo

Now create a non bare repository and add bare repository as origin

mkdir test_non_bare
cd test_non_bare
git init
git remote add origin /Users/raysrashmi/crap/test_bare_repo [path of bare repo created above]

Or you can clone bare repository

git clone /Users/raysrashmi/crap/test_bare_repo test_non_bare

Now add README file here

touch README
git push origin master

As you see we can write code here and push to remote repository which is bare.

Now clone your development repository 'test_non_bare'

mkdir git clone /Users/raysrashmi/crap/test_non_bare test_clone
cd test_clone

If we update README file, commit and then try to push we can't because we are pushing to non bare repository.

Migrating a Git repository

Sometime we require to migrate our Git repository to another location. we have to use bare repository

Run following commands for migrating repository

git clone --bare git@myhost.com:username/old-repo.git
cd old-repo.git
git push --mirror git@myhost.com:username/new-repo.git

Mirroring from bare repository will keep all you history every brach and tag of you remote repository. and will not push any local branch or changes to new repository