Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Run Your Own Git Server for Under $100

DZone's Guide to

Run Your Own Git Server for Under $100

If you're looking to host your private repos, don't pay others to do it—build your own Git server!

· IoT Zone
Free Resource

Discover why Bluetooth mesh is the next evolution of IoT solutions. Download the mesh overview.

Most open source projects are found via GitHub, and if you wanted to keep your project available to the world, GitHub is a good home for your repos. What about private repos and user access? GitHub, BitBucket, and others give you the ability to host private repos on their site, but at a cost.

There are alternatives such as DropBox, Google Drive, and hiding your repos on a shared-hosting server, but who really wants to do that? The former options are workable, but are messy and a lame hack at best and lack access control. 

Solution: Mix a Raspberry Pi or PogoPlug (personal favorite although hard to find sometimes or my current setup) with a little ArchLinux (or pretty much any Unix based OS) and some command line skills to create your own.
Device approximate cost: $18-40
Optional Hard Drive: Varies

“Gitolite allows you to setup git hosting on a central server, with very fine-grained access control and many (many!) more powerful features.”

The main benefit of rolling your own Git Server is you are in control from start to finish. If a company changes their pricing plan, there is no impact. If someone joins or leaves your team, simply add or remove their access. If you want to scale your server, make it happen. Complete control.

Let’s Get Started 

  • Create a dedicated userid to host the repos like “git” (for security reasons this user has shell access only “su - git” from a separate user account on the same server).
  • Login to your server and su over to your “git” account “su - git” .
  • Make sure that ~/.ssh/authorized_keys is empty and if it is not make it empty
  • Copy your ssh public key from your workstation
    • On the computer that is separate from your server run ssh-keygen
    • Name the file $HOME/YourName.pub
  • Install gitolite by running the following
    git clone git://github.com/sitaramc/gitolite
    mkdir -p $HOME/bin
    gitolite/install -to $HOME/bin
  • With yourself as the administrator, setup Gitolite

    gitolite setup -pk YourName.pub
  • It is possible that the previous command does not work because “bin” is not in your “PATH”, so in that case run:

    $HOME/bin/gitolite setup -pk YourName.pub

    Add Repos and Users

    First things first—never add users or repos manually to the server. All of these changes are controlled by a special repo called “gitolite-admin”. The administration workflow is fairly simple and straightforward. 

    Run the following command:

    git clone git@host:gitolite-admin

    You have now just downloaded the control center of your git server. Now cd into the folder “cd gitolite-admin”, there you will see the two directories “conf” and “keydir”. User keys are added into “keydir”(jack.pub, jill.pub, and john.pub)  and repos are added by editing “conf/gitolite.conf”. 

    To add a repo “tester” and provide access to jack, jill, and john, add the following to “conf/gitolite.conf”:

    repo tester
        RW+    =   john
        RW     =   jack
        R      =   jill

    In the previous example, Jill has read only access. Jack has read/write and can also fast-forward the push of a branch and create new branches/tags. And John can do anything he likes; fast-forward, rewind or delete branches/tags.

    As you make changes you are going to want to run git add conf or  git and keydir . Next, commit your changes using git commit -m “created tester and added users”  followed by git push. Once the push is complete, you have both added new users to ~/.ssh/authorized_keys and created new users on your server.

    Access From Abroad

    If you are setting up your git server on your personal network and desire to provide access to the outside world, think security first, but it is easily done. First, make sure that your firewall is up to the task and that you are comfortable with its settings. Secondly, make sure that you setup port forwarding for the desired port (the default for most systems is 22). Thirdly, if you do not have a static IP address, pick a dynamic DNS service to manage directing traffic to your server’s IP address.
    Optional Dynamic DNS service: free to $35 per year

    In Closing

    Depending on your specific needs this solution scales well for the hobbyist or the large organization (For Example: The Fedora Project - over 10,000 repos and 1,000 maintainers and  Gentoo Linux 200 repos with more than 1,000 users). With the incredible flexibility provided by Gitolite hosting and managing repos is as simple as running a few commands in the terminal, which gives you the control and a certain level of future proofing your data.

    References/Resources

    https://github.com/sitaramc/gitolite - official GitHub page for the open sourced project

    https://groups.google.com/forum/#!forum/gitolite  - the official and active support forum

    http://archlinuxarm.org/platforms/armv5/pogoplug-series-4- reference to installing ArchLinux on a PogoPlug series 4 device

    http://archlinuxarm.org/platforms/armv5/pogoplug-v2-pinkgray- reference to installing ArchLinux on a PogoPlug version 2 device

    Related Refcard:

    Take a deep dive into Bluetooth mesh. Read the tech overview and discover new IoT innovations.

    Topics:
    iot ,git ,server ,raspberry pi ,pogoplug

    Opinions expressed by DZone contributors are their own.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}