Enhancing Git on OS X
Git is a distributed version control system that has grown to be wildly popular among developers. It was created for use in managing the Linux kernel, and has since expanded to all walks of software development. It has become standard in iOS and OS X development, for use as dependency management, if nothing else. Git is extremely popular in other places, as well. Regardless of what git is used for, there are a number of enhancements that can be made to improve its usability. I outlined a few of them, tailored for OS X, in this article.
There are many ways to install git on OS X, but I am only going to discuss one of them in this blog post, using the Homebrew package manager for OS X. Installing git through Homebrew downloads the latest stable release of git, compiles it, and installs it to the ‘cellar’, usually located in
/usr/local/. The advantage in using a package manager like Homebrew or MacPorts to install git is that your machine is always running the latest version of it.
Assuming you have Homebrew installed, git can be installed with one command:
1 brew install git
Git can be configured to color its output so that it is easier to read and understand. For example, if colors are enabled, changes staged for commit are colored in green in the output of
git status, whereas those that are not are colored in red. This allows the user to easily and quickly distinguish between the two. Color is not enabled by default, but can be enabled with one command:
1 git config --global color.ui true
When using git in a shell, typing out long commands can often be a hassle. Installing bash completion for git will save you a lot of time, allowing you to tab complete all of your commands. It is very easy to set up, especially if you are using Homebrew. You only need to install the
1 brew install bash-completion
and then add the following to your
if [ -f $(brew --prefix)/etc/bash_completion ]; then
source $(brew --prefix)/etc/bash_completion
and voila! Git should now tab-complete all of your commands.
In addition to bash completion, aliases can also be defined to shorten the time required to enter long commands. The only problem with aliases is that they can become embedded in muscle memory, and are only local to your own machine. This means that if you login into an arbitrary server, or use your friends computer, the aliases will not be there to help you. It may only cause frustration, but you also may forget the underlying command behind the alias. Regardless, adding aliases is as simple as one command. For example, to alias
co, you can enter:
1 git config --global alias.co checkout
Git also comes with a handy utility to add information about the current repository to your bash prompt. Having git status in the prompt provides a heads up display of whether the current directory is under version control or not, and can also provide basic information, like which branch is currently checked out. This feature can be easily set up, assuming you installed git and bash-completion with Homebrew, by adding the following to your
1 source $(brew --prefix)/etc/bash_completion.d/git-prompt.sh
__git_ps1 function is now available in your shell. You can then use it in your prompt like the example below, which adds the current branch to the default prompt in OS X:
1 PS1="\h:\W \u\$(__git_ps1 \" (%s) \")\$"
It you happen to use Github for hosting your git repositories (and enhancing your git workflow), there is a convenient tool that can integrate some of Github’s functionality into your command line. The tool is called Hub. One feature it adds is short-hand notation for repositories, assuming that the repository is hosted on github. For example, it can shorten
1 git clone git://github.com/conradev/QuickQR.git
1 git clone conradev/QuickQR
In addition to short-hand, Hub also adds Github specific functionality to your command line, like the ability to open pull requests. If you are working on a topic branch, and want to open a pull request, you need only type
1 git pull-request
and you can type up a pull request in your text editor. Installing hub is also a breeze. To install it with Homebrew, you can type
1 brew install hub
This installs the ‘hub’ command to your PATH. It acts as an intermediary layer on top of git, meaning it uses git to execute the commands you enter, after preprocessing them a bit. To integrate hub seamlessly (as seen in the examples above), you can alias git to hub, by adding the following to your
1 eval "$(hub alias -s)"
When working with git, graphical components in one’s workflow can be of great aid in doing specific tasks. If you have had to conduct an extensive merge manually, for example, you know that it can become a nightmare. Git was created with this limitation in mind. Git allows for external diff and merge tools to act as front-ends to aid in those operations. The fine folk at Black Pixel created an awesome OS X application for comparing and merging files, named Kaleidoscope. With a couple clicks, it will install a command line tool and add itself to your git configuration as a diff and merge tool. To conduct a complex merge, you can use
git mergetool as opposed to
git merge, and Kaleidoscope will be opened for resolving any conflicts. There are also tools out there that allow you to interact with a git repository graphically, one of the most popular on OS X being Git Tower. Graphical managers allow you to commit code, manage branches and do anything you want with the click of your mouse.
I personally do not use Git Tower, as I prefer git on the command line. I prefer it because I like to know exactly what I am executing, when I execute it, and because shell is portable. This is why I like Kaleidoscope as a diff/merge tool, because it is invoked from the command line only when needed. Regardless, deciding whether or not to use a graphical manager is a matter of personal preference.
The hope in making git more usable is that it transforms from a necesary and annoying task into one that is efficient and enjoyable. This way, you can spend more time coding, and less time dealing with version control. So, get coding!
Feel free to follow me on Twitter or check out my software. Additionally, if your company is hiring mobile dev interns this upcoming summer, and has no inhibitions hiring a high school student, be sure to get in touch.
Discuss on Hacker News here.