One of the harder parts about development is the workflow. There’s no one right way to do it. What works for one person or team may not be efficient for you. For this reason it’s not something that is widely taught. You generally have to find your own way of doing things.
Today we’re talking about adding git into your workflow. If you already aren’t using version control and developing locally, you need to start now. You’ve probably been told time and time again, but seriously stop it. Don’t wait until something terrible happens before you change. There are plenty of resources (Like try github), which make it easy to learn.
Once you’re actually using git, how do we deploy sites?
If you are working on a server that you do not have ssh access or git isn’t installed, you may want to look into solutions like git ftp and git tower. These programs make it easy to use git and take care of pushing changes live. If you do have the appropriate access, you can look into a workflow like the one that net tuts has put out. It’s a nice solution, but there’s a simpler way to do it without the use of GitHub (read: free).
General idea of what we’re doing:
Because we can only push to a bare repo, we need to create two repos on our server. The first, our hub, will be our bare repo. A bare repo is one without a working tree, i.e., it contains none of your working files. We will push to this repo, and on a hook, have our production repo pull from the hub. Our production repo, which we will name prime, will contain our live production code.
Setting up the server
The first thing we need to do is get onto our server. Find out your SSH settings from your host and open up the command line:
$ ssh email@example.com -p 22
“–p 22” defines which port you are connecting to. If your host is not using a custom port for SSH connections, you can leave this part off. Entering our our password everytime we connect to our server costs time, so it is a good idea set up an SSH key. I will not go into that here, but a quick google search will give you instructions on how to set this up.
Creating our repos
We are going to navigate to the root of our server and create a folder named git to hold our hub repo:
$ cd $ mkdir git $ cd git $ git init –-bare
Now let’s create our production “prime” repo. In our case this is going to be in the public_html directory.
$ cd ~/public_html $ git init
Simple enough. We also need to add the hub repo as a remote:
$ git remote add hub ~/git
Creating our hooks
First, we’re going to create our hook that will run when we push to the hub. Let’s move into our hooks folder and edit our hook.
$ cd ~/git/hooks $ nano post-update.sample
Now paste this into the file above the exec command. If you don’t want to use the command line, you can always FTP to the file and change it there. When you push to the hub, this script will navigate to our prime repo and pull the changes from the hub.
echo echo "**** Pulling changes into Prime" echo cd ~/public_html unset GIT_DIR git pull hub master
Get out of nano, and rename our hook file to activate it.
^X $ mv post-update.sample post-update
Getting our local repo ready
If you haven’t already done so, set up the local repo. On your local machine:
$ cd /your_project_directory $ git init $ git add . $ git commit –m "Our first commit"
Alternatively, if your site is already live we can clone our live repo. On the server:
$ cd ~/public_html $ git add . $ git commit -m "Our first commit" $ git push hub master
On the local machine:
$ cd /your_project_directory $ git clone ssh://firstname.lastname@example.org:22/~/public_html
Again, the “:22” is for custom ports and is optional.
Now let’s add our remotes.
$ git remote add hub ssh://email@example.com:22/~/git $ git remote add production ssh://firstname.lastname@example.org:22/~/public_html
Everything is set up-Let’s push to the server.
Now that we have the set up out of the way, it’s easy sailing from here on out.
$ git push hub master
On no, the live files have been edited!
We don’t get a one line command to fix this, but it’s still fairly easy.
On your web server:
$ cd ~/public_html $ git add . $ git commit –m "Live files were edited"
$ git pull prime master
Have questions? Contact us and we’ll help you figure it out!