Skip to content

Instantly share code, notes, and snippets.

@robertosanval
Forked from Integralist/remote-git.md
Last active December 15, 2015 16:49
Show Gist options
  • Save robertosanval/5292100 to your computer and use it in GitHub Desktop.
Save robertosanval/5292100 to your computer and use it in GitHub Desktop.

Set-up remote git repository on a cPanel server

These instructions are an adaption of these articles for my particular needs I hope this could help somebody else's:


The first thing to do is to install Git on the cPanel server.

  • sudo yum install git-core

Once you do that the rest of the process is split into three sections:

  1. Server set-up
  2. Local set-up (push commits)
  3. Server (pull commits)

Server set-up

Note: First of all you need to have SSH access to your cPanel account.

  • ssh username@xxx.xxx.xxx.xxx (this is you connecting to your remote server)

Note: Your web directory is public_html then move up one level from there.

The remote repository you’re going to push to will be a separate directory than the actual you're deploying. Locally, the repository is always the code directory, but for remote deployments, we need to do things a bit differently.

  • mkdir repos && cd repos
  • mkdir your-project.git && cd your-project.git
  • git --bare init

We’ve now created a bare repository on our web server that we will use as our remote location.

The next step is to setup our post-receive hook. This is a script that will run every time you push code to the remote repository.

  • vi hooks/post-receive

Now, put this code into your post-receive file (replacing the path with the full path to where you want the code deployed).

  • #!/bin/sh
  • GIT_WORK_TREE=/home/username/public_html git checkout -f

After that’s done, you’ll need to give the post-receive file execute rights so it’ll actually do the work:

  • chmod +x hooks/post-receive

Local set-up (push commits)

  • cd ~/Sites/myWebsite
  • git init
  • git add *
  • git commit -m 'Start of new project'
  • git remote add origin ssh://username@xxx.xxx.xxx.xxx:xxxx/~/repos/your-project.git
  • git push origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment