Skip to content

Push Git Repo Into Shared Hosting Account Like Hostgator

by arlo on January 28th, 2012 

Many use 3rd party git hosting (Github.com) to host their git repos, but sometimes you’re not wanting to take your project to a social network. Sometimes your repo is for yourself, for your website and no one else. I want to run you through how to setup your Hostgator account with the configuration needed to push directly into Hostgator in 3 steps. Theoretically you can do this with any shared hosting that gives you SSH access and has Git Client installed.

1.) Setup Hostgator

Make sure you have SSH access. Shared hosting accounts with Hostgator do not come with SSH access by default. You will have to go to the support chat and ask for SSH access or go here and ONLY follow instructions for “Shared Web Hosting”, this will only take minutes and is effective immediately.  Once they give you access, make sure you can login. SSH port for hostgator is 2222. Here is how you would SSH into your account with your Hostgator username and password.

ssh USER_NAME@your-domain.com -p 2222
...
Enter your password: *********

Get your public key into Hostgator.Your public key is located at ~/.ssh/id_rsa.pub (on your workstation), copy the contents and paste it in a file on Hostgator: ~/.ssh/authorized_keys.

Incase you don’t have ~/.ssh/id_rsa.pub on your workstation, run ssh-keygen in your console and hit enter all the way through. At the end of that, you will have the ~/.ssh/id_rsa.pub file on your computer.

If ~/.ssh/authorized_keys does not exist on Hostgator, then create it with touch ~/.ssh/authorized_keys.

Configure your repo on hostgator. First, you need to set a config option in your hostgator repo to accept pushes into this working directory. SSH into hostgator, navigate to your repo directory and run this command:

git config receive.denyCurrentBranch ignore

Finally, you need to set a git hook that will refresh your working directory after the push has been accepted. Save the following content in: PATH_TO_REPO/.git/hooks/post-receive.

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

Then, make it executable with chmod +x PATH_TO_REPO/.git/hooks/post-receive

2.) Setup Your SSH Config Locally

Automate SSH connection for Hostgator. In order to add hostgator as a git remote, you need to automate the fact that you will be connecting to hostgator via port 2222 and you will not be using your password for authentication. Your need to edit/create the file ~/.ssh/config on your computer. Then add this content.

Host your-domain.com
  Port 2222
  PreferredAuthentications publickey

Now, you should be able to login with ssh user_name@your-domain.com without needing to type your password. If this is not the case, start over at step 1.

3.) Setup Hostgator as a Remote

Add your domain as a remote. On your computer, navigate to your git repo and run this command:

git remote add hostgator-live user-name@your-domain.com:www/site-directory

Now, you can make you changes to your repo and when you are ready to push your changes:

git push hostgator-live

Something to think about

With this setup it is now easy to have a “dev” site on your hostgator account too. SSH into hostgator and replicate your repo into a different folder (~/www/dev). Locally, setup a new remote:

git remote add hostgator-dev user-name@your-domain.com:www/dev

So, from now on you can git push hostgator-dev to test out your new changes. When you are ready to make your changes live: git push hostgator-live

From → git, Workflow

  • http://www.arlocarreon.com Arlo Carreon

    Glad it was helpful!

  • ETheRed

    Thanks for the quick reply Arlo, there is something else I will like to confirm with you that I spent quite some time trying to figure what to do.

    On the step called “Configure your repo on hostgator” where I am suppose to copy the mentioned content into the “post_receive” file, well that file did not exist for me in the “hooks” directory. I end up creating the file, was that ok? or was that file suppose to be there already?

    The only files in the “hooks” directory where the following:
    - applypatch-msg.sample
    - commit-msg.sample
    - post-update.sample
    - pre-applypatch.sample
    - pre-commit.sample
    - prepare-commit-msg.sample
    - pre-rebase.sample
    - update.sample

    Thank you

  • http://www.arlocarreon.com Arlo Carreon

    You should be ok creating it yourself. Make sure you give it executable privileges. “chmod +x path/to/post-receive”

    As a side note, this post is 2 years old I haven’t looked at possible changes to these git hooks. If post-receive doesn’t work I would suggest using post-update.

  • lettuce

    Hi I am new to publishing a website, it seems like I followed all the steps and it pushes successfully to hostgator repo, but how do i view the website? I used handlebars instead of .html

  • Otto Salin

    I should try that out asap. Thanks! ;)

  • Agus dk

    I have little problem. I want to my friend can pull or clone my repo HG but i want my friend have personal username not my existing username. HG can support to add new username ? Please help me.

  • http://www.arlocarreon.com Arlo Carreon

    For this setup the “username” is based on SSH credentials. I understand you don’t want your friend to have the same SSH credentials as your account. I would suggest not to follow this guide and to use a web hook.

    http://mexitek.github.io/myWebHook/

  • Agus DK

    thanks for reply and your suggestion, in the future i dont want use github again, because i want use this private. you have other suggestion for this case?

  • http://www.arlocarreon.com Arlo Carreon

    You can still use the webhook with bitbucket. Bitbucket has free unlimited private repos.

    https://bitbucket.org/

  • http://www.arlocarreon.com Arlo Carreon

    The webhook I mentioned above also works with BitBucket which has unlimited free private repos.

  • http://www.rhoenaleister.com/ jacobirr

    Thanks for such a great article. I’ve got all this setup and in place, and I can even see my commits working in the log file on hostgator… The only issue I’m having is that my files are not updating. I use git add index.html then git commit -m “testing index change” and then I do git push hostgator-live …so I get the log entry, but the index.html file is still the old version… any ideas why?

  • http://www.rhoenaleister.com/ jacobirr

    I also just found this error when trying to push:

    remote: fatal: cannot exec ‘hooks/post-receive’: Permission denied

  • http://www.arlocarreon.com Arlo Carreon

    Yup. That post-receive hook is the one responsible for updating the files after a push. It seems that the permissions on ‘hooks/post-receive’ are not set so that it is an executable file.

    Try:
    chmod +x /path/to/hooks/post-receive

    If that fails try:
    chmod 777 /path/to/hooks/post-receive

  • pstrawberrie

    Thank. You. So. Much!!!!!!!!
    Saved me so much time I feel like a pro after reading this! ^_~