A Simple Git Workflow

I just started using Git for source control. Having used CVS and SVN extensively, I thought I had a decent background on version control and would be able to pick it up quickly. I quickly was overwhelmed by the amount of concepts and explanations needed in the documentation. Hint to the folks writing the Git book: start with simple concepts and build up from there. I don’t need to know how Git stores its files in the first chapter. I don’t need to know what a fast forward merge is. Give me one chapter that has a simple workflow example, then jump into the explanations of the internals and advanced features. I had to read through 50 pages of the book to figure out a basic remote workflow. Here are some of the bits I’ve gleaned. I’m still a Git beginner, so tips are always appreciated.

I’ll assume you have Git set up already, there are tutorials on the web for the wide variety of supported platforms. Once you’re set up with Git, you should be able to run ‘git –version’ at the command line and see a version string (like “git version 1.6.3.2″). All git commands are run in the format ‘git COMMAND_NAME OPTIONS’. Running ‘git –help’ will list all commands. Running ‘git help COMMAND_NAME’ will give a man page about that command. For example, to get information about the add command, you can run ‘git help add’.

Setting up a Git repository is simple. You can use an existing directory or create something from scratch. I’ll start out fresh. The ‘init’ command sets up a new repository.

Kleinschs-Macbook:tmp nick$ mkdir first && cd first
Kleinschs-Macbook:first nick$ echo 'firstEdit' > file1
Kleinschs-Macbook:first nick$ git init

Now we’re set up with a Git repository in this directory. The ‘status’ command gives you information about your working set.

Kleinschs-Macbook:first nick$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#	file1
nothing added to commit but untracked files present (use "git add" to track)

To make a commit in Git, you first do an ‘add’ of all files you want to be included in the commit, then a ‘commit’. The commit command will prompt for a message using your favorite editor, or you can use the ‘-m’ flag to add the message at the command line.

Kleinschs-Macbook:first nick$ git add file1
Kleinschs-Macbook:first nick$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#
#	new file:   file1
#
Kleinschs-Macbook:first nick$ git commit -m'First commit'
[master (root-commit) 4cc0433] first file
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file1
Kleinschs-Macbook:first nick$ git status
# On branch master
nothing to commit (working directory clean)

Let’s say we want to make a change. We’ll modify the first file and add another.

Kleinschs-Macbook:first nick$ echo 'secondEdit' > file1
Kleinschs-Macbook:first nick$ echo 'anotherFile' > file2
Kleinschs-Macbook:first nick$ git status
# On branch master
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#	modified:   file1
#
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#	file2
no changes added to commit (use "git add" and/or "git commit -a")

Git detects that we’ve changed one file and added another. If you want to see the changes you made, you can use the ‘diff’ command to show a diff of the files.

Kleinschs-Macbook:first nick$ git diff file1
diff --git a/file1 b/file1
index dc2bdc4..1837f2f 100644
--- a/file1
+++ b/file1
@@ -1 +1 @@
-firstEdit
+secondEdit

We could add the change and commit, but there’s a slightly easier way. As the status output describes, you can use the ‘-a’ flag for commit to automatically commit all modified files. It will not add new files, you’ll still have to do add those yourself. Let’s add the second file and commit the whole thing.

Kleinschs-Macbook:first nick$ git add file2
Kleinschs-Macbook:first nick$ git commit -am'Made a second commit!'
[master 74ec253] Made a second commit!
2 files changed, 2 insertions(+), 1 deletions(-)
create mode 100644 file2
Kleinschs-Macbook:first nick$ git status
# On branch master
nothing to commit (working directory clean)

The last thing to cover for right now is looking at past history. You use the ‘log’ command to view commit history.

Kleinschs-Macbook:first nick$ git log file1
commit 8ebf88b3d99309c1bd245156459a24549cf11ee8
Author: Nick Kleinschmidt <nick.kleinschmidt@gmail.com>
Date:   Sat Aug 15 09:28:26 2009 -0400
 
    Made a second commit!
 
commit 7801dc66b2b3b5029e888ad35e5f0405584b4ff1
Author: Nick Kleinschmidt <nick.kleinschmidt@gmail.com>
Date:   Sat Aug 15 09:24:47 2009 -0400
 
    first commit
Kleinschs-Macbook:first nick$ git log file2
commit 8ebf88b3d99309c1bd245156459a24549cf11ee8
Author: Nick Kleinschmidt <nick.kleinschmidt@gmail.com>
Date:   Sat Aug 15 09:28:26 2009 -0400
 
    Made a second commit!

With this setup, you can work locally, changing and committing to your hearts content. At this point, it’s fairly similar to most traditional version control systems. There’s still a lot to cover! Next time, I’ll get into setting up a remote repository, then we’ll dive into branching and merging.

Leave a comment

Setting up WordPress on Debian 5.0

I’ll assume you’re starting with a stock Debian 5.0 system and have set up sudo access for your account. I’ll be using the user nick as my personal user for this setup.

First we need install the prerequisites: Apache, PHP, and MySQL. It’ll ask you for a root password for MySQL, make sure you hold onto that. We’ll need it later.

sudo apt-get install apache2 mysql-server php5 php5-mysql

Now we need to make sure that Apache is configured with the modules needed for WordPress.

sudo a2enmod rewrite
sudo a2enmod php5

WordPress uses a MySQL database to store your blog posts and comments, so we need to create a MySQL database for wordpress. MySQL will ask you for the root password you created before. Make sure to save the password you set up for the wordpress user, as this is what WordPress will use to connect to the database.

mysql -u root -p
CREATE DATABASE `wordpress`;
grant all privileges on `wordpress`.* to 'wordpress'@localhost identified by 'ANOTHER-PASSWORD'; 
flush privileges;

Need to set up WordPress with a directory so we can use it. Download WordPress from their site and stick it in your home directory.

cd /var/www
sudo mkdir sites && cd sites
sudo cp ~nick/wordpress-2.8.2.tar.gz .
sudo tar xzvf wordpress-2.8.2.tar.gz
sudo touch wordpress/.htaccess
sudo chown -R nick:www-data wordpress
sudo chmod -R g+w wordpress

We changed the group and gave write permissions so WordPress will be able to set up its own config files. We set up the .htaccess file in advance because it’s easier to let WordPress change it based on the URL settings.

In order to serve the website, we need to set up an Apache Virtual Host. Create a file in /etc/apache/sites-available (mine is kleinsch.com) for your site and add the following code.

<VirtualHost *:80>
        ServerName kleinsch.com
        ServerAlias www.kleinsch.com
        ServerAdmin youremail@yourhost.com
        DocumentRoot /var/www/sites/wordpress
        <Directory /var/www/sites/wordpress>
                Options FollowSymLinks
                AllowOverride All
        </Directory>
</VirtualHost>

Enable the site within Apache and restart:

sudo a2ensite kleinsch.com
sudo /etc/init.d/apache restart

You should see no warnings. Open up your browser and navigate to ‘http://yoursite.com/wp-admin/install.php’.

The first page will tell you WordPress needs to create a config file, we want it to do that. On the next page, enter the database information you set up above. Leave the defaults for the encoding and table prefix. Enter a title for your blog, and you’re up and running!

Leave a comment