Thinking about setting up your own Git server? There are plenty of perks to doing so instead of relying on platforms like GitHub. For starters, you’ll have complete control over your code, ensuring it can’t be used for profit or deleted by anyone but you.

What You’ll Need

  1. A Computer for Your Server: Ideally running Linux (Debian is a solid choice).
  2. Internet Connection: Pretty essential for accessing your server remotely.
  3. Another Computer with Git Installed: This is what you’ll use to interact with your server.

Step-by-Step Setup Guide

1. Install Git on Your Server

First, you need to install Git on your server. Depending on your Linux distribution, use one of these commands:

  • For Debian-based systems (like Ubuntu):
    sudo apt install git
    
  • For Fedora-based systems:
    sudo dnf install git
    
  • For Arch-based systems:
    sudo pacman -S git
    

2. Create a Git User

Next, create a dedicated user for Git operations:

sudo useradd git

You’ll be prompted to enter some information. Feel free to leave everything blank except for the password.

3. Secure Your Server with SSH Keys

Log in as the new Git user:

su git

Then, enhance security by setting up SSH keys:

mkdir .ssh
chmod 700 .ssh/
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys

4. Generate and Add SSH Keys

On your main machine (the one you’ll be committing from), generate an SSH key:

ssh-keygen

Copy the SSH public key to your server:

ssh-copy-id git@your-server-ip

To find your server’s IP, use:

ifconfig

Now, you should be able to log in to your server without needing a password:

ssh git@your-server-ip

5. Set Up Your Git Repository Directory

Create a directory to store your Git repositories. For example:

mkdir ~/git

Give ownership of this directory to the Git user:

chown git:git ~/git

6. Create a New Repository

For each project, create a separate directory:

mkdir ~/git/my_cool_project.git

Initialize the repository as a bare repository (suitable for sharing):

cd ~/git/my_cool_project.git
git init --bare

7. Test Your Setup

To test your new Git server, try cloning the repository from another machine:

git clone git@your-server-ip:~/git/my_cool_project.git

You might see:

warning: You appear to have cloned an empty repository.

That’s because you haven’t added any files yet. Go ahead and add a file:

touch stuff.txt

Commit your changes:

git add .
git commit -m "first commit"

Now, try cloning again, and everything should work smoothly.

Congratulations!

You’ve successfully set up your own Git server! Now you have full control over your code and can collaborate without relying on third-party services.

Enjoyed this guide? Consider supporting me to help me create more content like this!

Subscribe

Where I share what I’ve been up to that week, including articles I’ve published, cool finds, tips and tricks, and more! Receive an email every time I post something new on my blog

No spam, no ads. Unsubscribe at any time.