Never Stop Building - Crafting Wood with Japanese Techniques
Crafting Wood with Japanese Techniques

Legacy Blog Articles

How to Setup Git Completion and Repo State on OSX (Like a Pro)

Author's Note! This article has been imported from my previous website. I wanted to preserve all of the old content as many people have found some value in it. There may be some broken links and or formatting issues. If something isn't right, please let me know and I'll do my best to make an update.

I spent some time recently helping two of my students set up some Git tools on their systems, which proved rather difficult. Information was spotty on the internet, and due to the variety of system configurations, it was impractical to download Xcode, install HomeBrew, and so forth. I thought it was time to layout how we did it, quite simply, and also admonish that all Git users to set something similar up.

Every Git User Should Do This

I personally think that if you ever point someone else at something in your terminal, it is nice to have a reasonable prompt that helps convey some information. Also, who wants to type git status -s over and over and OVER again?

1. Create the files you need

touch ~/.bash_profile
touch ~/.git-completion.bash
touch ~/.git-prompt.sh

2. Populate your completion file

In the .git-completion.bash file put the contents located here: https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

Update the permissions of the file (not sure if this is necessary).

chmod 755 ~/.git-completion.bash

3. Populate your prompt file

In the .git-prompt.sh file, put the contents located here: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

Update the permissions of the file (not sure if this is necessary).

chmod 755 ~/.git-prompt.sh

4. Populate your .bash_profile

You may already have things in this file; if so, you may add this to the bottom or top:

#!/bin/bash

source ~/.git-completion.bash
source ~/.git-prompt.sh

MAGENTA="\[\033[0;35m\]"
YELLOW="\[\033[0;33m\]"
BLUE="\[\033[34m\]"
LIGHT_GRAY="\[\033[0;37m\]"
CYAN="\[\033[0;36m\]"
GREEN="\[\033[0;32m\]"
GIT_PS1_SHOWDIRTYSTATE=true
export LS_OPTIONS='--color=auto'
export CLICOLOR='Yes'
export LSCOLORS=gxfxbEaEBxxEhEhBaDaCaD

export PS1=$LIGHT_GRAY"\u@\h"'$(
    if [[ $(__git_ps1) =~ \*\)$ ]]
    # a file has been modified but not added
    then echo "'$YELLOW'"$(__git_ps1 " (%s)")
    elif [[ $(__git_ps1) =~ \+\)$ ]]
    # a file has been added, but not commited
    then echo "'$MAGENTA'"$(__git_ps1 " (%s)")
    # the state is clean, changes are commited
    else echo "'$CYAN'"$(__git_ps1 " (%s)")
    fi)'$BLUE" \w"$GREEN": "

alias ll='ls -lah'
alias gg='git status -s'

5. Reset your terminal

Now, you should have a few things going for you:

  • Clean looking path prompt.
  • Indication of the branch you are on in a Git repository.
  • gg now gets you your git status -s.
  • ll gives you an ls -lah (file list all, long, human readable).
  • Sweet color changes and symbols on repo state change.

Everything is fine and dandy and my repo is in a clean state!

It looks like I must have added some files.

There are files added, but also modified.

Something has been modified, but not added.

softwareJason Fox