Alexey Gravanov

Improving git on Mac OS X

Mar 21, 2015

Git is a distributed version control system quite popular among software developers. It was created by Linux Torvalds about 9 years ago for managing Linux Kernel sources, and has quickly expanded to the entire software development community. It's fast, powerful, and you do not need a dedicated server to work with it. Providing analogy, I would say it's like a "sharp knife", by using it, you can do amazing things, but you can easily cut your finger off as well if you pay no attention. Although you do not have to use a dedicated server to use git, you can do it and there are several companies providing hosted git server for your repositories, like BitBucket and GitHub. All the things are happening at GitHub, but BitBucket offers private repositories for free.

Installation

First of all, git has to be installed:

brew install git

You don't have Homebrew? Go and get it, it's awesome!

Completion

Like tab completion instead of typing long commands? No problem, there is a completion package which should be installed together with git. Add the following line into your ~/.bash_profile:

source /usr/local/etc/bash_completion.d/git-completion.bash

Restart terminal or reload bash profile by typing:

. ~/.bash_profile

and voila! You can now complete all git command by pressing tab.

Colors

Don't like boring black and white output of git status? Let's add some colors. Open your git configuration, usually ~/.gitconfig, and edit it to get something like this:

[color]
    ui = true
    status = true
[color "status"]
    added = green
    updated = green
    changed = green
    untracked = cyan
    branch = magenta

Possible keys for coloring status are:

  • header – header text of the status message,
  • added or updated – files which are added, but not committed,
  • changed – files which are changed, but not added to the index,
  • untracked – files which are not tracked by git,
  • branch – the current branch,
  • nobranch – color the no branch warning is shown in, defaulting to red.

Possible values for colors are: normal, black, white, red, green, yellow, blue, magenta and cyan, following by optional styles: bold, dim, ul, blink, and reverse.

You can also specify two colors. In this case first one is the foreground and the second one is the background.

Prompt

Now we need some colors to make prompt colorful:

and a bit of git configuration:

Download and source both files as well as expose PS1 environment variable by adding the following lines into your ~/.bash_profile:

source /path/where/you/put/it/colors.bash
source /path/where/you/put/it/git-prompt.bash

export PS1="$Color_Off[$ICyan\w\$(git_status)$Color_Off] \$ "

After restarting terminal or reloading bash profile, it should look something like this (repository with added and changed files):

or like this (repository with added and modified files):

Aliases

git allows you to configure aliases for commands, here are mine:

[alias]
    cl = clone
    st = status
    co = commit
    rb = rebase
    pl = pull
    pu = push
    br = branch

Add them to your ~/.gitconfig and you will be able to type git st instead of git status.

Hub

If you are using GitHub for hosting your projects, you may also install hub, which replaces git and extends it functionality with a bunch of nice GitHub-specific commands, allowing you to work with forks, feature branches, pull requests and so on from the command line.