Deploy Projects to a Remote Server Using Git for Faster, Simpler Updates

Learn how to deploy code to a remote server using Git instead of FTP. This guide walks you through setting up a Git-based deployment workflow for faster and more reliable updates.

Deploy Projects to a Remote Server Using Git for Faster, Simpler Updates
Photo by Damian Zaleski / Unsplash

Keeping your project in sync between your development machine and a remote server doesn't have to be frustrating or repetitive. If you're still using FTP tools like FileZilla or WinSCP to manually transfer files, it's time for an upgrade. Git—the tool you're already using for version control—can also streamline your deployment process.

In this guide, you'll learn how to set up Git-based deployment to a remote server. With just a few commands and a simple hook script, you’ll be able to push changes directly from your local machine to your server, making deployments faster, safer, and easier to repeat.

Requirements Before You Start

To follow this tutorial, ensure the following are in place:

  • Git is installed on both your local machine and the remote server.
  • You have SSH access to your remote server.

With these in place, you're ready to automate deployments using Git.

Set Up the Remote Server for Git Deployment

Initialize a Bare Repository

On your remote server, start by creating a bare Git repository. A bare repository is one that doesn’t contain working files—only the Git version history. It’s typically used for sharing code between environments.

git init --bare project_name.git

You can change project_name.git to any folder name you like. This repository will serve as the destination for your code pushes.

Create a Post-Receive Hook

Once the bare repository is created, navigate into its hooks directory and create a post-receive hook. This script will handle automatic deployment after code is pushed.

cd project_name.git/hooks
nano post-receive

Paste the following script into the file. Adjust the paths to match your project and Git repository locations:

#!/bin/sh

# Deploy code to working directory
git --work-tree=/home/user/project --git-dir=/home/user/project/project_name.git checkout -f

Add Custom Deployment Commands (Optional)

You can extend the post-receive script to handle other tasks like clearing cache, removing temporary files, or running database migrations:

# Navigate to project folder
cd /home/user/project

# Remove unnecessary files
rm README.md

# Run database migrations
php bin/console doctrine:migrations:migrate

# Clear cache
php bin/console cache:clear

Make the Hook Executable

After saving the script, give it executable permissions:

chmod +x post-receive

Your server is now ready to receive and automatically deploy code from your Git project.

Configure Your Local Machine for Remote Deployment

Add the Remote Repository

On your local development machine, add the remote server repository to your Git project using git remote add:

git remote add prod ssh_user@server_ip:/home/user/project/project_name.git

Replace:

  • ssh_user with your SSH username
  • server_ip with your server's IP address
  • The path with your actual remote project directory
⚠️
Avoid naming this remote origin, as that name is typically reserved for your primary remote (like GitHub or GitLab).

Deploy with a Simple Push

Now that the remote is added, you can deploy your code by pushing to the new remote:

git push prod master

Git will connect to your server, update the repository, and automatically run your post-receive script. You’ll see output in your terminal indicating the result of the deployment.

Best Practices for Git-Based Deployment

Here are some additional tips to improve your deployment workflow:

  • Use branches: Instead of pushing directly to master, consider using a dedicated production branch.
  • Automate with CI/CD: For more advanced scenarios, integrate this workflow with a CI/CD pipeline using tools like GitHub Actions or GitLab CI.
  • Secure your SSH access: Use SSH keys instead of passwords and restrict Git commands via limited shell access if needed.

Conclusion

Git-based deployment is a simple yet powerful way to keep your remote server updated without the hassle of manual file transfers. By pushing your changes via Git and leveraging a post-receive hook, you can deploy faster and with fewer errors. Once you set this up, you'll wonder how you ever lived without it.

For more guidance on deployment and server configuration, explore related tutorials on automated deployment tools.