3

SOLUTION BELOW - How to use git to push to cpanel server

I finally got somewhere with setting up Git between my localhost (WAMP setup on Windows 8.1) and my Linux server (CentOS 6.6 x64 with cPanel 11.46.2).

Locally I created a bare clone: git clone --bare my_project my_project.git

NOTE: my_project is an example name, not the real name, and from this doc here: http://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server

I copied the my_project.git folder to my server's root directory /home/myuser/public_html/

so now in the root directory I have:

  • cgi-bin
  • my_project.git

This is one area I am unsure of. Do I have to do an init (using putty) on my server in the public_html directory? I read something about a bare init? I just want to push (from my PC) the website I already have under Git control, to the server. When I make a change to 1 file, push that change to the server so it's updated live with a push. The website is DONE and ready to be live. I have already manually moved back and forth for live testing on the server. My last step is to get the Git setup correctly, so any further changes I can just push them to the server without the need of FTP.

I added a remote origin: git remote add origin ssh://myuser@thedomain.com/home/myuser/public_html/my_project.git

I tried to push to it, and got "Permission denied (publickey)". I already had an id_rsa and id_rsa.pub key locally on my PC, so I copied them and renamed them to id_rsa.myname id_rsa.myname.pub (where myname is my first name). I then copied them to the .ssh folder through FTP (FTP as cpanel user, and it's the directory your dumped into, above public_html), same as /home/myuser/.ssh/ directory.

Once they where there, I added them to 'authorized_keys' using Putty logged in as the cpanel user (my private ppk) by doing:

cd .ssh
cat id_rsa.myname >> ~/.ssh/authorized_keys
cat id_rsa.myname.pub >> ~/.ssh/authorized_keys

After doing that, a push appeared to work. Because I was having a key/auth issue, I used Git Gui version, which was setup and worked fine locally. I added the origins through Git Bash though. When I did "Remote > Push" in the Gui version, I got:

Pushing to ssh://theregistrybank@theregistrybank.com/home/theregistrybank/public_html/yiire    gistrybank.git
stdin: is not a tty
To ssh://myuser@thedomain.com/home/myuser/public_html/my_project.git
44ae034..0388a05  master -> master
updating local tracking ref 'refs/remotes/origin/master'

Before doing the push, the only file modified (diff from the bare clone I transferred to the server) was my .gitignore file. I added 2 more exclusions to it, and committed it locally. So I was trying to push the change in that file. After I did the push, it said "success" in green and appeared to work. However, when I check the file in FileZilla, the .gitignore file is not the updated one that I just committed locally.

I think I am close, but missed a step somewhere. I tried to be as descriptive as possible.

And putting the source on GitHub is not an option as the client does not want the source public, and does not want to pay for the private repos. I should be able to push from my local setup to the cPanel server so I don't have to transfer thousands of files every time. I actually transfer a zip file, and unzip on the server lol.


Server Info

  • cPanel 11.46.2 build 0
  • CentOS 6.6 x86_64 kvm build01

Yes, Git is setup on the server, and working, and git --version reports:git version 1.7.1

Git on my PC: git version 1.9.4.msysgit.2

Thank you in advance.


SOLUTION

Thanks to @VonC I was able to get this to work :)

You need somewhere for your git repo to sit. I created a 'git-repos' folder in '/home/cpaneluser/git-repos' to house my repos for this cpanel user.

First step is to create a bare repo: http://git-scm.com/book/en/v2/Git-on-the-Server-Getting-Git-on-a-Server - I only followed the first step, basically created the bare repo 'my-project.git'

Before putting it on your server, rename 'my-project.git/hooks/post-receive.sample' to just 'post-receive' so it will be ran. Edit it with your editor, and add the line that @VonC gave us in his chosen answer:

#!/bin/sh
umask 0022
GIT_WORK_TREE=/home/cpaneluser/public_html GIT_DIR=/home/cpaneluser/git-repos/my-project.git git checkout -f

Note: I am using cpanel, so your path's may be different, and your umask could be different. 0022 is for 0644 file permissions. Without the umask, I was getting 500 Internal Server Errors, because the files were created with 0664 permissions instead.

Using FTP or whatever you like, copy the 'my-project.git' bare repo to your server to '/home/cpaneluser/git-repos'. Then go into 'my-project.git/hooks' and change the permissions of post-receive to have execute permissions. For me, 0744 worked fine. This was the magic sauce :)

Locally, add your remote (must be in your git project): git remote set-url origin ssh://cpaneluser@yourdomain.com/home/cpaneluser/git-repos/my-project.git

Now if you try to push now, it won't put the files in 'public_html' because the git tree (terminology?) matches and is up to date. If they are up to date, it seems to skip over executing your 'post-receive' hook. That means your bash script never ran, and it never checked out the files to your working tree.

We need to manually run the 'post-receive' bash script to create all the files of our project in the 'public_html' directory.

cd to '/home/cpaneluser/git-repos/my-project.git/hooks'

Run: ./post-receive

Boom, all your files are in 'public_html'. Now you can work locally, then push to your cpanel server as expected :)

Community
  • 1
  • 1
Wade
  • 3,757
  • 2
  • 32
  • 51

2 Answers2

2

I had a similar problem with my cPanel account, git was set up but for some reason I couldn't push to it. After a lot of head-banging I realized that the root of the problem is like you pointed out that although git is set up, the repo is empty so there's nothing for it to track. This happens when you set up an empty repo in cPanel and then try pushing a local repo that you've already created, as opposed to cloning it first from github or your local repo (which is what happened to me because for some reason I couldn't access my github from the cPanel git interface even though I had set up a SSH key)

