How to Install and Use Git on Your VPS: A Step-by-Step Guide

0
3

git on vps

If you’re managing your own code and want full control over where it lives, installing Git directly on your VPS gives you a private, self-hosted alternative to third-party platforms.

This guide walks through installing Git, setting up secure SSH access, and creating a bare repository you can push to and pull from — no third-party Git hosting account required.

By the end, you’ll be able to deploy code straight to your VPS using nothing but standard Git commands.

Prerequisites

  • A VPS with root or sudo access — an Exabytes NVMe VPS plan works well for this.
  • Basic comfort with the Linux terminal.
  • Git installed on your local computer to clone from and push to the server.

Step 1 — Install Git on Your VPS

Most VPS images come with Git pre-installed, but it’s worth verifying before assuming it is.

On Ubuntu or Debian, update your package list and install Git with: apt update && apt install git

On CentOS, RHEL, or Fedora, use: yum install git

Once installed, confirm the version with: git –version

Step 2 — Configure Your Git Identity

Git attaches an author name and email to every commit, so set these globally before you start committing:

git config –global user.name “Your Name”

git config –global user.email “[email protected]

Step 3 — Generate and Add an SSH Key

SSH keys are the recommended way to authenticate with your Git server — DigitalOcean’s own private Git server tutorial specifically recommends key-based authentication over passwords for this reason.

On your local machine, generate a key pair with: ssh-keygen -C “[email protected]”, and set a passphrase when prompted for an extra layer of protection.

This creates a private key (keep this secret) and a public key ending in .pub, which you’ll copy to your VPS.

Step 4 — Create a Dedicated Git User (Recommended)

Rather than using root for day-to-day Git operations, create a separate system user dedicated to Git.

On your VPS, run: useradd -m git, then set a password with: passwd git. The -m flag ensures a home directory is created for the account.

Switch to that user, create an SSH directory, and add your public key to its authorized_keys file so it can authenticate without a password.

Step 5 — Create a Bare Repository on Your VPS

A ‘bare’ repository stores only the version history — no working directory of files — because it’s meant to be pushed to and pulled from, not edited directly on the server.

As the Git user, create your project folder and initialise it: git init –bare myproject.git

That’s the entire server-side setup — your VPS is now ready to receive pushes.

Step 6 — Clone and Push From Your Local Machine

For a brand-new project, initialise it locally and point it at your VPS: git init && git remote add origin git@your-vps-ip:myproject.git

For an existing local repository, simply update the remote: git remote set-url origin git@your-vps-ip:myproject.git

Push your first commit with: git push origin main — from then on, every push and pull goes directly to your own server.

Optional — Auto-Deploy With a Post-Receive Hook

You can turn a simple push into an automatic deployment using a Git hook.

Inside myproject.git/hooks, create a file named post-receive containing a short script that checks out the latest code into your live web directory whenever a push is received.

This gives you a lightweight continuous deployment setup without needing a separate CI/CD platform.

Troubleshooting Common Issues

  • Permission denied (publickey) — confirm your public key is correctly listed in the server’s authorized_keys file, and that the .ssh directory has 700 permissions and the key file has 600.
  • fatal: not a git repository — double-check the exact server path and confirm the repository was initialised with the –bare flag.
  • Connection timed out — verify your VPS firewall allows inbound traffic on port 22 (SSH).

Frequently Asked Questions

Do I need a static IP to host Git on my VPS?

Not strictly, but a static IP — or a domain name pointed at your VPS — makes cloning and pushing far more reliable than relying on an address that can change.

Is self-hosted Git safe for production code?

Yes, when secured properly with SSH-key-only authentication, restricted user permissions, and regular backups — this is the same baseline approach recommended in DigitalOcean’s own Git server tutorial.

What’s the difference between git-scm and GitHub?

git-scm.com is the official home of the open-source Git software itself, while GitHub is a separate, third-party hosted platform built on top of Git that adds a web interface and collaboration tools — you can run Git entirely without GitHub, exactly as in this guide.

Can I run Git and a live website on the same VPS?

Yes — this is a common setup, especially when using a post-receive hook to deploy straight from your Git repository to your website’s directory on the same server.

How much VPS storage or RAM do I need to host Git?

A bare Git repository itself is lightweight, since it mostly stores text-based diffs — size your VPS around whatever application or website you’re deploying alongside it rather than around Git itself.

Conclusion

Installing Git on your own VPS takes about ten minutes and gives you a private, fully self-hosted alternative to third-party Git platforms.

Once it’s running, you can extend it with a deployment hook, a web interface like Gitea, or simply keep it as a lightweight private repository for solo projects.

An Exabytes NVMe VPS gives you the root access and fast storage this setup relies on, whether you’re hosting Git alongside a live site or on its own.