Photo from Chile

Using Git Rebase to Squash Commits

A Git command that can be very useful, yet is often overlooked by beginners (such as myself) is the rebase command. I admit to not fully understanding all of its uses, but there is one simple use for it that I have found myself taking advantage of lately. Here's the scenario:

You have a remote Git repository, perhaps hosted at GitHub. You want to work on a new feature, so you create a local branch on your machine:

view plain print about
1git checkout -b newBranch

You make some changes and commit them to the local branch:

view plain print about
1git commit -a -m"My first set of changes"

You make some more changes and commit them to the local branch:

view plain print about
1git commit -a -m"My second set of changes"

You make some more changes and commit them to the local branch:

view plain print about
1git commit -a -m"My third set of changes"

You now have three separate commits that relate to one feature, and the feature is complete. You could simply push the changes to your remote, but then you'd end up with three commits on the remote that are not particularly meaningful. The only reason you have three commits in your local repo is that you completed the work in three steps. Perhaps you'd rather have just one commit reported in the remote repo for this feature. Thankfully git rebase allows you to do that very simply.


Using TextMate as the Default Editor for Git on OS X

There are a number of Git commands which pop open a text editor which you then use to provide information. For example, if you issue the command:

view plain print about
1git commit

The editor will appear allowing you to type your commit message. The default editor that appears for me, on OS X, is vi which is a strange beast to work with if you've never encountered it before (which I hadn't, prior to using Git). I found a helpful cheat sheet, which allowed me to use the editor, but I still find it cumbersome. Thankfully it's a pretty simple matter to use a different text editor with Git. There are a number of ways of doing this, and I'm going to discuss two of them. To start, let's look at how Git decides what editor to use.

Which editor will Git use?

According to the man page for git-commit:

The editor used to edit the commit log message will be chosen from the GIT_EDITOR environment variable, the core.editor configuration variable, the VISUAL environment variable, or the EDITOR environment variable (in that order).

We're going to look at changing the EDITOR environment variable and the core.editor configuration variable.

Change the EDITOR environment variable

Simply add the following line to your .bash_profile:

view plain print about
1export EDITOR="/usr/bin/mate -w"

This will cause Git to use TextMate, and may also allow other command line tools to use it as well.

Change the core.editor configuration variable

Issue the following command:

view plain print about
1git config --global core.editor "mate -w"

This is useful if you only want to change the behaviour of Git, and not affect the rest of your environment.

Git Cheat Sheet

I've been using Git for awhile, but I still use Subversion for most of my projects, so I seem to always come back to Git after a long break. Of course, by that time I've forgotten how to do certain things, so I thought I'd throw together a quick cheat sheet that I, and anyone else, can use as a reference. I will continually update this post as I come up with new questions and answers.


Using Git and GitHub to Sync Config Files between Machines

This post is a follow-up to my earlier post about Placing Config Files Under Version Control with Git and GitHub. In that post I discussed how one can use Git and GitHub to place your config files under version control (via Git), and to maintain a backup of them (via GitHub). In this post I'm going to discuss a set up that will allow another machine's config files to stay in sync with the originals. The scenario I'm discussing involves config files, but one could use this approach for any set of files that one wants to keep in sync between two machines.


Placing Config Files Under Version Control with Git and GitHub

Working with Git, I've become aware of the fact that there are certain config files on my machine which require customization and therefore would be nice to have under version control. These files are often referred to as dot files, or dotfiles, as their names all start with a dot. The three files that I currently have under version control are .bash_profile, .gitconfig and .gitignore. The first two of those files expect to reside in my home directory, but the way Git works, in order to place them under version control they need to reside in a folder that is also a Git repository.

For obvious reasons I don't want to make my home directory a Git repo, but there's a simple solution to this problem. Using symbolic links, a topic that I discussed in an earlier blog post, I can keep my dotfiles in a Git repo, and also continue to use them as live config files. Here is a step-by-step guide to getting your dotfiles under version control with Git:


OS X Command Line Interface Tips - Customizing the Bash Shell

I've been working on a Mac for around four months now, and never really had much reason to open up Terminal and use the command line interface (CLI), other than for starting and stopping Tomcat. Now that I've started trying to learn about Git, I'm using the command line more and more, and finding out new stuff daily, so I'm going to write the occasional post to share some of this info.

About Bash

The operating system that most of us run on Macs is called OS X, and it's based on Unix. The way that one interacts with Unix is via a command shell, and the default shell for OS X is called Bash. It allows us to interact with our operating system without going through the graphical user interface (GUI) that sits on top of the OS. According to Wikipedia Bash stands for Bourne-again shell as it is a successor to the Bourne shell. So, when you open up Terminal, or iTerm which is an enhanced Terminal alternative, you are interacting with the Bash shell.


Setting up a Mac to Work with Git and GitHub

After hearing much on Twitter and the blogisphere about Git, I've finally decided that I need to invest some time in learning about it so I can decide whether or not it's for me.

I started out by watching a video that I found linked to somewhere. The video, Tom Preston-Werner, Chris Wanstrath and Scott Chacon -- Git, GitHub and Social Coding consists of three presentations by the guys that created GitHub. The first one, by Tom Preston-Werner, provided a nice overview of Git and GitHub - I found it quite useful. The second one, by Chris Wanstrath, went into more detail about GitHub itself. Although this is not necessary information for using Git, I found it to be quite inspirational. I began to see how not only is Git a cool version control tool, but how combining it with with something like GitHub can potentially help an open source project get more contributors, as it really lowers the barrier to entry. The final presentation, by Scott Chacon was way over my head. I didn't get much out of it, but may view it again in the future when I become proficient with Git.

Having attained a basic understanding of Git, I decided that it was time to install it, and attempt to move one of my open source projects to GitHub. I found a number of blog posts that cover getting started with Git, including a very useful series by Mike Henke, which starts with Setting Up a Riaforge Project with Git and Github (Part 1). Mike's posts cover setting up Git on Windows, and as I'm on a Mac I needed some more information. I found a post by Jaisen Mathai entitled How to get started hosting your git repository using GitHub and OSX, which included most of the setup instructions, as well as a post by Andrew Bednarz on theAppleBlog called Using Git With OS X: 6 Tools to Get You Up and Running which directed me to some of the software. I decided to take all of the information that I gleaned from those resources are compile it into this one post, for others that would like to get started with Git and GitHub on a Mac.


Previous Entries