How do I show the git branch with colours in Bash prompt?











up vote
65
down vote

favorite
41












I am using this guide to show the branch name in gnome terminal (Ubuntu 15.10) when working in a git repository. Based on the above I now have the below in my ~/.bashrc file:



# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes


...



# Add git branch if its present to PS1
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt


As a result I now get:



enter image description here



so it works. But why has the coloring of my user@host been removed? And I would also expect that the branch name should be colored. Before it looked like this:



enter image description here



UPDATE:
I have now tried this guide instead:



https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt



adding this to .bashrc:



parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
}
export PS1="u@h [33[32m]w[33[33m]$(parse_git_branch)[33[00m] $ "


and that works:



enter image description here



Notice in .bashrc I also have this (default):



# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes


I have yet to find the reason why that snippet gives the correct result and the other version does not. Any input on this?



Here is the version of my .bashrc that has the old snippet enabled that does not work:



http://pastebin.com/M8kjEiH3










share|improve this question
























  • Was force_color_prompt uncommented before?
    – muru
    Feb 7 '16 at 8:13










  • Yes I have tried with both uncommented and commented same result. The guide posted above says its should be commented out.
    – u123
    Feb 7 '16 at 8:18










  • Can you post your complete .bashrc? IIRC the default .bashrc doesn't enable colour prompts, so you have to change it to show colours. It depends on what you changed.
    – muru
    Feb 7 '16 at 8:20








  • 1




    Have a look at line 64, which should tell you why uncommenting force_color_prompt didn't help.
    – muru
    Feb 7 '16 at 8:39






  • 2




    @u123 don't worry about the default .bashrc too much. If you mess up, you can always get the original from /etc/skel/.bashrc.
    – muru
    Feb 7 '16 at 8:52

















up vote
65
down vote

favorite
41












I am using this guide to show the branch name in gnome terminal (Ubuntu 15.10) when working in a git repository. Based on the above I now have the below in my ~/.bashrc file:



# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes


...



# Add git branch if its present to PS1
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt


As a result I now get:



enter image description here



so it works. But why has the coloring of my user@host been removed? And I would also expect that the branch name should be colored. Before it looked like this:



enter image description here



UPDATE:
I have now tried this guide instead:



https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt



adding this to .bashrc:



parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
}
export PS1="u@h [33[32m]w[33[33m]$(parse_git_branch)[33[00m] $ "


and that works:



enter image description here



Notice in .bashrc I also have this (default):



# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes


I have yet to find the reason why that snippet gives the correct result and the other version does not. Any input on this?



Here is the version of my .bashrc that has the old snippet enabled that does not work:



http://pastebin.com/M8kjEiH3










share|improve this question
























  • Was force_color_prompt uncommented before?
    – muru
    Feb 7 '16 at 8:13










  • Yes I have tried with both uncommented and commented same result. The guide posted above says its should be commented out.
    – u123
    Feb 7 '16 at 8:18










  • Can you post your complete .bashrc? IIRC the default .bashrc doesn't enable colour prompts, so you have to change it to show colours. It depends on what you changed.
    – muru
    Feb 7 '16 at 8:20








  • 1




    Have a look at line 64, which should tell you why uncommenting force_color_prompt didn't help.
    – muru
    Feb 7 '16 at 8:39






  • 2




    @u123 don't worry about the default .bashrc too much. If you mess up, you can always get the original from /etc/skel/.bashrc.
    – muru
    Feb 7 '16 at 8:52















up vote
65
down vote

favorite
41









up vote
65
down vote

favorite
41






41





I am using this guide to show the branch name in gnome terminal (Ubuntu 15.10) when working in a git repository. Based on the above I now have the below in my ~/.bashrc file:



# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes


...



# Add git branch if its present to PS1
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt


As a result I now get:



enter image description here



so it works. But why has the coloring of my user@host been removed? And I would also expect that the branch name should be colored. Before it looked like this:



enter image description here



UPDATE:
I have now tried this guide instead:



https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt



adding this to .bashrc:



parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
}
export PS1="u@h [33[32m]w[33[33m]$(parse_git_branch)[33[00m] $ "


and that works:



enter image description here



Notice in .bashrc I also have this (default):



# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes


I have yet to find the reason why that snippet gives the correct result and the other version does not. Any input on this?



Here is the version of my .bashrc that has the old snippet enabled that does not work:



