Posted by & filed under GIT.

Colored Output:

(notice the red modified and untracked files)

Put the following code in your ~/.gitconfig (global git configuration) file.

[color]
        diff = auto
        status = auto
        branch = auto
[format]
        pretty = "Commit:  %C(yellow)%H%nAuthor:  %C(green)%aN <%aE>%nDate:    (%C(red)%ar%Creset) %ai%nSubject: %s%n%n%b"

This will give you colored output like the following (notice the red modified file, red untracked file [added files will be green], and the current branch being green [non-current branches will be white]):

Shortcut Terminal Commands:

(notice how the gs command is the same as git status)

Put the following lines into your ~/.bash_profile file, then you can use the two or three character shortcuts to make your life a lot easier.

alias ga='git add'
alias gp='git push'
alias gl='git log'
alias gs='git status'
alias gd='git diff'
alias gm='git commit -m'
alias gma='git commit -am'
alias gb='git branch'
alias gc='git checkout'
alias gra='git remote add'
alias grr='git remote rm'
alias gpu='git pull'
alias gcl='git clone'

Terminal Autocompletion:

(screenshot of tab completion showing a list of all branches)

Download this script, and put it in the root of your home directory:

git-completion.bash

Now that the script is there, you will need to execute it each time your bash session starts. Put the following code in your ~/.bash_profile file:

if [ -f ~/.git-completion.bash ]; then
    . ~/.git-completion.bash
fi

Also, if you are using the short codes above, you will need to add the following two lines to your ~/.bash_profile as well so that the shortcut commands also get auto completed:

complete -o default -o nospace -F _git_branch gb
complete -o default -o nospace -F _git_checkout gc

Bash Prompt Branch Name:

(notice the master and test in parenthesis)

The following code requires the git-completion.bash script be setup properly (as defined in the above section). Once you have it setup, you can add the following code to your ~/.bash_profile file. It will change your bash prompt to this new. If you are inside of a git repository, it will display the name of the branch. If you aren’t in a git repository, it will just display the path. You may want to tweak it to be a little more familiar; it is based on a standard old linux prompt, and may look out of place depending on your distro or if you are using OS X.

green=$(tput setaf 2)
blue=$(tput setaf 4)
bold=$(tput bold)
red=$(tput setaf 1)
reset=$(tput sgr0)
PS1='\u@\[$green\]\h\[$reset\]:\w\[$blue\]$(__git_ps1)\[$reset\] \$ '

Thomas Hunter II

Thomas is passionate about technology and building products. A web design business created while attending college slowly evolved into a brick and mortar on Main St. of his small Midwestern hometown. His desire for fame and fortune led to the co-founding of a Y Combinator startup and a life in California.

Tags:

Hey there! I'm currently writing a book on Microservices which I expect to release in early 2017. If you're interested in getting updates please signup here. More info about the Book
  • Peter Abrahamsen

    Thanks for this. Have you found a way to color the prompts for the interactive commands, like git commit/add -p? It’s very difficult to figure out how far the chunk under question extends up the screen.

    Peter

  • Hi,

    Thank you so much for sharing.

    I used part of your solution in conjunction with this piece of code added to .bash_profile

    # Set git autocompletion and PS1 integration

    if [ -f /usr/local/git/contrib/completion/git-completion.bash ]; then
    . /usr/local/git/contrib/completion/git-completion.bash
    fi
    GIT_PS1_SHOWDIRTYSTATE=true

    if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
    fi

    On OS X, when you install GIT, the git-complete.bash file gets saved here:

    /usr/local/git/contrib/completion/git-completion.bash

    Kudos to this site for providing an alternate solution:

    http://en.newinstance.it/2010/05/23/git-autocompletion-and-enhanced-bash-prompt/

    Thanks!

  • It seems that color.ui=true does all of the above color.diff/status/branch.

    The three lines can be replaced by:

    [color]
    ui = true

  • I ran into a bunch of errors with this.

    Stripping it all back to my orig now yields this below every time I open up terminal

    -bash: uname: command not found
    -bash: ps: command not found
    $: