Report this

What is the reason for this report?

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

Posted on August 29, 2024

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!

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.

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

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.