blogblogblogblog SyKoHPaTh

Web Development From Scratch, Day 4

Alright, looks like my SSH request finally went through. Let's go ahead and set this up for today's lesson (yes I know you have your database "test" created - we'll get to that soon enough!)

First off, from your terminal...oh right, we're in Windows. Open up Git Bash. Now, from *this* terminal, type:
ssh @

So, for example, your username to ssh into your site is "Blah", and your site is "", your line will appear as so:

niiiiiice. Type in your password, and you'll be at a funky looking prompt, which varies *greatly* among hosting servers:>$

Yeah, weird, right? Type "ls". Chances are, there isn't much here. Probably your "www" folder (or some variant). "cd" into that, and you'll see all your web files. This is your "web root" directory, that is, it's the directory that appears when you type in "" into your browser. Note that it'll usually load "index.html" first. Ok, done poking around? "cd .." or "cd ~" to get to the home directory.

Now, the part I really can't help with here is making sure your host has Git installed and enabled. Again, since there's many hosting companies, and every one is different, they may or may not have it installed. A quick test is to type "git" at the command prompt. If it's installed, it will show you a list of commands, with something like "See 'git help '..." at the bottom. If so, YAY. If not, welp:

1) Google your webhost and "git". See if there's any easy way to get git on there.
2) If not, unfortunately you'll probably have to stick with ftp for now. (Get a better webhost! I suggest A2 Hosting - they have Git installed for sure!)

Keys. Sigh. Now, unless you feel like typing in your password every time Git or you connects via SSH to your server, you're going to have to make authentication keys.

1) If you're still on git bash, and connected to your webserver, type "exit" to disconnect.
2) Now, just to make sure, type "cd ~" to put yourself back at the home directory.
3) View all files with "ls -al"
4) look for a ".ssh" directory. If one exists, you'll want to back it up (or if you know you're already using it - say, you already set it up for git, you can use the same keys! - skip to if you want to use the same key)
5) You can refer to GitHub SSH Key Guide if you want to generate keys, or to make new ones (don't forget to add them to Git!).
6) Arguable, but leave the passkey blank so that you don't have to type it every time you git/login.
7) Open up "" by any means necessary. Well, you can do this directly through Git Bash. Type "cat". It'll display the contents of the file.
8) Now, to copy *from* Git Bash, Click the top-left window icon and Edit -> Mark, then drag a selection box over the text (make sure you cover *all* of the file contents!). Then press Enter.
9) Ok, ssh back into your webserver.
10) At your home directory, "cd .ssh". If the directory doesn't exist, create it: mkdir .ssh
11) In the directory, type: vi authorized_keys2
12) A really strange-looking setup appears.
13) Press i (when you do, the bottom of the window should say "-- INSERT--")
14) Paste into the window - with Git Bash, press the "Insert" key to do this. This should paste the entire "ssh-rsa" into the top line.
15) Press ESC (removes the "-- INSERT --" at the bottom of the screen)
16) Type :x
17) Press enter (":x" )
18) You're magically at the prompt again! whew that was scary!
19) confirm the file actually contains what you pasted by typing "cat authorized_keys2"
20) exit
21) Relog back into your ssh server - this time, it should log in without a password (however, it will ask for a passkey if you set one!)

And that's "it". Again, you only have to set this up once (unless you access from a computer with different keys!)


Ok, now, we have to somehow magically make Git push to your webserver. This is another one of those "setup once" things.

1) ssh to your server.
2) First, we're going to make a subdirectory on the web root, so that it will be accessible from http:///
3) cd into your webroot, which varies by server.
4) mkdir
5) pwd
6) With "pwd", it shows the full path to the current directory. Remember this! Again, it varies by server, and will look something like:
7) cd ~
8) Type: mkdir (create the folder within your home directory, different than the ".git" one in webroot)
9) cd
10) git init --bare
11) The home/ directory is now initialized with a "bare" repository. This is separate from your repository on, but we'll be pushing directly to this one.
12) Now, this next part is called the "hook". Remember the path you got from pwd? I hope you wrote it down!
13) vi hooks/post-receive
14) Press "i" for insert mode.
15) Enter in the following, replacing with the full path you got from "pwd"
GIT_WORK_TREE= git checkout -f

16) Press ESC, then :x
17) Verify the file with "cat hooks/post-receive". If it has what you entered, you're good so far.
18) chmod x hooks/post-receive
19) The file is now marked as "execute".
20) exit

We're back on the local machine for this next part. I promise we're almost done. Seriously.

1) Open Git Bash, if for some reason you closed it within the last 2 seconds of the previous section. "cd ~/Projects/"
2) Now, to setup our remote access and push to our webserver!
3) git remote add web ssh://@/
Note: Make sure it's pointing to the repository, and not the .git directory you made in your web root.
- if you get "repository not found", you may have to set it up as: ssh://@/~/
4) git push web master:refs/heads/master
Note: this pushes the master config and goodies and all that - you only have to type "git push web" for normal use

Note that if you get an error...tough! Well actually, google the error. It helps to google your host, git, and error message, such as "godaddy git git-receive-pack:command not found". I apologize if you're using GoDaddy. They don't have git installed, and are kind of a pain to get working. However, I'm nice, and managed to run across this post when I tried that search: GoDaddy git-receive-pack error fix

5) Everything should be happy, but, there's only one way to be sure!
View your work at: http:///
For me, that's:

Hey look at that...I'm Awesome. Super Awesome.

Now, just to make sure, we have 2 "remotes" setup. Simply type "git remote" to view all of them. There should now be one for "origin" and one for "web". Origin points to your repository on github, while web points to your website. With this process, you should be able to setup a "dev" and "live" environment. I'll explain that setup in a separate tutorial. For now, be happy you can push to git and your website!

And finally from now on - after you commit your changes, all you have to type is "git push origin" or "git push web".

php, tutorial, git, github