Git and Deployments on Shared Hosting

Just a quick guide to setting up Git with deployments on a shared hosting account. If your host allows you to use git on the server and you have SSH access, then this should be relatively straight-forward.

Local Environment

Assuming you have Git installed, simply git init in the directory you want to start version controlling on your local machine. After that, and if this is your initial commit and you have files, run git add and git commit -m "Initial commit" to get started.

Remote Environment

SSH into your hosting environment and depending on its setup you may need to do a few things. With my hosting I needed to modify my .bashrc file to set up the proper pathing for my git install. Your mileage may vary here:

PATH=$PATH:/usr/local/cpanel/3rdparty/bin
export PATH

Now, I also faced an issue where I wasn’t able to push to my non-bare server. I have a few other sites on files that I’m not version controlling, so I needed to run the following in my root directory where git lives:

					git config receive.denyCurrentBranch ignore
				

git init in the directory you want to start version controlling and add this as a remote on your local machine.

I have not yet had any issues with setting this config, but there are very likely better ways to do this. I need to do more research here, but it’s working for now.

Local Environment

Now, back on your local environment you can run git remote add origin and finally git push -u origin master.

Deployments

If you’ve followed up to this point, you should able to utilize your hosting environment as a git repo. However, this is likely not exactly what you wanted – you probably also want your master branch to deploy the code you push to it, so it gets pushed live when you commit. There’s one more step to get this working. Back on your hosting environment, in your root git repo, you’ll want to create a file .git/hooks/post-receive. In that file, you can paste the following code:

#!/bin/sh
# Save this in: PATH_TO_REPO/.git/hooks/post-receive
GIT_WORK_TREE=../ git checkout -f

Finally run the following to make it executable:

					chmod +x .git/hooks/post-receive
				

 

What this will do is deploy code you push to you branch automatically. If I were able to install Node.js I would take this a step further and keep compiled files like CSS and JavaScript out of my version control and use a Grunt script to compile those when deployed. Currently that is not possible on my shared hosting.

Leave a Reply

Your email address will not be published. Required fields are marked *