Linux (Ubuntu): Show Current Git Branch in Terminal’s Prompt

1 Min. Read
Sep 13, 2014

 Rails Ruby JS git branch git branch

Terminal does not have anything to do with git-branch. If you need to know in which branch you are currently working on then you need to manually type git branch

Good News :

You can modify your terminal settings to make it show you which git branch you are working currently. To do this you need to put some piece of code in a file. Follow the steps below

  1. open the file ~/.bashrc (in home folder) with some text editor with root permission and copy-paste the following code in that file (click the link below to see the code snippet)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    # Author : Shiva Bhusal
    # github: shivabhusal
    # stackoverflow: users/3437900/illusionist
    ##########################################
    
    # Usages:
    # - create a file called ~/.promptrc
    # - add following in ~/.bashrc
    #   `source ~/.promptrc`
    #   # this will include the source of the file
    
    # Content of the file ~/.promptrc
    
    # @returns {String}
    #  Eg: master
    function parse_git_branch () {
      git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
    }
    
            RED="\[\033[0;31m\]"
         YELLOW="\[\033[0;33m\]"
          GREEN="\[\033[0;32m\]"
           BLUE="\[\033[0;34m\]"
      LIGHT_RED="\[\033[1;31m\]"
    LIGHT_GREEN="\[\033[1;32m\]"
          WHITE="\[\033[1;37m\]"
     LIGHT_GRAY="\[\033[0;37m\]"
       NO_COLOR="\[\033[0m\]"
    
    RUBY_VERSION="\$(~/.rvm/bin/rvm-prompt)"
    
    # \w --> working directory
    # \u --> current user
    # \h --> Computer name
    
    export PS1="$GREEN\u:$BLUE\w$YELLOW (${RUBY_VERSION} : $(parse_git_branch)) $NO_COLOR$ "
    
  2. Restart the terminal or open new tab, you are done!

You might also like

Terminal does not have anything to do with git-branch. If you need to know in which branch you are currently working on then you need to manually type $ git branch. Good News: You can modify your terminal settings to make it show you which git branch you are working currently. To do this you […]


Read More..


Write To Me

Hire me shiva bhusal
We'll never share your email with anyone else.
I'll never share your email with anyone else.