# Simple Git Auto Deployment to Digital Ocean or any other VPS

I have been using Digital Ocean VPS for couple of years now. I am always editing my websites even their technology stack has changed back and forth multiple times. I have always used FileZilla to transfer my code from local to remote repository. This is not efficient way and while searching for better way of autodeployment I came across an article by Digital Ocean itself for using git for deployment. This article recommended using beta directory for testing and then deploying to your primary directory. I thought it is not required for small and single developer websites so I thought to create a simple structure.

Here we will have only two repositories

• Local repository where I will be making changes
• Remote repository on Digital Ocean droplet from where my live website will run

Installing git

$sudo apt-get install git  ### Step#2 Initiate Git repo On Local machine cd to folder having your code and then initiate git repo as below $git init


On remote server, cd to directory which will host your code and then initiate git repo

$git init --bare  Please note the difference : A bare repository is a git repository without a working copy, therefore the content of .git is top-level for that directory. Use a non-bare repository to work locally and a bare repository as a central server/hub For example, when you create a repository on github.com, it is created as a bare repository. To conclude, the repository on the server side is going to get commits via pull and push, and not by you editing files and then committing them in the server machine, therefore it is a bare repository. You can push your changes from local to remote by using following command ### Step#3 Add remote repository You can add remote repository using below command. $git remote add live ssh://user@domain.com/var/www/foldername


Here instead of domain.com you can use your droplets ip as well.

Once this is added you can check your remotes using following command

$git remote -v live ssh://user@mydomain.com/var/www/foldername (fetch) live ssh://user@mydomain.com/var/www/foldername (push)  ### Step#4 Deploying the changes. Once your changes are ready, you can deploy it using following commands git add . git commit -m "1st commit" git push live master  Step#5 Verify the changes You can log in to your droplet and check if your code is updated. You can do this by simply verifying the files. #### Troubleshooting I faced an issue that my local repo was pushing files successfully but remote git repo was not updated. To check if git changes were actually pushed or not run following command $git log


This will show list of changes pushed to remote. If you see your changes are pushed but still not reflected, you need to run following manually

GIT_WORK_TREE=/var/www/foldername git checkout -f


Obviously, you should not run above command manually every time you make the change as this will loose the whole purpose of deployment automation.

cd to hooks folder, create post-receive file and add above line into file

cd hooks/
sudo nano post-receive

Once file is created, change the file permissions as below


sudo chmod a+x post-receive


And now you are all set. Going forward, any change that you push will be reflected on your remote repository.

Important Note: Depending on your changes and the type of application that you are hosting, you might have to restart the server to have these changes reflected on your website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.