Report this

What is the reason for this report?

How to set up automatic deployment with Git on Ubuntu

Posted on July 13, 2026
Ruz

By Ruz

t



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

There are two directions depending on how much infrastructure you want to run yourself.

If you want to keep it on your own VPS, the classic setup is a bare git repo with a post-receive hook. You create a bare repo on the server, add it as a remote from your local machine, and the hook checks the code out into your web directory and restarts your app whenever you push. Something like this in hooks/post-receive:

#!/bin/bash
git --work-tree=/var/www/myapp --git-dir=/home/deploy/myapp.git checkout -f main
cd /var/www/myapp && npm install && systemctl restart myapp

That’s the lightest version. The more common approach these days is to push to GitHub and have a GitHub Actions workflow SSH into the server on merge to run the pull and restart, which keeps the deploy logic in your repo and gives you a log of every deploy.

The other direction is to skip managing the pull step at all. DigitalOcean’s App Platform connects straight to a GitHub repo and redeploys on every push to the branch you pick, so you get the auto-deploy without maintaining hooks or a CI pipeline yourself. Worth considering if the git hook is the only reason you’d be SSHing into a box, though the hook approach is fine if you want full control.

Heya,

There are two directions depending on how much infrastructure you want to run yourself.

If you want to keep it on your own VPS, the classic setup is a bare git repo with a post-receive hook. You create a bare repo on the server, add it as a remote from your local machine, and the hook checks the code out into your web directory and restarts your app whenever you push. Something like this in hooks/post-receive:

#!/bin/bash
git --work-tree=/var/www/myapp --git-dir=/home/deploy/myapp.git checkout -f main
cd /var/www/myapp && npm install && systemctl restart myapp

That’s the lightest version. The more common approach these days is to push to GitHub and have a GitHub Actions workflow SSH into the server on merge to run the pull and restart, which keeps the deploy logic in your repo and gives you a log of every deploy.

The other direction is to skip managing the pull step at all. DigitalOcean’s App Platform connects straight to a GitHub repo and redeploys on every push to the branch you pick, so you get the auto-deploy without maintaining hooks or a CI pipeline yourself. Worth considering if the git hook is the only reason you’d be SSHing into a box, though the hook approach is fine if you want full control.

Hi there,

The question seems a bit incomplete but I will assume you want to set up automatic deployments to a DigitalOcean Droplet when you push to Git.

The simplest approach is a bare Git repo with a post-receive hook on the server:

# On your Droplet
mkdir -p /var/repo/myapp.git
cd /var/repo/myapp.git
git init --bare

Then create the hook:

cat > hooks/post-receive << 'EOF'
#!/bin/bash
GIT_WORK_TREE=/var/www/myapp git checkout -f
sudo systemctl restart myapp
EOF
chmod +x hooks/post-receive

On your local machine, add the Droplet as a remote:

git remote add production ssh://user@your-droplet-ip/var/repo/myapp.git
git push production main

Every push to that remote triggers the hook and deploys your code.

If you want something more robust with rollbacks, environment variables, and zero downtime, GitHub Actions deploying via SSH is the next step up. DigitalOcean also has App Platform which handles all of this automatically if you want to skip the manual setup entirely: https://docs.digitalocean.com/products/app-platform/

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.