The Perfect Workflow, with Git, GitHub, and SSH
videos

The Perfect Workflow, with Git, GitHub, and SSH

In this lesson, we’ll focus on workflow. More specifically, we’ll use the helpful GitHub service hooks to automatically update a project on our personal server whenever we push updates to a GitHub repo.


Prefer a Video Tutorial?

Press the HD for a clearer picture.
Subscribe to our YouTube and Blip.tv channels to watch more screencasts.

Step 1 - Create a Git Repo

We certainly need some sort of project to play around with, right? Let’s do that right now. Using which ever tool you prefer (I’d recommend Structurer), create a new directory, called awesomeProject, and add an index.html file. Feel free to populate this with some gibberish markup for the time being.

With our test directory in place, let’s create our first Git commit.

If you’re unfamiliar with Git, I highly recommend that you first review “Easy Version Control with Git.”

Open the command line:

cd path/to/awesomeProject
git init
git add .
git commit -m 'First commit'

Those familiar with Git should feel right at home. We’re creating a Git repo, adding all the files to the staging area, and are then creating our first commit.


Step 2 - Uploading to GitHub

The next step is to upload our project to GitHub. That way, we can easily call a git pull to download this project from any computer/server we wish.

Again, if you’re not familiar with GitHub, and haven’t yet created an account, read Terminal, Git, and GitHub for the Rest of Us.

Begin by creating a new Git repository.

Create a Git Repo

Next, you’ll need to fill in some details about your project. That’s simple:

Details

And finally, since we’re already working with an existing Git repo, we only need to run:

git remote add origin git@github.com:Your-Username/awesomeProject.git
git push -u origin master
Git

With that out of the way, our awesomeProject is now available on GitHub. That was easy!


Step 3 - SSH

Now, we certainly need some sort of live preview for our project, ideally stored on our own server. But this can be a pain sometimes. Push your updates to GitHub, login to your server, manually transfer the updated directory, etc. Granted, this only takes a moment or so, but when you make multiple changes through out the day, this can quickly become a burden.

But one step at a time. We’ll tackle this dilemma in Step 4. For now, let’s simply pull in our Git repo to our server. To do so, we need to SSH in.

Depending upon your host, your SSH credentials will vary slightly. Search Google for “your-host-name SSH,” and you’ll surely find the necessary instructions. Once you’re ready, let’s move along:

We’ll use my personal server as an example:

ssh jeffrey-way.com@jeffrey-way.com
<enter your host password>

And with those two lines, we’re in!

SSH

Next, we cd to the parent directory of where we wish to store awesomeProject. For me, this will be: cd domains/demo.jeffrey-way.com/html/. Of course, modify this according to your own directory structure.

Git Clone

Let’s clone the GitHub repo now.

git clone git@github.com:Your-User-Name/awesomeProject.git

Give that command a few seconds, but, before you know it, that directory is now available on your server, and, in my case, could be viewed at: http://demo.jeffrey-way.com/awesomeProject.


Step 4 - Creating a Connection

The inherent problem at this point is that there’s no specific connection between our GitHub repo and the directory stored on our server — at least not an automated connection. For example, if we update our source files on our local machine, and then push the changes to GitHub:

git add index.html
git commit -m 'Added photo of dancing chicken'
git push origin master

These changes will certainly not be reflected on our server. Of course they won’t! In order to do so, we must – once again – SSH into our server, cd to the awesomeProject directory, and perform another git pull to bring in the updated source files.

Wouldn’t it be great if, every time we pushed updates to GitHub, those new source files were automatically updated on our live preview server?

As it turns out, we can do this quite easily with GitHub service hooks.

GitHub Service Hooks

You can access this page by pressing the “Admin” button from within your GitHub repo, and then clicking “Service Hooks.” The “Post-Receive URL” option will instruct GitHub to send a POST request to the specified page every time you push to your GitHub repo. This is exactly what we need!

“We’ll hit these URLs with POST requests when you push to us, passing along information about the push.”

To make this work, we’ll need to create one more file that will handle the process of performing the git pull. Add a new file, called github.php (or anything you wish – preferably more vague), and add:

<?php `git pull`;

So now you’re thinking: “Jeff’s gone crazy. You can’t put a Bash script into a PHP string.” Well…yes you can, once you realize that those aren’t single quotes above, they’re back-ticks.

When you wrap a sequence in back-ticks, in PHP, it’ll be treated as a Bash script. In fact, it’s identical to using the bash_exec function.

Save that file, and upload it to the awesomeProject directory on your server. When finished, copy the url to that file, and paste it into the “Post-Receive URL” textbox. In my case, the url would be http://demo.jeffrey-way.com/awesomeProject/github.php.

With this in place, every single time you push to your GitHub repo, that file will be called, and the awesomeProject directory on your server will auto-update, without you needing to move a finger. Pretty nifty, ay?


You Also Might Enjoy:

Add Comment

Discussion 87 Comments

Comment Page 2 of 2 1 2
  1. Shaun says:

    I can ssh into my domain, but once I run any git commands it says “command not found.” Please excuse me if I missed something obvious. This whole command line stuff is totally new to me.

    • Shaun says:

      To be more specific, the problem happens when I type in “git clone …” which it then responds with “command not found.”

      • Ian says:

        It sounds like you don’t have git properly installed. Run a git –version and/or git help. If those still return command not found, then you most likely don’t have git installed properly. Look into how to install git onto your server.

  2. Irfan Zaheer says:

    Thanks for the great post, really wonderful explanation.
    But just having some difficulties with command git pull.
    I mean when i manually execute this command on SSH it asks for pass-phrase and after verifying its update the code.
    How can i avoid this pass-phrase entering step, & may be this is the primary reason that when i do push from my local computer, it does updates on github but unable to do it on my own server?

    Please if any body come across same problem, please help!

    Thanks.

    • Todd says:

      Irfan,
      In order to use ssh without password you will need to generate ssh keys. This will typically be the same process on most servers/hosts, but will vary depending on whether you are using osx/linux/windows. This might be of assistance: http://oreilly.com/pub/h/66

  3. Ryan says:

    Jeffrey Way is a clueless hipster. The recent surge of moronic, bikeshedding JavaScript developers are also clueless hipsters. DIAF please.

  4. Met says:

    Very nice workflow,
    but how do you manage those files where local version differs from online (eg: db settings)?

    Great tut, anyway (as always).
    Met

  5. It’s really a nice and useful piece of information. I am glad that you just shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

  6. Aron says:

    Very nice tutorial.

    We made a similar video demo (or tutorial if you like) for our internal team about the Integrator Manager workflow. Our developers have been using SVN for long years before converting to Git, and it was a major conceptual change that required multiple tutorial sessions like this. You may also find it worthwhile: “Git Workflows Demo: What is the Integrator Workflow?” http://www.youtube.com/watch?v=67zQHNO2gaQ

  7. Niels van Aken says:

    Won’t the Github passphrase for your ssh key be a problem?

Comment Page 2 of 2 1 2

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.