The simple solution that I found is to manually clone the repo using the terminal in your cPanel account

On your cPanel dashboard, under the "advanced" section, you'll find the terminal. You'll get a warning saying that you could mess up your server if you don't know what you're doing, click ok and you're in.

Now you just have to clone your repo the same way you would if you're cloning onto a local machine.

Navigate to where git was set up in your cPanel

cd repositories/<nameOfYourRepo>

And run the clone command

git clone <URLofYourGithubReop>

You'll be asked for your github username and password if it's a private repository

And that's it, you're good to go

Naftoli Ost
  • 91
  • 1
  • 5
1

What you have copied (my_project.git) is a bare repo, meaning one without a working tree (the actual checked out files).
Read for instance "Git workflow - Setting up a build process".

That means pushing to if won't change anything in /home/myuser/public_html/

The missing piece is a post-receive hook (in /home/myuser/public_html/my_project.git/hooks/post-receive, make sure it is executable: chmod +x), in order to checkout the repo in /home/myuser/public_html/.

#!/bin/sh
GIT_WORK_TREE=/home/myuser/public_html GIT_DIR=/home/myuser/public_html/my_project.git git checkout -f
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Note: git 1.7.1 sounds ancient: you can upgrade it (http://stackoverflow.com/q/20111476/6309) – VonC Jan 16 '15 at 06:41
  • Thanks, and yes the version number appeared old, but didnt look into it. Thought maybe it was a variation in libraries and their independent versions. I will follow your instructions, and see I can get them to work. I will report back in a bit :) – Wade Jan 16 '15 at 07:00
  • In the "Git workflow - Setting up a build process" link you gave, I gathered that I need to cd to the "public_html" directory and run a git pull. It also mentioned to unset GIT_DIR. I created a perl script, that chdir() to 'public_html', and used system() to execute the 'git pull /home/myuser/git-repos/my_project.git' - Note: I moved the my_project.git out of the web directory into 'git-repos'. My question is, do I need to do that, ie: create post-receive.pl to do what I just said, or do I just need to use a bash script with the 1-line defining GIT_WORK_TREE and GIT_DIR ? Thanks again! – Wade Jan 16 '15 at 08:24
  • @WadeShuler no pull necessary: I have written a possible command in the answer below. A checkout is enough since `public_html` is *not* a git repo. Don't use perl here: the simple bash script is enough. – VonC Jan 16 '15 at 08:27
  • I didn't see the checkout -f at the end.. makes more sense.. brb – Wade Jan 16 '15 at 08:29
  • I renamed post-receive.sample to post-receive.sh and pasted in what you gave with GIT_WORK_TREE and GIT_DIR. I obviously changed the names and path accordingly. I modified a file locally, committed it, the did "Remote > Push" with Git Gui version. It said it was a Success and looked good. However when I refresh the public_html directory, the files are still not there. – Wade Jan 16 '15 at 08:38
  • @WadeShuler is your `post-receive` file executable? – VonC Jan 16 '15 at 09:11
  • Yes, for user, 0744 It contains the 2 line bash file you provided, and that's it. Should it contain more? When I push from my end, it says success, but does not put any files in the public_html directory. Do I have to put them in there, or will it create them if they don't exist? – Wade Jan 16 '15 at 09:14
  • You can try and execute that line yourself on the server, just to see if it works: `GIT_WORK_TREE=/home/myuser/public_html GIT_DIR=/home/myuser/public_html/my_project.git git checkout -f` – VonC Jan 16 '15 at 09:17
  • @WadeShuler you can also go to `/home/myuser/public_html/my_project.git` and do a `git log` in it, to see if your last commits were actually pushed in that repo. – VonC Jan 16 '15 at 09:18
  • YES! Thank you so much! I think the problem was that I named it 'post-receive.sh' instead of just 'post-receive'. I did make it executable, just was the wrong file name. I was also trying to avoid doing 100 test commits locally to push to the server. I modified the .gitignore file, comitted it, then push. To spare some, I just did a 'git push' without modifying files. It seems it wont do anything if there is nothing to do. I thought it would still run the hook to move the files, but it didn't. So a real edit, commit, push, was needed (once named properly). – Wade Jan 16 '15 at 09:29
  • I got it working, but you have to edit a file (causing a pointless commit), then push, before the files get put into 'public_html'. A 'git push' does not trigger the creation of the files IF the repo is up to date. Is there a command to run to essentially "build" the files of the current repo. You mentioned running the command used in the bash script of 'post-receive'. So I would run that manually the first time to "build" the files. Is that the only way, or is there a git command to pull this off? – Wade Jan 16 '15 at 10:17
  • @WadeShuler no, if you want to checkout those files directly, then you need to run the post-receive script yourself. Otherwise, it will run only when new commits are pushed. – VonC Jan 16 '15 at 10:18
  • Only problem I am having is, after the checkout and the files were copied, I got a 500 internal error. Seems I then have to edit index.php and save, and it then works. I am wondering if this is a bug with the old git version installed. I am trying to update cpanel but having problems. WHM 11.46.2 comes with Git 2.2.0. I ran yum install git earlier and maybe that messed things up? I did yum remove git, yum install git, yum update git.. stil seeing 1.7.1. There are RPM's with Git 2.2.x on it, this cpanel version comes with 2.2.0, so why the heck is it installing 1.7.1 – Wade Jan 16 '15 at 18:12