CommonThread - Our Blog

Recent Articles

Setting Up a Git Server

Setting Up a Git Server

Written By: Ben Wyrosdick

April 13th, 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

27 Responses to “Setting Up a Git Server”

  1. RichardBronosky Says:
    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 Says:
    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 Says:
    you should add a '--bare' to 'git init', when you set up the repo on the server
  4. Ben Says:
    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.
  5. bobbo Says:
    Very, very helpful post. Thanks alot!
  6. Evanr Says:
    Excellent tutorial! Simple and to the point.
  7. Scott Says:
    Really nice. this is much easier then setting up gitosis.
  8. geekQ Says:
    @Arne: the counterpart to `hg server` in the git world is `git-daemon`. It starts a simple daemon on well known git port 9418 and serves the repository.
  9. Jason Broyles Says:
    Thanks for the post. This helped me out.
  10. Jim Says:
    To the point and easy. Thank you
  11. olifante Says:
    Great mini-guide! Before finding this page, I thought setting up and serving a remote Git repository was much more complicated. This really got me started using Git.
  12. Kaspar Schiess Says:
    Good guide - finally got over the --bare bit with your help!
  13. Kirill Maximov Says:
    Thanks, I thought it should be something like this and I'm glad I wasn't mistaken.
  14. gitn00b Says:
    Thanks a lot! That took all of 30 seconds. I would describe what "origin" means though.
  15. Michael Says:
    Excellent! Thanks, helped a lot!
  16. Dave Says:
    Great instructions! I was expecting this to be a big pain, but I got done quickly. Thanks.
  17. Bob Says:
    Then the question is just to get "git 4.x" to work just as smooth.. A lot of us are forced to use this old version because the dreambox repository does not compile on anything newer (debian platform).
  18. Louis Says:
    "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." this means everyone pushes changes under the 'git' user ? what if fred changes his .gitconfig file to set his name to david: will he be able to push under david and no one knows it ?! cheers
  19. Clay Shentrup Says:
    Very handy! Good man. :)
  20. Timo Says:
    Nice howto. However, if you need user management und secure datatransfer gitosis is the way to go. http://sunoano.name/ws/public_xhtml/scm.html#provide_a_git_repository_to_the_public
  21. Andrew B Says:
    What if you want to start keeping a remote copy of a repository that already exists on your local machine?
  22. Sridhar Says:
    Thanks for this Document! But When I am going to Push.. its showing error like this: "fatal: I don't handle protocol 'git@192.168.1.3'". Whatz the reason for getting this error. But I'm able to do all works on GIT Repo. except this PUll PUSH and CLONE. Suggest me what I'm wrong. Thanks in Advance!
  23. peter Says:
    the best and simplest guide i have found, THANKS
  24. Kevin Says:
    Just wanted to add my thanks too! I'll just re-iterate what so many others have already said: this is by far the best guide to getting a simple Git server up and running. I've tried other guides (gitosis, WebDAV, etc...) and just failed at getting them to work. Your guide just took one minute, if that!
  25. Thomas Says:
    Wow this was so unbelieveably simple. Thanks a million times.
  26. JP Says:
    Well done!
  27. richtaur Says:
    "Notice we named our folder with a .git extension." It looks like it's not necessary to actually name your folder "bla.git". Instead you can just point to the .git folder within your desired folder, so like: git remote add origin git@your-server:your-folder/.git

Leave a Reply