Skip to content

Push Git Repo Into Shared Hosting Account Like Hostgator

by arlo on January 28th, 2012  Tip

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

  • atuline

    I’ve been reflecting on this article for quite some time now, and it’s only in the past few days that I’ve come to realize that you’re not just pushing to a Hostgator repository, which you can easily do if you have created a ‘bare repository’ on your Hostgator account. Rather, you are pushing directly to a ‘non-bare’ repository which could be a staging or public web server. This is a much more difficult thing to do, and one I didn’t think was possible. As a result, if you’re a lone developer, you don’t need a bare repository at all. You would just use a non-bare repository for your development workstation, and a non-bare repository for the public web server(s).

    My recommendation is that you spend a bit of time at the beginning of the article making that ‘cut out the bare-repo middleman’ difference in architecture a little clearer for any of us noobs.

    Combining this technique with git hooks for database migration allows me to move WordPress or Drupal based web sites between hosts with different URL’s much easier.

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

    Thanks for commenting! I am glad this post has helped. You are totally right, I should make a distinction between common hosted bare repos and the purpose of this tutorial. If you have any other suggestions feel free to let me know.

  • designerbrent

    Even though I’ve known about this since you published the article, I FINALLY tried it and man! Where I have I been?!? This is great! Thanks…

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

    Hey! Glad it was useful!! If you are involved in teams projects I have a project that will automatically deploy depending on switch branch you push to. I will be blogging about that one soon.

  • designerbrent

    That’s great!

  • designerbrent

    No team right now, but I’ll keep it in mind. Took me a year to circle back to this incredibly helpful tidbit but man is it useful!

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

    Hey Adam! I finally open sourced this little script I’ve been using to push into github and automatically update my shared hosting. Check it out:

    http://mexitek.github.com/myWebHook/

  • http://www.residr.com/ Adam N.

    Cool! Thanks for letting me know!

  • OwnSourcing developer training

    Great tutorial. Nice of you to solve one of HostGator’s issues so thoroughly! (Git ups their level of professionalism significantly, as far as we’re concerned.) Excellent!

    Two very minor things that might make this more useful for people who are new to git:
    - add an instruction to ‘Configure your repo on hostgator’ to create repo directory and run git init before running config option
    - note that the first push will probably require the remote branch name e.g. git push hostgator-live master (subsequent pushes probably won’t require it again)

    Thanks for working up such clear, comprehensive instructions. We’ll definitely be sending new developers here.

  • Senff

    I get this error when I use anything that starts with GIT:

    senff@senff.com [~/www/subdomains/wordpress]# git config receive.denyCurrentBranch ignore

    error: could not lock config file .git/config: No such file or directory

    Ideas? I thought it should work since you say Hostgator should already have git?

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

    Yes it should work. The fact that you get the error means git is installed.

    My first guess would be that ~/www/subdomains/wordpress is not actually a repo. Not really sure unless I can get access or we can screen share. Email me: arlo.carreon@gmail.com or add me to skype under arlo.carreon.

    Also I recently released a opensource git webhook that would make this easier, if you don’t mind hosting your git repo in github or bitbucket. It might be worth checking out.

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

  • David

    Thanks for this awesome article. FWIW you can bypass hostgator support chat to enable SSH here: http://support.hostgator.com/articles/hosting-guide/lets-get-started/how-do-i-get-and-use-ssh-access#connect

  • http://www.facebook.com/budi.prakosa Budi Prakosa

    is it working with hostgator shared hosting? because php exec is disabled

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

    yup works for me. i use it everyday actually. it’s odd that exec would be disabled on HG. fairly new account? new restrictions?