This guide walks you through setting up your own private Git server on a VPS (Virtual Private Server). You’ll get a central place to store your repositories with SSH-based access, giving you full control over your code and no dependency on third-party hosting. By the end, you’ll be able to clone, push, and pull from your own server using standard Git commands.
What You’ll Need
- A VPS running a modern Linux distribution (Ubuntu 22.04 or Debian 12 recommended). You can use any provider that gives you root SSH access.
- SSH client installed on your local machine (usually preinstalled on macOS and Linux; on Windows, use PowerShell or install OpenSSH).
- Basic familiarity with the command line and Git.
- Git installed on your local machine (if not, install it from git-scm.com or your package manager).
- A domain name (optional, but makes SSH config easier). If you skip it, you’ll use the server’s IP address.
Step-by-Step
- Connect to your VPS. Open a terminal and SSH into your server as a user with sudo privileges. For example:
ssh user@your-server-ip. If you’re using a key pair, ensure the key is added to your SSH agent. - Update the system. Run
sudo apt update && sudo apt upgrade -y(for Debian/Ubuntu) to ensure all packages are current. - Install Git. Run
sudo apt install git -y. Verify withgit --version. - Create a dedicated Git user. This user will own all repositories and will be the account you SSH into for Git operations. Run:
sudo adduser git. Follow the prompts to set a password (or skip if you prefer key-only access). This user should not have sudo privileges. - Set up SSH keys for the git user. On your local machine, if you don’t have an SSH key pair, generate one with
ssh-keygen -t ed25519 -C "your_email@example.com". Then copy your public key to the server:ssh-copy-id git@your-server-ip. This adds your public key to/home/git/.ssh/authorized_keys. Test withssh git@your-server-ip– you should get a shell (we’ll restrict it later). - Create a repositories directory. As the git user (or via sudo), create a directory to hold all repos:
sudo mkdir /home/git/reposand set ownership:sudo chown git:git /home/git/repos. - Restrict the git user to Git commands only. Edit the SSH configuration for the git user by adding a forced command. Create a file
/home/git/.ssh/authorized_keysif it doesn’t exist. For each public key line, prefix it with:command="/usr/bin/git-shell -c \"$SSH_ORIGINAL_COMMAND\"",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-ptyfollowed by the key. This ensures the user can only run Git commands, not a shell. - Create a bare repository. On the server, as the git user, navigate to the repos directory and create a bare repo:
cd /home/git/repos && git init --bare myproject.git. The.gitsuffix is a convention for bare repositories. - Clone from your local machine. On your local machine, clone the empty repo:
git clone git@your-server-ip:/home/git/repos/myproject.git. If you set up a domain, replace the IP with the domain (e.g.,git@git.example.com:/home/git/repos/myproject.git). You’ll be prompted to confirm the host key on first connection. - Add files and push. Add some files to the cloned repo, commit, and push:
cd myproject, create a file,git add .,git commit -m "initial commit", thengit push origin master(ormainif you set that as default). - Configure Git to use a shorter SSH alias (optional). On your local machine, edit
~/.ssh/configto add a Host entry:Host gitserverHostName your-server-ipUser git. Then you can clone usinggit clone gitserver:repos/myproject.git.
Common Mistakes
- Forgetting to make the repo bare. If you use
git initinstead ofgit init --bare, you’ll have a working directory that can cause conflicts. Always use bare for central servers. - Not setting permissions correctly. If the git user doesn’t own the repos directory, you’ll get permission errors when pushing. Ensure
chown -R git:git /home/git/repos. - Using the wrong SSH key. Make sure the public key you added to the server matches the private key on your local machine. Test with
ssh -T git@your-server-ip– you should see a Git shell message, not a shell prompt. - Ignoring the forced command syntax. If you misplace quotes or backslashes in the authorized_keys file, SSH may fail entirely. Double-check the syntax.
Tips
- Use SSH keys instead of passwords. It’s more secure and convenient. Disable password authentication for the git user if you want extra security.
- Set up a domain name. It makes SSH config cleaner and avoids remembering IP addresses. Add an A record pointing to your VPS.
- Enable Git’s built-in hooks. You can add post-receive hooks to trigger deployments or notifications.
- Back up your repos regularly. Use
git clone --mirroror simply tar the repos directory to a safe location. - Use a firewall. Only allow SSH (port 22) and maybe HTTP/HTTPS if you plan to serve Git over those protocols later.
Troubleshooting
- Permission denied (publickey). This means your SSH key isn’t being accepted. Check that the public key is in
/home/git/.ssh/authorized_keys, that the file permissions are correct (chmod 700 ~/.sshandchmod 600 ~/.ssh/authorized_keys), and that you’re connecting as the git user. - Connection refused. SSH might not be running on the server, or the firewall is blocking port 22. Check with
sudo systemctl status sshandsudo ufw status. - Fatal: ‘/home/git/repos/myproject.git’ does not appear to be a git repository. This often means the path is wrong or the repo doesn’t exist. Verify the repo exists on the server and that you’re using the correct path.
- Push fails with ‘cannot lock ref’ or ‘unable to create …’ Check file permissions on the repo directory. The git user must have write access.
- Git shell appears but commands don’t work. If you see a message like “fatal: unrecognized command ‘ls'”, it means the forced command is working correctly – you can only run Git commands.
Frequently Asked Questions
Can I use this setup for multiple users?
Yes. Add each user’s public key to the authorized_keys file. Each user can then access the repositories. For finer control, you can create separate users or use a tool like Gitolite.
How do I create a new repository on the server?
SSH into the server as the git user (or via sudo), navigate to /home/git/repos, and run git init --bare newrepo.git. Then clone it from your local machine.
Can I use HTTP/HTTPS instead of SSH?
Yes, but it requires additional setup (like a web server and authentication). SSH is simpler and more secure for a small team.
What is the difference between a bare and non-bare repository?
A bare repository has no working directory – it only contains the Git data. It’s meant to be a central hub for pushing and pulling. Non-bare repos are for development with a working copy.
How do I back up my Git server?
You can simply copy the entire /home/git/repos directory to another location. Alternatively, use git clone --mirror for each repo to create a full backup.
Is it safe to use the git user for other tasks?
No. The git user should only be used for Git operations. The forced command restricts it to Git commands, which is a security best practice.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- How to Choose an Amplifier for Magico A3 Speakers
- How to Ban a Player from Your FiveM Server
- Pokemon Go Friend Codes: How to Add & Use Them33.33 XP/day
- Google Algorithm Change: Original Reporting Boost
- Create Folder for Selected Item on Mac: Full Guide
- Data Center Location Questions for AI Workloads