Of all version control systems, Git has proved most versatile and robust, at least for the needs of my employers and me. Currently, Github is by far the most popular hosted Git service, and is a wonderful way to make coding social or collaborate on someone else’s project.
However, you may find yourself working on project that you want to keep private for a while, and you don’t want to pay Github’s service fees for whatever reason. In this case, you want a private Git repo and server.
This article demonstrates how to setup a Git server on one of Digital Ocean’s Ubuntu droplets. However, the process should be similar for other Linux distros, excepting any distro specific commands.
It is also assumed your development machine uses Linux, OSX or some other UNIX variant. This article uses command line references, so hopefully you have some knowledge of that as well.
On the development machine, you’ll want to create an SSH key from the command line.
The second line spits out your SSH key. Take note of it for now and we’ll add it to the server later.
If you have a bare server that you’ve never interacted with much, you’ll want to setup SSH access. It’s best to create a separate user to perform automated Git related actions, but I’ll leave that up to you and your discretion for now.
With your SSH access, login to your remote server. Your credentials to achieve this are located in Digital Ocean’s admin panel.
After logging in, add your SSH key generated above to your list of authorized keys. You can do that by using your text editor of choice (vim, nano, etc) and pasting your key inside the file.
Since you’ve added your development environment’s SSH key here, you should be able to SSH in without entering your password. Try it out!
Next, pull in your updates then install Git. You may already have Git installed, and if so just skip this step. In case you’re not positive if Git is installed, just run
which git at the command line to find out. If it’s installed, you’ll get the installation location returned. If not, time to install it!
If you’re using this server by yourself, then you’re finished with the server setup. However, if you’ll be working on a team with other folks, you’ll want to do some additional configuration to allow that. This extra configuration is up to you, the reader, yet here are two resources to get started in that direction: Gitosis and Gitolite.
Now that we’ve configured server, it’s time to create some repos! If you created a separate user for Git related activity, login as that user. Otherwise, you’re ready to begin.
Change directories to your user’s home. Then, create a repo using any name you want. Here, I just called it
repo.git, but you can be as creative as you want!
Finally, change into this new Git directory and initialize a bare repository. That’s it!
It’s now time to setup your codebase on your development machine. To start, exit from your server.
Change directories into your codebase. If you haven’t created a Git repository here, go ahead and do that now. Make sure to add in your files too.
If you’ve already created your Git repository, delete any origins in case they exist by running
git remote rm origin. This is just a precaution to ensure everything is pushed to your personal server.
Next add your remote server and push in all your commit files. Note that the example uses
root for the username,
server.com for the server name or IP address (you can use either) and
repo.git for the repository name. You’ll want to use your own values there.
The final two commands above simply set a default merge and remote.
Well, that wraps it up! I’ve demonstrated here a quick and easy way to setup your own Git server, so that you can maintain a private repository without paying any more than you already do for hosting.