I’m new to using Git with DigitalOcean and would like to know the best way to connect my local Git repository to my DigitalOcean server. What are some basic steps to set this up for deploying code directly from my local machine?
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!
Hey!
Usually you would use GitHub or GitLab, and on there setup an automation to push your code to your server whenever a pull request is merged.
But if you are specifically looking to set up code deployment from your local Git repository to a DigitalOcean Droplet directly without using GitHub, you could setup a post-receive
hook to deploy code automatically whenever you push your code.
Here is one way of doing this:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-copy-id root@your_droplet_ip
ssh root@your_droplet_ip
git
user:
sudo adduser git
git
user’s .ssh/authorized_keys
for secure access.git
user and create a bare repository for your app:
mkdir -p /home/git/yourapp.git
cd /home/git/yourapp.git
git init --bare
/home/git/yourapp.git/hooks/
, create a post-receive
hook to deploy code automatically:
nano /home/git/yourapp.git/hooks/post-receive
#!/bin/bash
GIT_WORK_TREE=/var/www/yourapp git checkout -f
chmod +x /home/git/yourapp.git/hooks/post-receive
main
branch:
git remote add droplet git@your_droplet_ip:/home/git/yourapp.git
git push droplet main
This setup will allow you to deploy your application by pushing to the droplet
remote.
If you’re looking for a simpler way to deploy your application, consider DigitalOcean App Platform. It has built-in Git and GitHub integration for automated deployments, saving you from manual setup and handling deployments with every Git push to main
. This is a great option if you want a streamlined, managed approach to code deployment!
For more details, check out the DigitalOcean App Platform documentation.
Let me know if you need further help!
- Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.