WordPress & CMS Guides

Using Git for Website Version Control on a Server

Using Git for Website Version Control on a Server

Whether you’re a solo developer, a freelancer, or managing a team, keeping your website’s code organized and safe is essential. That’s where Git comes in — and it can make your life a lot easier, especially when you’re hosting on a platform like Vicservers.

In this post, we’ll walk you through how to use Git to manage your website directly on your server.

Why Use Git for Your Website?

Here are just a few reasons:

  • Version Control: Keep track of every change made to your site.
  • Easy Rollbacks: Broke something? Go back to a working version.
  • Team Collaboration: Multiple developers? Git handles it like a champ.
  • Deployment Simplified: Push from local to live with one command.

What You’ll Need

  • A server (like one hosted with Vicservers) with SSH access.
  • Git installed on both your local machine and the server.
  • A basic website project (HTML/CSS/JS, PHP, etc.).
  • Familiarity with the terminal/command line.

Step-by-Step: Deploy a Website Using Git

1. Install Git on Your Server

If it’s not already installed, SSH into your server and run:

sudo apt update
sudo apt install git

(For CentOS: sudo yum install git)

2. Set Up a Bare Git Repository on the Server

SSH into your server and choose where to store your Git repository:

cd ~
mkdir website.git
cd website.git
git init --bare

This is a “bare” repo — it doesn’t contain files, just the Git history.

3. Configure the Post-Receive Hook

This hook will automatically deploy your website when you push to the repo.

cd ~/website.git/hooks
nano post-receive

Paste this:

#!/bin/bash
GIT_WORK_TREE=/var/www/html git checkout -f

Then:

chmod +x post-receive

Make sure /var/www/html is your actual web root directory.

4. Set Up the Local Repository

On your local machine:

cd your-website-folder
git init
git add .
git commit -m "Initial commit"

Then add the remote pointing to your server:

git remote add live ssh-user@your-server-ip:~/website.git

5. Push to the Server

git push live master

That’s it! Your website files will deploy to /var/www/html.

Tips & Best Practices

  • Use .gitignore to exclude files you don’t want on the server.
  • Set up SSH keys to avoid entering your password every push.
  • Back up your server periodically even with Git in place.
  • Use Git branches for staging/production workflows.

Bonus: Secure Your Deployment

Don’t forget to:

  • Use strong SSH keys for authentication.
  • Lock down file permissions (www-data or your web server user should own the deployed files).
  • Optionally, use a CI/CD tool later as your project grows.

Git + Vicservers = Smooth Deployment

At Vicservers, we support developers by offering fast, secure, and reliable servers that are Git-friendly out of the box. Combine Git with our server solutions and you’ve got a powerful setup for modern web development.

Happy coding — and even happier deploying!

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button