A really easy Git workflow for site development

Stop with that FTP non-sense and setup a real development workflow using Git.*

I’m fairly new to Git. I’ve only been using it in a team setting for about 6 months. But it has quickly become an indispensable tool. At work, the Git environment is already setup when I get to a project. I just clone the repo to my local machine and off I go. Each push I do goes up to the origin repo and automatically gets deployed to a live test server and domain. It’s simple and amazingly helpful for a couple of reasons.

  1. I instantly get to see my work on a live server instead of just on my local server. Deployment is taken care of.
  2. I can share a link to the password-protected preview site with the client for review. And all by just typing: ‘git push’ It’s like magic, basically.

Except, it’s not magic. I’m going to show you really quickly how to get a simplified version of the same git workflow setup for yourself. This is what I use for my personal development projects.

The philosophy

The actual steps to doing this are pretty easy, the harder part is wrapping your head around the idea of how it works. Really, all you’re doing is initiating a bare repo on your server that you will only use to push/pull from. You need a bare repo for this because it will avoid a ton of conflicts later. Git was created to help you avoid conflicts! A bare repo doesn’t maintain a working tree, meaning you won’t be making commits to it directly like your local repo. Again, you will only push to and pull from this bare repo.

These are the steps to get your git workflow flowing:

  1. Create a bare repository on your remote server, which you will push to from local and pull from for the live preview site.
  2. Clone the bare repository to your local development environment.
  3. Clone the bare repository to another directory on your remote server that’s mapped to a preview domain

 

I’m going to assume you have the following:

  • A remote server with Git installed
  • A local development environment with Git installed
  • A domain you can use for the preview site
  • A basic knowledge of Git (if not, go here: http://git-scm.com/)

 

Regarding Git on the server

In most cases, if you’re using a shared server Git will probably already be installed. Those of us newer to developing for the web often start by buying shared server space. This is what I’m doing. I use Dreamhost (because it’s cheap), which already has Git installed for me. What I’m saying is, if you’re using shared hosting space (Dreamhost, Bluehost, etc) then you’re probably ok. Check your server’s documentation for this or contact them to inquire.

Let’s get to it…

1. Create a bare repository on your remote server

SSH into your server.

ssh username@serverdomain.com  You’ll then be asked for your password

You may have to configure this stuff if you’ve never ssh’ed into your server before. That’s not covered here. You can also do this step via FTP, but we’re trying to get away from FTP, remember.

Create a new directory called ‘bare’. This is where you’ll set up all bare repos for each site you make. You’ll need a bare repo for every site you create in the future.

mkdir bare
cd bare

Just type in git to make sure Git is installed. If you get a status report, all is good. (Later, you can customize your terminal prompt in a way that will always show you if you’re in a repo, what branch you’re in and if you’re in a clean or dirty state. Look into it when you get to that point. It’s really helpful.)

 

git screenshot

This is what you see if git is installed.

 

Now, create the bare repo using the domain name of your eventual site suffixed with .git. So if your domain is radwebsite.com, you’d do:

git init --bare radwebsite.git

That’s it, you now have a bare repo setup and ready to go.

2. Clone the bare repo to your local machine

On your computer create a directory called git in your user directory. On a mac, that’s something like Users/Sally/git

Open terminal, cd into the git directory you just created and clone the bare repo

git clone serverusername@serverdomain.com/bare:radwebsite.git

There’s some fancy stuff happening in the above command. This works similar to an ssh but with a few minor differences. Basically, you’re telling git where to clone from and the name of the repo you want to clone.

Now, within the git directory you will have another directory called ‘radwebsite’ (or whatever your repo was named). Make some changes, like add an index.html then do a commit of those changes.
Back at the terminal after you’ve made any changes run the following commands

git status – will list tracked changes available for commit
git add -A – adds all the changed files
git commit -m “message about the changes made” – commits the changes

 

After you’ve committed some changes push your repo back to the server

git push

3. Pull your repo into a live directory

The last step in this process is pulling your repo into a live directory so you can view your beautiful site online.

SSH into your server that serves up your preview site.

CD into the directory your domain points to. In a lot of cases, your hosting company will create a directory with the same name as your domain. So if they’re hosting myradwebsite.com you’ll have a directory on your server called myradwebsite. Everything in that directory is the website shown at myradwebsite.com. cd into this directory. We’re going to pull from the bare repo into this directory, essentially copying all those files to your live site.

git clone ../git:myradwebsite.git

That acts as your first pull. Every time after the first time simply run <code>git pull</code> and anything you’ve pushed from local to the bare repo will be copied to the live site

 

That’s it. You’re all set up. Now, just keep coding locally. After you push new changes from local to the bare repo you’ll just need to git pull changes into the live site directory on the server.

This post ended up being a lot of words, but the overall process is very simple. I hope you agree. And I hope it makes your life a lot easier. I know it has for me.

 

* People will say, don’t use Git for deployment. But it works great for test deployments during the development process. And that’s the idea of what we’re doing here. So ignore those people.

Leave a Reply

Your email address will not be published. Required fields are marked *