How to setup your own Git server

So I have been using Git for a few years now locally. I have also used Github and Bitbucket. I host my own server (this one) for which I want to deploy code too. I don’t want to pay for GitHub and Bitbucket is meh! What I really want is to setup Git on my own server and use the awesome GitHub for Windows client with my own server. I have tried in the past to set this up but failed. Today is the day I figured out the right combo of git init command and ssh keys to get it all to work!

This is all you need:

  • Ubuntu Server or any Linux box will work, but I am using the apt-get command in this example.
  • Windows 8 but any modern version of Windows should work
  • Github for Windows but you can try other clients, you just need to find that client’s public key.

First on your Windows computer install Github for Windows.

Go into C:/Users/{username}/.ssh/ and open “github_rsa.pub” in a text editor. This is the public key used by Github for Windows.

Copy the contents of this file. It is start with “ssh-rsa” and end with {username}@{computername}

Now SSH into your Linux server.

Edit /home/{username}/.ssh/authorized_keys and paste in the the key you copied earlier all on one line. Save it.

Install Git using what ever package management system you have, in my case that would be “apt-get install git”

Create a directory to hold your repos. I am going to use “git” but you can use what you want. I did “mkdir /home/{username}/git”

Change into this directory “cd /home/{username}/git”

Now create a bare repo here. It is very important to use the “–bare” option. This is what messed me up in the past. Bare means there is no working directory, no code files will be brought into the filesystem. Only the Git internal files will be created. Also the convention it is git the folder a “.git” extension and “git init –bare project.git” but replace “project” with what ever you want the repo named, maybe the domain name of the website or the namespace of the Java or C# project.

Log off your Linux server.

Back in Windows launch Github for Windows. Do any setup that is required such as your name and email.

Now there are two ways to go there. You can clone the remote repo from the command line, or create a new local report in the GUI and set its remote origin to your remote repo.

I am going to cover the full GUI method.

In the “Local” click “create”. In the name field enter the name that matches the repo name you created on your server minus the .git. So in this case “project”. Click Create.

Now you will see the repo listed in your Local repos. Click the little arrow to enter this repo.

Now we need to setup the remote origin to link it to our Linux server, but first we need to commit to a branch so there is something to push to the remote server. By default Github will want to add the .gitattributes and .gitignore files. Lets do it, enter a commit message and commit those suckers.

Now click the Gear icon in the top right and select “Repository settings”. In the “primary remote (origin)” text box enter “ssh://{username}@{server}/{full path}/project.git”. In my case it would be “ssh://gary@g25.org/home/gary/git/garyscott.net.git”. Click Update. See the Git Book for more details on protocols if needed.

Now the “Publish to Github” button has changed to “Publish”. Click it. If all is good it will turn to a “Sync” button and alert that you are at “+1” up. Click Sync to push up your branch and commit.

Enjoy!