Coloring git in Mac Terminal and more..

Most of the programmers prefers using git  from command-line as it unbound the true strength of this tool by helping you know the real flow. This is what most of fancy-gui-tool-users miss (even I was) when using git.

Today i am going to show you how we can afford a little more luxury with our command-prompt aka Terminal when using git.

It’s pretty strait-forward, easy to follow 2-step work-flow. [..Assuming you already have git installed..].

Git-coloring

Step 1 is just a git command to color git outputs and second part is a mac user-config to redesign prompt for better git experience. Anyone can skip step-2 and feel free to be happy with what you have. But believe me, it is also a easy trick with few steps.

  1. Git coloring: The small part with a big outcome: type this and <Enter>
    git config --global color.ui true

    and from now on, you’ll see your git outputs colored!

  2. Bash-prompt coloring: The bigger part with a unique outcome:
    1. Write this and press <Enter>
      cd ~
    2. And this again and press <Enter>
      nano .profile
    3. You should have nano opened with file .profile in your Terminal (a terminal-based notepad or something). Paste lines below (If there is already something written, scroll all the way bottom and paste.)
      # Setting GIT prompt
      c_cyan=`tput setaf 6`
      c_red=`tput setaf 1`
      c_green=`tput setaf 2`
      c_sgr0=`tput sgr0`
      
      branch_color ()
      {
          if git rev-parse --git-dir >/dev/null 2>&1
          then
              color=""
              if git diff --quiet 2>/dev/null >&2
              then
                  color=${c_green}
              else
                  color=${c_red}
              fi
          else
              return 0
          fi
          echo -n $color
      }
      
      parse_git_branch ()
      {
          if git rev-parse --git-dir >/dev/null 2>&1
          then
              gitver="["$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')"]"
          else
              return 0
          fi
      echo -e $gitver
      }
      
      #It's important to escape colors with \[ to indicate the length is 0
      PS1='{\[${c_cyan}\]\u\[${c_sgr0}\]} @\[${c_cyan}\]\W\[${c_sgr0}\]\[\[$(branch_color)\]$(parse_git_branch)\[${c_sgr0}\]# '
      
    4. Then Press Ctrl+x to close nano, y to confirm saving file. And you will be back in your Terminal again.
    5. Close Terminal and open again..(to get config loaded)
    6. Now you can change directory to any of your git repo and see the current branch-name with coloring! Red for uncommitted changes, Green for clean state.

Tell me if you have done it fine and like it. If there were something wrong, tell me that also and I’ll try to help.

Update:

After some experiment, i’ve come to know that .bash_profile and .bash_login gets precedence over .profile and so will not be executed if any of this is existed in your home dir. So if that is your case, you may prefer to append the code to .bash_profile instead of .profile.