Use git push to production server on shared hosting
I recently set out to improve my workflow. I've been using FTP to send files from my local server up to my production server and it's just way, way too inefficient. Plus, I have a hard time remember which files I modified. Git helps, but I still have to do a git status and then read through each one and manually upload each file. What a pain. So I set out to figure out how to git push to production server on shared hosting. I use Web Hosting Hub.
I found this tutorial which helped.
But not all of the info for my needs was in one neat place. So I decided to write it down here, both for me to remember and in case it may help others using Web Hosting Hub.
First follow these steps to set up Putty and SSH via a command line into your Web Hosting Hub account.
You will need to generate several keys, so keep that window open. Once you have followed the instructions and have command line access to your hosting account, log in using the putty command line and configure your repo using the steps below. (Note: The putty command line will put you at the root of your hosting account. You will most likely need to move into the public_html directory and then your project directory. Example: cd public_html
Configure your repo
Once you have moved into your project directory make a new directory for git by:
Then go into that directory
Then type this git command:
git init --bare
git config receive.denyCurrentBranch ignore
Then you need to set the git hook that will refresh your working directory after you push to the server.Save this into /public_html/PROJECT_FOLDER/.git/hooks/post-receive.
# Save this in: /public_html/PROJECT_FOLDER/.git/hooks/post-receive
GIT_WORK_TREE=/home/USER_ID/public_html/PROJECT_FOLDER git checkout -f
I had to use an absolute path to make this work, but you can try a relative path too. Note: You may have to update your permissions on the post-recieve to 777 on your server.
Set up Your SSH Config Locally
Automate SSH connection for Web Hosting Hub. In order to add web hosting hub as a git remote, you need to automate the fact that you will be connecting to the server via port 2222 and you will not be using your password for authentication.
In windows 7 the SSH folder is located at C:\Users\USERNAME\.ssh. Create a file in that directory called config. Then add this content.
You should not be able to login. Just in case, close your CLI windows and open a new fresh one.
In your local project folder type
git remote add production USER_ID@your-domain.com:public_html/PROJECT_FOLDER
Ok you should now be able to push directly to your production server:
git push production master
If you want to be able to push to both your production server and a git repo in one command, check out this stackoverflow question.