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 to go to the support chat and ask for SSH access, 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, 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.

<br />
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.jotorres.com/ Jotorres

    Hey man, thanx for the nice tip.  I’ll be sure to try this on my website by tomorow.  Gotta love GIT!

  • Szilard Szabo

     Hi Arlo,

    I am kinda new with git, so I find this post very very useful. Thanks so much!
    I did everything like you wrote here and worked fine.
    Just one thing I don’t understand: I added some new files locally to my repo and then
    I did “git push hostgator-live”  and then I checked my hostgator repo directory but the new files didn’t show up there! Later I tried a “git reset –hard” on hostgator and it helped, which is fine, I just don’t understand why i have to reset the git to make the files show up?

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

    Hey, glad it helped!

    How to Fix:
    I mentioned that a hook needs to be created in hostgator AND you need to change permissions for that file in order for that hook to be executed automatically. The command for that is: “chmod +x path/to/hook”

    Why Do I need this?:
    On hostgator your repo has an actualy working directory. When you push, you commit is accepted into the change log, but that is it. A push does not “man handle” your working tree. Remember git has 3 areas: working tree, staging and commit log. When you use github.com or bitbucket.org those repos are “bared” repos, therefore not having a working tree and thus not having this issue.

    I hope that explains the problem. If not please do not hesitate to ask again. Thanks for the comment.

    Also, feel free to follow my podcast at: http://dev1.tv

  • Qwertymk

    I was looking to do this for a while. I even open a SO question about this. Thanks

  • http://chrisblackwell.me/ Chris Blackwell

    Whenever I try to do a pull on the remote repo, I get the following error:
        warning: no common commits    remote: Counting objects: 2598, done.    error: git upload-pack: git-pack-objects died with error.    fatal: git upload-pack: aborting due to possible repository corruption on the remote side.    remote: fatal: unable to create thread: Resource temporarily unavailable    remote: aborting due to possible repository corruption on the remote side.    fatal: protocol error: bad pack header

    Any idea what I could be doing wrong?

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

    Sounds like your remote repo and the location you are attempting to pull from do not have the same history. You need to do a “git clone” or “git push” to init your remote. 

    I had a similar problem when I took an existing git repo, then ONLY copied to working directory to a separate location and did a “git init”, then tried to pull.   Unfortunately, this didn’t work how I imagined.  Even though the same files existed in both locations, git goes off of the history to know what it should merge.

    Sound like this might be your issue?

  • Johnny5

    Sigh. . . Doesn’t appear to work with a reseller account on Hostgator that has SSH access.  Even tried:

    find ~/ -name “*.pub” -print

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

    In theory if you can SSH into the machine (and git is installed) you can do these steps.

    You shouldn’t have a *.pub file on your reseller machine. id_rsa.pub file would be your public key on your workstation. Copy the contents into ~/.ssh/authenticated_keys file on Hostgator.

    id_rsa.pub (your comp) COPY INTO ~/.ssh/authenticated_keys (on hostgator)

    I see how my wording in this post is confusing.  Let me know if I identified your issue correctly.