Skip to content

Add branch name to terminal prompt

Franco Pestilli edited this page Jul 24, 2015 · 2 revisions

Once you start having many different branches of your repo, you will want to avoid getting confused about which branch you are currently working on. One way to do that is to make your terminal "git aware", by entering the following lines in your bashrc:

To do so, cd in your home directory:

  cd ~

Look for your .bashrc file (this is a hidden file so, the "." at the beginning of the file name).

  ls -all 

You should see the following in addition to several other files.

  $ .bashrc

Edit the file. I like "gedit" as editor in Linux.

  $ gedit .bashrc

Add the following lines somewhere in the file.

  # Set the prompt to show the current git branch:
  function parse_git_branch {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return
    echo "("${ref#refs/heads/}")" 
  }
  PS1="\h:\W$RED \$(parse_git_branch)$NO_COLOR $"