Skip to content

The Foundation of GitHub: How to Host Your Own Git Repository Server

GitHub and similar platforms are built on centralized bare Git repositories, network protocols, and user management. You can replicate this on a VPS for private collaboration.


1. Prepare Your VPS

  1. Choose a VPS provider and get SSH access as root (or a sudo user).
  2. Install Git:
bash
sudo apt update
sudo apt install git
  1. Optionally, create a dedicated git user for repository management:
bash
sudo adduser --disabled-password --gecos "" git
sudo mkdir -p /home/git/repos
sudo chown -R git:git /home/git/repos

2. Create a Bare Repository

A bare repository is the core of a collaborative Git server:

bash
sudo -u git git init --bare /home/git/repos/toolkit.git
  • No working files, only Git history and metadata.
  • Multiple developers can push/pull safely.

3. Configure SSH Access for Developers

  1. Each developer should generate an SSH key:
bash
ssh-keygen -t ed25519 -C "developer@example.com"
  1. Add their public key to /home/git/.ssh/authorized_keys:
bash
sudo mkdir -p /home/git/.ssh
sudo nano /home/git/.ssh/authorized_keys

Paste each key on a separate line. Then set permissions:

bash
sudo chown -R git:git /home/git/.ssh
sudo chmod 700 /home/git/.ssh
sudo chmod 600 /home/git/.ssh/authorized_keys

4. Test Cloning and Pushing

From a developer machine:

bash
git clone ssh://git@VPS_IP_ADDRESS/home/git/repos/toolkit.git
cd toolkit
echo "Hello World" > README.md
git add README.md
git commit -m "Initial commit"
git push origin main
  • If successful, the VPS is now the central repository.
  • Other developers can clone, branch, and push to the same repository.

5. Optional: Web Interface (Gitweb or Gitea)

For a GitHub-like experience:

  • Gitweb: Lightweight CGI-based interface.
  • Gitea: Full-featured self-hosted GitHub clone.
  • GitLab: Enterprise solution (requires more resources).

Install Gitweb (Debian/Ubuntu):

bash
sudo apt install gitweb lighttpd
sudo ln -s /usr/share/gitweb /var/www/gitweb
sudo nano /etc/gitweb.conf
# set $projectroot = "/home/git/repos";

Access via: http://VPS_IP/gitweb/


6. Tips for Collaboration

  • Branches & Pull Requests: Developers can create feature branches locally and push them to the central repo.
  • Permissions: Use SSH keys to control who can push to which repositories.
  • Backups: Regularly backup /home/git/repos.
  • Hooks: Automate tasks on the server, like CI/CD triggers (post-receive hook).

7. Directory Structure Example

/home/git/repos/
├── toolkit.git          # Bare repo for collaboration
├── project1.git
└── project2.git
  • Developers interact via SSH: ssh://git@VPS_IP/repos/toolkit.git
  • All commits and updates sync through this central hub.

8. Security Recommendations

  • Use non-root Git user.
  • Enforce SSH key authentication only.
  • Disable password login in /etc/ssh/sshd_config.
  • Firewall: only allow SSH (22) and optionally HTTP/HTTPS for web UI.

Conclusion

With this setup:

  • You now have a central Git repository similar to GitHub.
  • Developers can collaborate over SSH.
  • Optional web interface provides a GUI for project management.
  • Hooks and automation extend functionality.

This is essentially the foundation of GitHub: central bare repositories, network access, and user management — scaled for millions of developers.