http://pastebin.com/M8kjEiH3










share|improve this question















I am using this guide to show the branch name in gnome terminal (Ubuntu 15.10) when working in a git repository. Based on the above I now have the below in my ~/.bashrc file:



# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes


...



# Add git branch if its present to PS1
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt


As a result I now get:



enter image description here



so it works. But why has the coloring of my user@host been removed? And I would also expect that the branch name should be colored. Before it looked like this:



enter image description here



UPDATE:
I have now tried this guide instead:



https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-prompt



adding this to .bashrc:



parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
}
export PS1="u@h [33[32m]w[33[33m]$(parse_git_branch)[33[00m] $ "


and that works:



enter image description here



Notice in .bashrc I also have this (default):



# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes


I have yet to find the reason why that snippet gives the correct result and the other version does not. Any input on this?



Here is the version of my .bashrc that has the old snippet enabled that does not work:



http://pastebin.com/M8kjEiH3







command-line bash git






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 7 '16 at 8:39

























asked Feb 7 '16 at 8:07









u123

52311122




52311122












  • Was force_color_prompt uncommented before?
    – muru
    Feb 7 '16 at 8:13










  • Yes I have tried with both uncommented and commented same result. The guide posted above says its should be commented out.
    – u123
    Feb 7 '16 at 8:18










  • Can you post your complete .bashrc? IIRC the default .bashrc doesn't enable colour prompts, so you have to change it to show colours. It depends on what you changed.
    – muru
    Feb 7 '16 at 8:20








  • 1




    Have a look at line 64, which should tell you why uncommenting force_color_prompt didn't help.
    – muru
    Feb 7 '16 at 8:39






  • 2




    @u123 don't worry about the default .bashrc too much. If you mess up, you can always get the original from /etc/skel/.bashrc.
    – muru
    Feb 7 '16 at 8:52




















  • Was force_color_prompt uncommented before?
    – muru
    Feb 7 '16 at 8:13










  • Yes I have tried with both uncommented and commented same result. The guide posted above says its should be commented out.
    – u123
    Feb 7 '16 at 8:18










  • Can you post your complete .bashrc? IIRC the default .bashrc doesn't enable colour prompts, so you have to change it to show colours. It depends on what you changed.
    – muru
    Feb 7 '16 at 8:20








  • 1




    Have a look at line 64, which should tell you why uncommenting force_color_prompt didn't help.
    – muru
    Feb 7 '16 at 8:39






  • 2




    @u123 don't worry about the default .bashrc too much. If you mess up, you can always get the original from /etc/skel/.bashrc.
    – muru
    Feb 7 '16 at 8:52


















Was force_color_prompt uncommented before?
– muru
Feb 7 '16 at 8:13




Was force_color_prompt uncommented before?
– muru
Feb 7 '16 at 8:13












Yes I have tried with both uncommented and commented same result. The guide posted above says its should be commented out.
– u123
Feb 7 '16 at 8:18




Yes I have tried with both uncommented and commented same result. The guide posted above says its should be commented out.
– u123
Feb 7 '16 at 8:18












Can you post your complete .bashrc? IIRC the default .bashrc doesn't enable colour prompts, so you have to change it to show colours. It depends on what you changed.
– muru
Feb 7 '16 at 8:20






Can you post your complete .bashrc? IIRC the default .bashrc doesn't enable colour prompts, so you have to change it to show colours. It depends on what you changed.
– muru
Feb 7 '16 at 8:20






1




1




Have a look at line 64, which should tell you why uncommenting force_color_prompt didn't help.
– muru
Feb 7 '16 at 8:39




Have a look at line 64, which should tell you why uncommenting force_color_prompt didn't help.
– muru
Feb 7 '16 at 8:39




2




2




@u123 don't worry about the default .bashrc too much. If you mess up, you can always get the original from /etc/skel/.bashrc.
– muru
Feb 7 '16 at 8:52






@u123 don't worry about the default .bashrc too much. If you mess up, you can always get the original from /etc/skel/.bashrc.
– muru
Feb 7 '16 at 8:52












6 Answers
6






active

oldest

votes

















up vote
87
down vote



accepted










This snippet:



# Add git branch if its present to PS1

parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi


Is meant to replace the default prompt definition:



if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
fi


Which ends with:



unset color_prompt force_color_prompt


