kennethkam.com

git and fabric

I have been a Subversion user for a while, and I wanted to learn another version control system. I chose git. From the start, I found converting to using git a pleasure. There are plenty of guides out there to get you started. In this entry, I will talk about how I used git to store my blog’s code. In addition, I will talk about fabric, an excellent automation tool for deploying your website to multiple servers, but works just as well if you are only doing it to one.

Getting familiar with git

In my previous entry, I mentioned briefly how to initialize a git repository and committed files to it. It was as simple as running a git init command in your current working directory and you have a free repository. However, I wanted to be able to push these commits to a server where my other computers could do a pull. I logged into my remote server via SSH and initialized a barebones git repository:

$ ssh wf
$ mkdir /var/git/kennethkam-django.git
$ cd /var/git/kennethkam-django.git
$ git init --bare .
Initialized empty Git repository in /var/git/kennethkam-django.git

Locally, I added the remote repository and pushed the commits.

$ cd ~/Workspace/kennethkam-django
$ git remote add origin ssh://wf/var/git/kennethkam-django.git
$ git push origin master
Counting objects: 72, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (53/53), done.
Writing objects: 100% (54/54), 4.66 KiB, done.
Total 54 (delta 42), reused 0 (delta 0)
To ssh://kenkam@web58.webfaction.com/home/kenkam/git/kennethkam-django.git
   6e663a9..685aecc  master -> master

On another computer, I pulled the repository.

$ cd ~/Workspace/kennethkam-django
$ git remote add origin ssh://wf/var/git/kennethkam-django.git
$ git pull origin master
remote: Counting objects: 711, done.
remote: Compressing objects: 100% (620/620), done.
remote: Total 711 (delta 425), reused 121 (delta 75)
Receiving objects: 100% (711/711), 1.22 MiB | 323 KiB/s, done.
Resolving deltas: 100% (425/425), done.
From ssh://wf/home/kenkam/git/kennethkam-django
 * branch            master     -> FETCH_HEAD

That’s it.

Don’t you do a reset --hard without thinking

I continued coding and committing. I often made a mistake with typing the commit message, I found myself using the git commit --amend flag often. On one occasion, I made several bad commits. I wanted to undo the commits but still keep the changes in the staging area. This is that I did:

$ git reset --hard HEAD^
HEAD is now at 1a75c1d... "Commit message 1"

All the bad changes were discarded, but then all my good changes disappeared along with it. git anticipates this and even allows you to reset your reset:

$ git reflog
1a75c1d... HEAD@{0} reset --hard HEAD^: updating HEAD
f67d23f... HEAD@{1} "Commit message 2"
$ git reset --hard 1a75c1d
HEAD is now at f67d23f... "Commit message 2"

Phew. I was back to where I started. What I wanted to do was:

$ git reset --soft HEAD^

Sometimes I wanted to reset files that are added to the staging area but I did not want to commit them:

$ git reset HEAD file.txt

That was about all the things I did with git.

Automating deployment with fabric

Fabric is an excellent application that allows you to automate the deployment of your website. I am currently using the old 0.1.1, but there is a stable 0.9 which has some syntactic differences, same philosophy.

First, grab fabric off PyPI.

$ pip install fabric

Fabric needs a ‘fabfile’ to work with:

$ cd ~/Workspace/kennethkam-django
$ touch fabfile.py

A fabfile needs to define several configs and a few functions:

config.fab_hosts = ['someserver.com']
config.path = '/path/to/srv'

def deploy():
    # To deploy, we require the fab_hosts variable
    require('fab_hosts')
    deploy()
    restart()

def upload():
    local("git archive --format=tar master | gzip > upload.tar.gz")
    put('upload.tar.gz', '/home/kenkam/uploaded.tar.gz')
    local("rm -f " + 'upload.tar.gz')
    run('cd /home/kenkam; tar -xzf uploaded.tar.gz')
    run('rm -f /home/kenkam/uploaded.tar.gz')

def restart():
    run('service apache2 restart')

All I needed to do was to deploy it on my terminal:

$ cd ~/Workspace/kennethkam-django
$ fab deploy
...

No more playing around with FTP and SSH. Do take a look at the old and the new docs.

Small improvements over time

I hope to continue work on this blog from time to time. In the meantime, university work takes the most priority. I hope to get stuck in on a project and continue learning and sharing when I have a bit of free time on my hands. Grab my feed and stay tuned until then.

Comments

  • There are no comments.

Add a Comment