Push your Git repositories to a central server, in a bare repository

GitHub is a great central repository silo for open source projects, and for private Git repositories for companies and organizations with enough cash to afford the features GitHub offers.

However, for many projects and developers, GitHub can be overkill. For my needs, I have many smaller private projects that I'd like to have hosted centrally, and backed up, but don't warrant BitBucket or GitHub accounts. Therefore, I've taken to creating bare repositories on one of my Linode servers, and pushing all my local branches and tags to these repos. That server is backed up nightly, so I know if I lose my local environment, as well as my Time Machine backup (a very unlikely occurrence, but still possible), I will have backed up and fully intact Git repos for all my projects.

I recommend you do something like the following (presuming you already have a Git repo on your local computer that you've been working with):

Step 1 - on your remote server (accessible via ssh)

# Create a 'repositories' directory in your user home directory.
$ mkdir ~/repositories && cd ~/repositories

# For each of your local repositories, make a directory ending in '.git'.
$ mkdir My-Project.git && cd My-Project.git

# Create a bare git repository inside this new directory.
$ git --bare init

Here's a good article outlining the why and how of pushing to bare repositories.

Step 2 - on your local computer

# Add the 'origin' to push to. This uses the relative path for the username you were
# logged in as when you created the bare repo earlier.
$ git remote add origin [username]@[host]:repositories/My-Project.git/

# Push your master branch to the remote repo.
$ git push origin master

# Push other tags and branches, tracking them locally.
$ git push -u origin 1.x
$ git push -u origin 2.x
etc...

Now, after you've made some commits locally, you can just use git push to push the changes up to the live server. If you're working on different branches or tags, be sure to push those up as well!

Since your remote server doesn't have a checked out/live branch in the repository (it's bare, so it only contains a .git folder), you can check to see the latest changes in the repository by cding into the repository and using git log master to see the latest commits on the master branch, or git tag to see a list of all the tags. Commands like git status don't work on a bare repository.

Comments

Thanks for posting this, Jeff. I have been looking for a succinct article about how to set up this sort of stuff and this fit the bill.

I liked the explanations next to the actual instructions. So often we see explanations with no instruction or instructions with no explanation. Thanks Jeff, Many could take a lesson from your style as well as your information.
One gripe. It is not clear how the word 'master' is being used in the title. I assume it has no reference to the master branch. One thing that many Git guides fail to do is explain how we use the same words to mean different things and exactly how to determine the context , which will tell me what is really meant. Master branch, master repository, how does the use of the same word help me understand? Do they have some characteristic in common? I was confused until I unwound my assumption that master referred to a branch and read the article a second time.

Seven years later, and this is still relevant. Was just reading up on bare repositories, and having some confusions with how to go about setting up and using one. This article cleared up my confusion. It's a huge enlightenment. Thank you for this.

Good to see this. Still relevant today for those of us stuck in the ssh world