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.
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.
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.
Now, back on your local environment you can run
git remote add origin and finally
git push -u origin master.
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
3 thoughts on “Git and Deployments on Shared Hosting”
This is great, but I keep getting 500 errors “..is writeable by group” . because the files are given a 664 instead of 644 permission level. My host won’t allow this.
Even if I set the permissions back to what is correct on the remote server, and I see that my local file permissions are 644 -rw-r–r–, as soon as I push via git, the code goes through fine, but the file permissions all end up back at 664 and cause it to crash since the host won’t allow that.
Any idea how to resolve this? I even reinitiated git and force pushed it up and still happens. I can’t stop it from changing that every time and have tried things like `git config core.fileMode false` with no success.
UPDATE: Sorry, turns out this was a server issue on Namecheap. Others encountered it via SFTP as well. Had nothing to do with my git deployment. I had to add umask 022 to the .bashrc file on the server. This fixed it and it deploys fine now.
Jordan, I’m happy to hear it worked out for you! You may be able to request jailed SSH access from your hosting provider, which may be easier to use for this kind of setup, instead of SFTP.