The .bashrc you posted shows you're adding it after the default prompt definition and unset color_prompt force_color_prompt (line #64).



Either replace the default prompt definition with the snippet or leave your ~/.bashrc as it is and comment the default prompt definition along with unset color_prompt force_color_prompt on line #64:





So part of your .bashrc could look like



parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m] $(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
# THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc)
#if [ "$color_prompt" = yes ]; then
# PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
#else
# PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
#fi
#unset color_prompt force_color_prompt


screensot






share|improve this answer























  • Verified the above and you are correct. I will stick with new version of snippet since it works without having to modify the default parts of the bashrc file.
    – u123
    Feb 7 '16 at 8:51










  • it fails to add color to the branch name.
    – Avinash Raj
    Feb 7 '16 at 10:11












  • @AvinashRaj Test it with a copy of the default ~/.bashrc in /etc/skel/.bashrc, you might have something interfering in your ~/.bashrc.
    – kos
    Feb 7 '16 at 10:15












  • un-comment force_color_prompt=yes (line # 48) if the colors are not visible.
    – Adil Abbasi
    Jun 7 '17 at 6:05






  • 1




    To color your branch according to its status, you can use the native git-prompt script provided by git itself.
    – Niket Pathak
    Jan 15 at 11:42


















up vote
15
down vote













Ubuntu: Show your branch name on your terminal


Add these lines in your ~/.bashrc file



# Show git branch name
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
else
PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
fi
unset color_prompt force_color_prompt





share|improve this answer




























    up vote
    6
    down vote













    For now, I followed this
    https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f and working,
    liking it so far, though I'm planning to customize it further.




    In Terminal



    mkdir ~/.bash


    Copy the raw git-prompt.sh file from git contrib in to the ~/.bash
    directory:
    https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh



    Inside ~/.bashrc or ~/.bash_profile (choose the file where you
    normally put any bash customizations/setup), add the lines:



    source ~/.bash/git-prompt.sh # Show git branch name at command prompt
    export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color

    # Terminal Prompt:
    # Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more)
    export PROMPT_COMMAND='__git_ps1 "w" "n\$ "' # Git branch (relies on git-prompt.sh)


    As long as you're inside a git repo, your Bash prompt should now show
    the current git branch in color signifying if its got uncommitted
    changes.







    share|improve this answer






























      up vote
      2
      down vote













      Go to home folder



      click Ctrl+h to show hidden files.



      Open .bashrc file and at the end paste the next:



      parse_git_branch() {
      git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
      }
      export PS1="u@h [33[32m]w[33[33m]$(parse_git_branch)[33[00m] $ "


      In case you have your terminal opened, close and open again. Enjoy!!






      share|improve this answer























      • Hello, I tried it and it works only where I switch superuser, can you tell me how to enable always?
        – Denis Stephanov
        Nov 24 at 16:37


















      up vote
      0
      down vote













      My problem was that I hadn't enabled the option



      Run command as a login shell in



      TerminalEditProfile PreferencesCommand






      share|improve this answer






























        up vote
        0
        down vote













        replace



        parse_git_branch


        with



        parse_git_branch 2>/dev/null


        in your PS1 definition and live happily ever after.






        share|improve this answer





















          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f730754%2fhow-do-i-show-the-git-branch-with-colours-in-bash-prompt%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          87
          down vote



          accepted










          This snippet:



          # Add git branch if its present to PS1

          parse_git_branch() {
          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
          }
          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
          fi


          Is meant to replace the default prompt definition:



          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
          fi


          Which ends with:



          unset color_prompt force_color_prompt


          The .bashrc you posted shows you're adding it after the default prompt definition and unset color_prompt force_color_prompt (line #64).



          Either replace the default prompt definition with the snippet or leave your ~/.bashrc as it is and comment the default prompt definition along with unset color_prompt force_color_prompt on line #64:





          So part of your .bashrc could look like



          parse_git_branch() {
          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
          }
          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m] $(parse_git_branch)[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
          fi
          # THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc)
          #if [ "$color_prompt" = yes ]; then
          # PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
          #else
          # PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
          #fi
          #unset color_prompt force_color_prompt


          screensot






          share|improve this answer























          • Verified the above and you are correct. I will stick with new version of snippet since it works without having to modify the default parts of the bashrc file.
            – u123
            Feb 7 '16 at 8:51










          • it fails to add color to the branch name.
            – Avinash Raj
            Feb 7 '16 at 10:11












          • @AvinashRaj Test it with a copy of the default ~/.bashrc in /etc/skel/.bashrc, you might have something interfering in your ~/.bashrc.
            – kos
            Feb 7 '16 at 10:15












          • un-comment force_color_prompt=yes (line # 48) if the colors are not visible.
            – Adil Abbasi
            Jun 7 '17 at 6:05






          • 1




            To color your branch according to its status, you can use the native git-prompt script provided by git itself.
            – Niket Pathak
            Jan 15 at 11:42















          up vote
          87
          down vote



          accepted










          This snippet:



          # Add git branch if its present to PS1

          parse_git_branch() {
          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
          }
          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
          fi


          Is meant to replace the default prompt definition:



          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
          fi


          Which ends with:



          unset color_prompt force_color_prompt


          The .bashrc you posted shows you're adding it after the default prompt definition and unset color_prompt force_color_prompt (line #64).



          Either replace the default prompt definition with the snippet or leave your ~/.bashrc as it is and comment the default prompt definition along with unset color_prompt force_color_prompt on line #64:





          So part of your .bashrc could look like



          parse_git_branch() {
          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
          }
          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m] $(parse_git_branch)[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
          fi
          # THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc)
          #if [ "$color_prompt" = yes ]; then
          # PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
          #else
          # PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
          #fi
          #unset color_prompt force_color_prompt


          screensot






          share|improve this answer























          • Verified the above and you are correct. I will stick with new version of snippet since it works without having to modify the default parts of the bashrc file.
            – u123
            Feb 7 '16 at 8:51










          • it fails to add color to the branch name.
            – Avinash Raj
            Feb 7 '16 at 10:11












          • @AvinashRaj Test it with a copy of the default ~/.bashrc in /etc/skel/.bashrc, you might have something interfering in your ~/.bashrc.
            – kos
            Feb 7 '16 at 10:15












          • un-comment force_color_prompt=yes (line # 48) if the colors are not visible.
            – Adil Abbasi
            Jun 7 '17 at 6:05






          • 1




            To color your branch according to its status, you can use the native git-prompt script provided by git itself.
            – Niket Pathak
            Jan 15 at 11:42













          up vote
          87
          down vote



          accepted







          up vote
          87
          down vote



          accepted






          This snippet:



          # Add git branch if its present to PS1

          parse_git_branch() {
          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
          }
          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
          fi


          Is meant to replace the default prompt definition:



          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
          fi


          Which ends with:



          unset color_prompt force_color_prompt


          The .bashrc you posted shows you're adding it after the default prompt definition and unset color_prompt force_color_prompt (line #64).



          Either replace the default prompt definition with the snippet or leave your ~/.bashrc as it is and comment the default prompt definition along with unset color_prompt force_color_prompt on line #64:





          So part of your .bashrc could look like



          parse_git_branch() {
          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
          }
          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m] $(parse_git_branch)[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
          fi
          # THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc)
          #if [ "$color_prompt" = yes ]; then
          # PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
          #else
          # PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
          #fi
          #unset color_prompt force_color_prompt


          screensot






          share|improve this answer














          This snippet:



          # Add git branch if its present to PS1

          parse_git_branch() {
          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
          }
          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
          fi


          Is meant to replace the default prompt definition:



          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
          fi


          Which ends with:



          unset color_prompt force_color_prompt


          The .bashrc you posted shows you're adding it after the default prompt definition and unset color_prompt force_color_prompt (line #64).



          Either replace the default prompt definition with the snippet or leave your ~/.bashrc as it is and comment the default prompt definition along with unset color_prompt force_color_prompt on line #64:





          So part of your .bashrc could look like



          parse_git_branch() {
          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
          }
          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m] $(parse_git_branch)[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
          fi
          # THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc)
          #if [ "$color_prompt" = yes ]; then
          # PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[00m]$ '
          #else
          # PS1='${debian_chroot:+($debian_chroot)}u@h:w$ '
          #fi
          #unset color_prompt force_color_prompt


          screensot







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '17 at 16:14









          Mark

          287




          287










          answered Feb 7 '16 at 8:30









          kos

          25.2k869119




          25.2k869119












          • Verified the above and you are correct. I will stick with new version of snippet since it works without having to modify the default parts of the bashrc file.
            – u123
            Feb 7 '16 at 8:51










          • it fails to add color to the branch name.
            – Avinash Raj
            Feb 7 '16 at 10:11












          • @AvinashRaj Test it with a copy of the default ~/.bashrc in /etc/skel/.bashrc, you might have something interfering in your ~/.bashrc.
            – kos
            Feb 7 '16 at 10:15












          • un-comment force_color_prompt=yes (line # 48) if the colors are not visible.
            – Adil Abbasi
            Jun 7 '17 at 6:05






          • 1




            To color your branch according to its status, you can use the native git-prompt script provided by git itself.
            – Niket Pathak
            Jan 15 at 11:42


















          • Verified the above and you are correct. I will stick with new version of snippet since it works without having to modify the default parts of the bashrc file.
            – u123
            Feb 7 '16 at 8:51










          • it fails to add color to the branch name.
            – Avinash Raj
            Feb 7 '16 at 10:11












          • @AvinashRaj Test it with a copy of the default ~/.bashrc in /etc/skel/.bashrc, you might have something interfering in your ~/.bashrc.
            – kos
            Feb 7 '16 at 10:15












          • un-comment force_color_prompt=yes (line # 48) if the colors are not visible.
            – Adil Abbasi
            Jun 7 '17 at 6:05






          • 1




            To color your branch according to its status, you can use the native git-prompt script provided by git itself.
            – Niket Pathak
            Jan 15 at 11:42
















          Verified the above and you are correct. I will stick with new version of snippet since it works without having to modify the default parts of the bashrc file.
          – u123
          Feb 7 '16 at 8:51




          Verified the above and you are correct. I will stick with new version of snippet since it works without having to modify the default parts of the bashrc file.
          – u123
          Feb 7 '16 at 8:51












          it fails to add color to the branch name.
          – Avinash Raj
          Feb 7 '16 at 10:11






          it fails to add color to the branch name.
          – Avinash Raj
          Feb 7 '16 at 10:11














          @AvinashRaj Test it with a copy of the default ~/.bashrc in /etc/skel/.bashrc, you might have something interfering in your ~/.bashrc.
          – kos
          Feb 7 '16 at 10:15






          @AvinashRaj Test it with a copy of the default ~/.bashrc in /etc/skel/.bashrc, you might have something interfering in your ~/.bashrc.
          – kos
          Feb 7 '16 at 10:15














          un-comment force_color_prompt=yes (line # 48) if the colors are not visible.
          – Adil Abbasi
          Jun 7 '17 at 6:05




          un-comment force_color_prompt=yes (line # 48) if the colors are not visible.
          – Adil Abbasi
          Jun 7 '17 at 6:05




          1




          1




          To color your branch according to its status, you can use the native git-prompt script provided by git itself.
          – Niket Pathak
          Jan 15 at 11:42




          To color your branch according to its status, you can use the native git-prompt script provided by git itself.
          – Niket Pathak
          Jan 15 at 11:42












          up vote
          15
          down vote













          Ubuntu: Show your branch name on your terminal


          Add these lines in your ~/.bashrc file



          # Show git branch name
          force_color_prompt=yes
          color_prompt=yes
          parse_git_branch() {
          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
          }
          if [ "$color_prompt" = yes ]; then
          PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
          else
          PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
          fi
          unset color_prompt force_color_prompt





          share|improve this answer

























            up vote
            15
            down vote













            Ubuntu: Show your branch name on your terminal


            Add these lines in your ~/.bashrc file



            # Show git branch name
            force_color_prompt=yes
            color_prompt=yes
            parse_git_branch() {
            git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
            }
            if [ "$color_prompt" = yes ]; then
            PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
            else
            PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
            fi
            unset color_prompt force_color_prompt





            share|improve this answer























              up vote
              15
              down vote










              up vote
              15
              down vote









              Ubuntu: Show your branch name on your terminal


              Add these lines in your ~/.bashrc file



              # Show git branch name
              force_color_prompt=yes
              color_prompt=yes
              parse_git_branch() {
              git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
              }
              if [ "$color_prompt" = yes ]; then
              PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
              else
              PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
              fi
              unset color_prompt force_color_prompt





              share|improve this answer












              Ubuntu: Show your branch name on your terminal


              Add these lines in your ~/.bashrc file



              # Show git branch name
              force_color_prompt=yes
              color_prompt=yes
              parse_git_branch() {
              git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/(1)/'
              }
              if [ "$color_prompt" = yes ]; then
              PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[33[01;34m]w[33[01;31m]$(parse_git_branch)[33[00m]$ '
              else
              PS1='${debian_chroot:+($debian_chroot)}u@h:w$(parse_git_branch)$ '
              fi
              unset color_prompt force_color_prompt






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 16 '17 at 9:23









              Sam

              25925




              25925






















                  up vote
                  6
                  down vote













                  For now, I followed this
                  https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f and working,
                  liking it so far, though I'm planning to customize it further.




                  In Terminal



                  mkdir ~/.bash


                  Copy the raw git-prompt.sh file from git contrib in to the ~/.bash
                  directory:
                  https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh



                  Inside ~/.bashrc or ~/.bash_profile (choose the file where you
                  normally put any bash customizations/setup), add the lines:



                  source ~/.bash/git-prompt.sh # Show git branch name at command prompt
                  export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color

                  # Terminal Prompt:
                  # Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more)
                  export PROMPT_COMMAND='__git_ps1 "w" "n\$ "' # Git branch (relies on git-prompt.sh)


                  As long as you're inside a git repo, your Bash prompt should now show
                  the current git branch in color signifying if its got uncommitted
                  changes.







                  share|improve this answer



























                    up vote
                    6
                    down vote













                    For now, I followed this
                    https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f and working,
                    liking it so far, though I'm planning to customize it further.




                    In Terminal



                    mkdir ~/.bash


                    Copy the raw git-prompt.sh file from git contrib in to the ~/.bash
                    directory:
                    https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh



                    Inside ~/.bashrc or ~/.bash_profile (choose the file where you
                    normally put any bash customizations/setup), add the lines:



                    source ~/.bash/git-prompt.sh # Show git branch name at command prompt
                    export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color

                    # Terminal Prompt:
                    # Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more)
                    export PROMPT_COMMAND='__git_ps1 "w" "n\$ "' # Git branch (relies on git-prompt.sh)


                    As long as you're inside a git repo, your Bash prompt should now show
                    the current git branch in color signifying if its got uncommitted
                    changes.







                    share|improve this answer

























                      up vote
                      6
                      down vote










                      up vote
                      6
                      down vote









                      For now, I followed this
                      https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f and working,
                      liking it so far, though I'm planning to customize it further.




                      In Terminal



                      mkdir ~/.bash


                      Copy the raw git-prompt.sh file from git contrib in to the ~/.bash
                      directory:
                      https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh



                      Inside ~/.bashrc or ~/.bash_profile (choose the file where you
                      normally put any bash customizations/setup), add the lines:



                      source ~/.bash/git-prompt.sh # Show git branch name at command prompt
                      export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color

                      # Terminal Prompt:
                      # Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more)
                      export PROMPT_COMMAND='__git_ps1 "w" "n\$ "' # Git branch (relies on git-prompt.sh)


                      As long as you're inside a git repo, your Bash prompt should now show
                      the current git branch in color signifying if its got uncommitted
                      changes.







                      share|improve this answer














                      For now, I followed this
                      https://gist.github.com/eliotsykes/47516b877f5a4f7cd52f and working,
                      liking it so far, though I'm planning to customize it further.




                      In Terminal



                      mkdir ~/.bash


                      Copy the raw git-prompt.sh file from git contrib in to the ~/.bash
                      directory:
                      https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh



                      Inside ~/.bashrc or ~/.bash_profile (choose the file where you
                      normally put any bash customizations/setup), add the lines:



                      source ~/.bash/git-prompt.sh # Show git branch name at command prompt
                      export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color

                      # Terminal Prompt:
                      # Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more)
                      export PROMPT_COMMAND='__git_ps1 "w" "n\$ "' # Git branch (relies on git-prompt.sh)


                      As long as you're inside a git repo, your Bash prompt should now show
                      the current git branch in color signifying if its got uncommitted
                      changes.








                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 16 '17 at 12:04









                      muru

                      135k19288488




                      135k19288488










                      answered Mar 7 '17 at 23:11









                      ifelsemonkey

                      16114




                      16114






















                          up vote
                          2
                          down vote













                          Go to home folder



                          click Ctrl+h to show hidden files.



                          Open .bashrc file and at the end paste the next:



                          parse_git_branch() {
                          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
                          }
                          export PS1="u@h [33[32m]w[33[33m]$(parse_git_branch)[33[00m] $ "


                          In case you have your terminal opened, close and open again. Enjoy!!






                          share|improve this answer























                          • Hello, I tried it and it works only where I switch superuser, can you tell me how to enable always?
                            – Denis Stephanov
                            Nov 24 at 16:37















                          up vote
                          2
                          down vote













                          Go to home folder



                          click Ctrl+h to show hidden files.



                          Open .bashrc file and at the end paste the next:



                          parse_git_branch() {
                          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
                          }
                          export PS1="u@h [33[32m]w[33[33m]$(parse_git_branch)[33[00m] $ "


                          In case you have your terminal opened, close and open again. Enjoy!!






                          share|improve this answer























                          • Hello, I tried it and it works only where I switch superuser, can you tell me how to enable always?
                            – Denis Stephanov
                            Nov 24 at 16:37













                          up vote
                          2
                          down vote










                          up vote
                          2
                          down vote









                          Go to home folder



                          click Ctrl+h to show hidden files.



                          Open .bashrc file and at the end paste the next:



                          parse_git_branch() {
                          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
                          }
                          export PS1="u@h [33[32m]w[33[33m]$(parse_git_branch)[33[00m] $ "


                          In case you have your terminal opened, close and open again. Enjoy!!






                          share|improve this answer














                          Go to home folder



                          click Ctrl+h to show hidden files.



                          Open .bashrc file and at the end paste the next:



                          parse_git_branch() {
                          git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* (.*)/ (1)/'
                          }
                          export PS1="u@h [33[32m]w[33[33m]$(parse_git_branch)[33[00m] $ "


                          In case you have your terminal opened, close and open again. Enjoy!!







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Oct 13 at 22:24









                          abu_bua

                          3,15081023




                          3,15081023










                          answered Oct 13 at 21:59









                          webtechnelson

                          78110




                          78110












                          • Hello, I tried it and it works only where I switch superuser, can you tell me how to enable always?
                            – Denis Stephanov
                            Nov 24 at 16:37


















                          • Hello, I tried it and it works only where I switch superuser, can you tell me how to enable always?
                            – Denis Stephanov
                            Nov 24 at 16:37
















                          Hello, I tried it and it works only where I switch superuser, can you tell me how to enable always?
                          – Denis Stephanov
                          Nov 24 at 16:37




                          Hello, I tried it and it works only where I switch superuser, can you tell me how to enable always?
                          – Denis Stephanov
                          Nov 24 at 16:37










                          up vote
                          0
                          down vote













                          My problem was that I hadn't enabled the option



                          Run command as a login shell in



                          TerminalEditProfile PreferencesCommand






                          share|improve this answer



























                            up vote
                            0
                            down vote













                            My problem was that I hadn't enabled the option



                            Run command as a login shell in



                            TerminalEditProfile PreferencesCommand






                            share|improve this answer

























                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              My problem was that I hadn't enabled the option



                              Run command as a login shell in



                              TerminalEditProfile PreferencesCommand






                              share|improve this answer














                              My problem was that I hadn't enabled the option



                              Run command as a login shell in



                              TerminalEditProfile PreferencesCommand







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Sep 26 '17 at 16:39









                              Owen Hines

                              2,40511034




                              2,40511034










                              answered Sep 26 '17 at 11:15









                              Joker

                              1011




                              1011






















                                  up vote
                                  0
                                  down vote













                                  replace



                                  parse_git_branch


                                  with



                                  parse_git_branch 2>/dev/null


                                  in your PS1 definition and live happily ever after.






                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    replace



                                    parse_git_branch


                                    with



                                    parse_git_branch 2>/dev/null


                                    in your PS1 definition and live happily ever after.






                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      replace



                                      parse_git_branch


                                      with



                                      parse_git_branch 2>/dev/null


                                      in your PS1 definition and live happily ever after.






                                      share|improve this answer












                                      replace



                                      parse_git_branch


                                      with



                                      parse_git_branch 2>/dev/null


                                      in your PS1 definition and live happily ever after.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered May 18 at 10:19









                                      andrej

                                      22614




                                      22614






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Ask Ubuntu!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f730754%2fhow-do-i-show-the-git-branch-with-colours-in-bash-prompt%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Ellipse (mathématiques)

                                          Quarter-circle Tiles

                                          Mont Emei