Doug Jaworski

Run You Own Git Server

I was originally introduced toGit will working with Puppet and Chef configurations. After having leveraged other CMS in the past such as CVS and SVN, I simply found Git easier and more enjoyable to use.

While I don’t code per se, I treat all my configurations as code and revision them as such. It ties in nice with working across multiple systems while enabling me to take my work with me even when it is not truly with me. While I still do have and and use a GitHub account, I often find myself in situations in which I don’t want to publish my content to GitHub. For this, I run my own Git Server. This HowTo is only to show you how to setup a Git Server. There are plenty of great tutorials already out there that help explain the details of Git.

These are just a few links to some great tutorials on Git.

Setup Git
Create a Repo
Fork a Repo
Git Cheat Sheet

The real secret sauce to Git in my opinion, is its use of SSH Keys for authentication. While you can use a password, it does not work well with multi-user setups. Key’s are easy to manage and revoke, allowing for a central authority of remote access.

1. Install Git

1
apt-get install git-core

If you are on MAC OS X and have xCode installed you already have the core functionality required to follow along.

2. Create a GIT User

If you are familiar with SSH Keys, this will seem rather simple however if you have not used SSH Keys for password less authentication, I recommend you read this for further understanding. The actual creating of a “Git” user is completly optionial as you can use your existing user. The purpose for creating a standalone user is that you can share this with multiple individuals.

1
adduser git

3. Create SSH Keys for the Git User

As the newly created git user, run the following commands.

1
2
3
mkdir ~/.ssh
chmod 0700 ~/.ssh
ssh-keygen -t rsa -b 4096

Accept all the defaults and hit enter for the passphrase. We want the passphrase to be blank. These commands setup your SSH directory for Git as well as installs a secure RSA key.

4. Create Client SSH Keys

On the client

If you do not already have an .ssh directory or any SSH keys, we will go ahead and set them up. If you do have keys, skip this step and go down to Step 5.

1
2
3
mkdir ~/.ssh
chmod 0700 ~/.ssh
ssh-keygen -t rsa -b 4096

You then have to get copy your id_rsa.pub contents to the user Git on the server to a file called ~/.ssh/authorized_keys. There are many ways to get this key on the server however for this tutorial, we will just use the good old copy/paste method.

5. Copy CLient SSH Keys to Server

On the client

1
cat ~/.ssh/id_rsa.pub

Copy to the clipboard

Server

1
2
3
touch ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
vi ~/.ssh/authorized_keys

Paste from clipboard and save.

From the client as the user who you want to grant access to.

6. Create Empty Git Repository

On the server (as the git user) run the following command.

1
git init --bare test.git

7. Clone Repository to Client

1
2
3
git clone git@localhost:test.git
Initialized empty Git repository in /home/doug/test/.git/
warning: You appear to have cloned an empty repository.

This created a directory called testing on your local client.

8. Setup Git Global Variables

Run the following commands.

1
2
git config --global user.name "Your Name"
git config --global user.email you@example.com

9. Commit Changes

1
2
3
4
5
cd ~/test
touch README
git add .
git commit –m “first commit”
git push –u origin master

If all is successful, you should have something like this.

1
2
3
4
5
6
7
git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 226 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@localhost:test.git
* [new branch] master @ master
Branch master set up to track remote branch master from origin.

Comments