Advanced Git on the Mac (and probably Linux)

By September 14, 2011 Uncategorized 2 Comments

Recently at Posse we had an awesome talk from David Claridge on the finer points of Git, which I cannot explain how much it helped.

Git has been one of those things I want to understand more intimately, but through laziness (and probably fear) I have been using GUI tool for some time now and I have shielded myself from it’s inner workings. Having said that, one lunch time session later and I am feeling much more confident to start attacking the command line.

Some of the awesomeness that came out of the talk were finer explanations around Git terminology, some workflow tips and

also some tools. Tools which I am going to share here.

Git Branch Autocompletion
I cannot tell you how many times I’ve wanted to switch branchs and forgotten names, or simply wanted to be more certain of a name and had to do a git banch just to see the names etc. This tool allows you to tab-autocomplete a partial branch name much the same way bash does.

To sum up, you need just a few basic commands:

  1. Go to your home directory:
    cd ~
  2. Get the script:
    curl http://bit.ly/sc3ajv -OL
  3. open .bash_profile
    nano .bash_profile
  4. add this line to the end of your .bash_profile:
    source ~/git-completion.bash
    alias gco='git co'
    alias gci='git ci'
    alias grb='git rb'
  5. (optional) then, lastly, if you want to use autocomplete right away
    source ~/.bash_profile

Custom Bash Prompt for Git Branches
This is probably going to be the most handy tool to add to your Git workflow. It allows you to simply and easily see what git branch you currently have checked out, and the status of that branch by adding it to your bash prompt.

The install instructions are simple and laid out on the link above, but in the interests of keeping all the instructions here and easily accessible I will summize.

  1. Go to your home directory:
    cd ~
  2. Get the code:
    curl http://bit.ly/uCfcr8 -OL
  3. Add the following to your .bash_profile:
    source ~/.git_completion.sh

    function parse_git_dirty {
    [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
    }

    function parse_git_branch {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* (.*)/[1$(parse_git_dirty)]/"
    }

    export PS1='h:W$(__git_ps1 "[[e[0;32m]%s[e[0m][e[0;33m]$(parse_git_dirty)[e[0m]]")$ '

  4. (optional) then, lastly, if you want to use autocomplete right away
    source ~/.bash_profile

I am currently working on a way to automate this all via a simple one-line command, stay tuned.

2 Comments

  • Yasky says:

    With changes in the git status message, this would break. Be sure to match the comparison after the `!=` in `parse_git_dirty` with the default message for a clean repository. For instance, 1.8.2 has a different message for clean repositories and `parse_git_dirty` would always return “*”

Leave a Reply

Get in touch now! Contact Mitch