Question

How can I connect my local Git repository to a DigitalOcean server for easy code deployment?

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?


Submit an answer


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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
October 27, 2024

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:

1. Set Up SSH Access to Your Droplet

  • Generate an SSH key pair if you don’t have one:
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  • Copy your public key to the Droplet:
    ssh-copy-id root@your_droplet_ip
    
  • Test your SSH connection to confirm it’s set up correctly:
    ssh root@your_droplet_ip
    

2. Create a Dedicated Git User on the Droplet

  • To keep deployments organized, create a git user:
    sudo adduser git
    
  • Add your SSH key to the git user’s .ssh/authorized_keys for secure access.

3. Initialize a Bare Git Repository on the Droplet

  • Log in as the git user and create a bare repository for your app:
    mkdir -p /home/git/yourapp.git
    cd /home/git/yourapp.git
    git init --bare
    

4. Set Up a Post-Receive Hook for Deployment

  • Inside /home/git/yourapp.git/hooks/, create a post-receive hook to deploy code automatically:
    nano /home/git/yourapp.git/hooks/post-receive
    
  • Add these lines to specify where the code will be deployed:
    #!/bin/bash
    GIT_WORK_TREE=/var/www/yourapp git checkout -f
    
  • Make this hook executable:
    chmod +x /home/git/yourapp.git/hooks/post-receive
    

5. Add the Droplet as a Remote in Your Local Repository

  • On your local machine, add the Droplet as a remote using the main branch:
    git remote add droplet git@your_droplet_ip:/home/git/yourapp.git
    

6. Push Your Code to Deploy

  • Now, deploy your code by pushing to the new remote:
    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

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

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

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.