teaching machines

Hosting a Git Repository

March 4, 2017 by . Filed under git, howto, public.

When I first started learning Git, I was unimpressed by the notion of distributed version control. I felt and still feel that a central server hosting a project is important for the projects I work on, and I think the popularity of GitHub, Bitbucket, and Gitlab confirms my belief.

With these great services around acting as central servers, I’ve not had to host my own Git project. Then I started tracking student grades in my course reposities. This is not the kind of information I can give to a third-party service like GitHub, so it was time to learn how to host my own project. I’ve jotted my notes down here.

On the central server, we create an empty project directory and initialize it as a bare repository:

mkdir myrepo.git
cd myrepo.git
git init --bare

A bare repository differs from a non-bare repository in that it doesn’t give you a working directory in which to make edits. Its sole purpose is to serve out files, not edit them. It’s my understanding that making the central repository bare eliminates confusion. If changes are pushed from a client up to a non-bare central repository (one with a working directory), the working directory and repository passively get out of sync. You haven’t actively pulled any changes, but they were silently pushed to you.

On the client, if we don’t already have a project directory, we can clone the bare repository:

git clone host:path/to/myrepo

Suppose we already have a project that we initialized as a Git repository:

cd project
git init

In this case, we don’t need to clone anything, but we do need to set the repository’s remote to be our bare repository:

git remote add origin host:path/to/myrepo