Thursday, April 8, 2010

Setting up GIT for home network.

I have a few different computers that I use at home and I wanted to set up GIT that I can access my code from any of them. It took me a bit because I was too used to working with a client-server model where I designate one machine as the "server" that holds the repository and everything else was a client. Instead, GIT seems to operate more like a merge tool and every local copy is its own "master." Once I understood that, it turns out that setting up GIT is very simple and just needs GIT itself and SSH.

Let's start with 2 machines. The first one, "remote", is where you want to store the code and could be where other users will also pull copy of the code from. The second one, "local", is the machine where you want to check out the code and make your edits.

On "remote" we want to store the repository in /usr/local/src:

cd /usr/local/src
git --bare init

On "local" we want to work on the code in ~/workspace and we already have some files to populate the repository with so we first create a local git repository:

cd ~/workspace
git init
git add .
git status
git commit

Now let's push this code to the remote server:

git push ssh://@/usr/local/src

Done! Now we have both servers with the files. Files can be edited locally, commit locally and periodically synced/pushed to the remote server.

Now, on another computer, we want to "check out" the source code to work on it:

mkdir ~/workspace
cd ~/workspace
git clone ssh://@/usr/local/src

Some things to make it easier to work with:

1. Use ssh keys so that you don't have to put in your password each time you push to the remote host.