CommonThread

Setting Up a Git Server

Posted by ben, Sun Apr 13 21:07:00 UTC 2008

Setting up a remote git server is as easy as setting up a new user. In fact if you have git installed on the machine that is about all you need to do. We will not discuss setting up git … just how to set it up to be used as a remote repository.

Setup git User

First we need to setup a user with a home folder. We will store all the repositories in this users home folder.

sudo adduser git

Rather than giving out the password to the git user account use ssh keys to login so that you can have multiple developers connect securely and easily.

Create a Repository

Next we will make a repository. For this example we will work with a repository called example. Login as the user git and add the repository.

# login to remote server
ssh git@REMOTE_SERVER

# once logged in
mkdir example.git
cd example.git
git --bare init

That’s all there is to creating a repository. Notice we named our folder with a .git extension.

Also notice the ‘bare’ option. By default the git repository assumes that you’ll be using it as your working directory, so git stores the actual bare repository files in a .git directory alongside all the project files. Since we are setting up a remote server we don’t need copies of the files on the filesystem. Instead, all we need are the deltas and binary objects of the repository. By setting ‘bare’ we tell git not to store the current files of the repository only the diffs. This is optional as you may have need to be able to browse the files on your remote server.

Commit to Remote Repository

Finally all you need to do is add your files to the remote repository. We will assume you don’t have any files yet.

mkdir example
cd example
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@REMOTE_SERVER:example.git
git push origin master

replace REMOTE_SERVER with your server name or IP

Filed Under: | Tags:

Comments

  1. RichardBronosky 05.05.08 / 14PM
    This is awesome. Setting up a git server was something that I thought was going to require a ton of emails here at my company. This is so simple that I can do this on one of our sandboxes without even bothering any of the admins. Nice!
  2. Arne Babenhauserheide 06.11.08 / 19PM
    I also thought it much harder, and I'm glad I was proven wrong. You can do it the same way with Mercurial by just replacing "git" with "hg" :) Still it's a bit behind the extreme ease with which I can serve a repository via Mercurial, if I only want to provide my changes to others without much overhead: $ hg serve Serves my repository from my local machine at port 8000. $ hg serve -p PORT -n NAME uses a different port and gives the repository a name (It's only a subset of the options, but the most useful, I think :) ).
  3. thorny_sun 06.27.08 / 19PM
    you should add a '--bare' to 'git init', when you set up the repo on the server
  4. Ben 06.29.08 / 01AM
    You could add that and it would save you a little bit of disk space. I can't find it in the man page but I have read explanations online of how --bare works. I will update the post.

Have your say

A name is required. You may use HTML in your comments.




Recent Articles

Categories