Using Github to Manage Git Repositories (Part 1)

For a long time, I was content to manage git respoitories myself (you can see earlier articles in this blog to see how I accomplished that). More and more, however, I liked the idea of my remote repositories using a more client-server model. To that end, I created an account for myself on Github. If you want to have private repositories, there is a monthly cost.

New Repository

Creating a new repository is simple.

From your github dashboard, you create a new repository (click the "New Repository" button on the righthand panel). You are asked to complete a form:

  • Project Name: bnr
  • Description (optional): Big Nerd Ranch
  • Homepage URL: www.bignerdranch.com
  • Access: Only the people I specify (I want this to be a private repository)

After you complete this form and press the "Create Repository" button, you are presented with some helpful setup information, including the "identity" of your repository (e.g., git@github.com:fenoglma/bnr.git).

cd ~/Sites
mkdir bnr && cd bnr
git init
touch README
git add README
git commit -m "[create] initial repository contents"
git remote add origin git@github.com:fenoglma/bnr.git
git push -u origin master

And there you are. You have a brand new repository ready for more code!

Existing Repository

What if I already have a repository, though? (That is certainly true for Big Nerd Ranch, which has been an active project of mine for a couple years.) In that case, you change the current configuration of your git repository to point to the one on github.

cd ~/Sites/bnr
git remote add origin git@github.com:fenoglma/bnr.git
git push -u origin master

This is only the first step, of course. Before using github, I managed the repositories by adding remote entries to my configuration. I no longer need those. I want my local configuration (~/Sites/bnr/.git/config) to look like this now:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
[remote "origin"]
    url = git@github.com:fenoglma/bnr.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

The original config file would have had remote entries for "bignerdranch" and "bnrstaging". What I want to do next is update my git setup on those servers to use the github repository instead.

Remote Repositories

Previously, I set up git on both the staging and production servers for Big Nerd Ranch. What I can do now is just change the configuration file to use the github repository instead.

ssh fenoglma@www.bignerdranch.com
sudo su - www-data
cd bignerdranch.com
git remote add origin git@github.com:fenoglma/bnr.git
git pull

The last statement (git pull) will result in an error! Why? I have not yet let github know that this server can use the repository.

Deploy Key vs. SSH Public Key

Here's where things get a bit dicey. A deploy key is a way to have read-only access to exactly one repository. That would be the perfect solution, except for one detail: The framework that powers the Big Nerd Ranch web application is a separate repository. With an individual github account, the only solution is to add SSH Public Keys. First, I need to create that public key for the server where the repositories will be deployed.

ssh www.bignerdranch.com
sudo su - www-data
mkdir .ssh && cd .ssh
ssh-keygen -t dsa

Two files result: id_dsa (private key) and id_dsa.pub (public key). Back in github, go to Account Settings > SSH Public Keys and add the public key. Now, "git pull" works just fine.