script to show git branch in bash no longer works on ubuntu 18.04

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
3
down vote

favorite
1














In my 16.04 installation I put this in my ~/.bashrc file:



#Show git branch in commandline
parse_git_branch()
git branch 2> /dev/null
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(parse_git_branch)[33[00m] $ "


It showed a prompt like this:



user:~/myrepo (master) $


But when I do the same on 18.04 the prompt looks like this instead:



user:~/myrepo $ 


How do I get it to work in 18.04?



$ printf "%qn" "$PS1" "$PROMPT_COMMAND" "$0" "$SHELL"
\[\033[01;32m\]\u:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\[\033[00m\] $
''
bash
/bin/bash


I’m using sed (GNU sed) 4.4 and GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu). This is a "fresh" installation, so there is nothing additional in .bashrc.







share|improve this question





















  • @PerlDuck Please add one or even two answers!!!
    – dessert
    Jun 5 at 11:22














up vote
3
down vote

favorite
1














In my 16.04 installation I put this in my ~/.bashrc file:



#Show git branch in commandline
parse_git_branch()
git branch 2> /dev/null
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(parse_git_branch)[33[00m] $ "


It showed a prompt like this:



user:~/myrepo (master) $


But when I do the same on 18.04 the prompt looks like this instead:



user:~/myrepo $ 


How do I get it to work in 18.04?



$ printf "%qn" "$PS1" "$PROMPT_COMMAND" "$0" "$SHELL"
\[\033[01;32m\]\u:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\[\033[00m\] $
''
bash
/bin/bash


I’m using sed (GNU sed) 4.4 and GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu). This is a "fresh" installation, so there is nothing additional in .bashrc.







share|improve this question





















  • @PerlDuck Please add one or even two answers!!!
    – dessert
    Jun 5 at 11:22












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1







In my 16.04 installation I put this in my ~/.bashrc file:



#Show git branch in commandline
parse_git_branch()
git branch 2> /dev/null
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(parse_git_branch)[33[00m] $ "


It showed a prompt like this:



user:~/myrepo (master) $


But when I do the same on 18.04 the prompt looks like this instead:



user:~/myrepo $ 


How do I get it to work in 18.04?



$ printf "%qn" "$PS1" "$PROMPT_COMMAND" "$0" "$SHELL"
\[\033[01;32m\]\u:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\[\033[00m\] $
''
bash
/bin/bash


I’m using sed (GNU sed) 4.4 and GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu). This is a "fresh" installation, so there is nothing additional in .bashrc.







share|improve this question















In my 16.04 installation I put this in my ~/.bashrc file:



#Show git branch in commandline
parse_git_branch()
git branch 2> /dev/null
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(parse_git_branch)[33[00m] $ "


It showed a prompt like this:



user:~/myrepo (master) $


But when I do the same on 18.04 the prompt looks like this instead:



user:~/myrepo $ 


How do I get it to work in 18.04?



$ printf "%qn" "$PS1" "$PROMPT_COMMAND" "$0" "$SHELL"
\[\033[01;32m\]\u:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\[\033[00m\] $
''
bash
/bin/bash


I’m using sed (GNU sed) 4.4 and GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu). This is a "fresh" installation, so there is nothing additional in .bashrc.









share|improve this question












share|improve this question




share|improve this question








edited Jun 5 at 15:41
























asked Jun 4 at 20:14









Felix Rosén

164111




164111











  • @PerlDuck Please add one or even two answers!!!
    – dessert
    Jun 5 at 11:22
















  • @PerlDuck Please add one or even two answers!!!
    – dessert
    Jun 5 at 11:22















@PerlDuck Please add one or even two answers!!!
– dessert
Jun 5 at 11:22




@PerlDuck Please add one or even two answers!!!
– dessert
Jun 5 at 11:22










2 Answers
2






active

oldest

votes

















up vote
4
down vote













Although you already solved the problem, I'd like to introduce the solution already shipped with git:



Together with git the file /usr/lib/git-core/git-sh-prompt gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x flags) but rather to be sourced from the .bashrc. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1.



You can add the following lines to your .bashrc:



source /usr/lib/git-core/git-sh-prompt
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "


The top of the file /usr/lib/git-core/git-sh-prompt gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh). but this is not necessary (the instruction is obsolete).



You may also set some environment variables to alter the function's behaviour. I, for instance, use



export GIT_PS1_SHOWDIRTYSTATE=yes


The result then looks like:



# clean
pduck:~/oss/rsyslog (master) $


or



# dirty
pduck:~/oss/rsyslog (master *) $


or



# something added to index
pduck:~/oss/rsyslog (master +) $


or



# in a completely fresh repo
pduck:~/oss/xxx (master #) $



Btw: The "proper" way to get the current branch is not to parse git branch's output as that may change with new git versions.
git branch is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with



if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
else
current_branch=""
fi


The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^), then it fails and we use git rev-parse --short HEAD instead to show the SHA1. The if checks whether we are in a git working directory at all (because else the commands don't make any sense).






share|improve this answer






























    up vote
    2
    down vote



    accepted










    The solution was quite simple.



    I set an upstream, staged a commit and pushed it. After that it worked.



    I thought that the script somehow used the .git folder that is generated with git init. Since it works on branches that does not have an upstream.






    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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      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%2f1043610%2fscript-to-show-git-branch-in-bash-no-longer-works-on-ubuntu-18-04%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote













      Although you already solved the problem, I'd like to introduce the solution already shipped with git:



      Together with git the file /usr/lib/git-core/git-sh-prompt gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x flags) but rather to be sourced from the .bashrc. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1.



      You can add the following lines to your .bashrc:



      source /usr/lib/git-core/git-sh-prompt
      export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "


      The top of the file /usr/lib/git-core/git-sh-prompt gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh). but this is not necessary (the instruction is obsolete).



      You may also set some environment variables to alter the function's behaviour. I, for instance, use



      export GIT_PS1_SHOWDIRTYSTATE=yes


      The result then looks like:



      # clean
      pduck:~/oss/rsyslog (master) $


      or



      # dirty
      pduck:~/oss/rsyslog (master *) $


      or



      # something added to index
      pduck:~/oss/rsyslog (master +) $


      or



      # in a completely fresh repo
      pduck:~/oss/xxx (master #) $



      Btw: The "proper" way to get the current branch is not to parse git branch's output as that may change with new git versions.
      git branch is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with



      if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
      current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
      else
      current_branch=""
      fi


      The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^), then it fails and we use git rev-parse --short HEAD instead to show the SHA1. The if checks whether we are in a git working directory at all (because else the commands don't make any sense).






      share|improve this answer



























        up vote
        4
        down vote













        Although you already solved the problem, I'd like to introduce the solution already shipped with git:



        Together with git the file /usr/lib/git-core/git-sh-prompt gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x flags) but rather to be sourced from the .bashrc. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1.



        You can add the following lines to your .bashrc:



        source /usr/lib/git-core/git-sh-prompt
        export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "


        The top of the file /usr/lib/git-core/git-sh-prompt gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh). but this is not necessary (the instruction is obsolete).



        You may also set some environment variables to alter the function's behaviour. I, for instance, use



        export GIT_PS1_SHOWDIRTYSTATE=yes


        The result then looks like:



        # clean
        pduck:~/oss/rsyslog (master) $


        or



        # dirty
        pduck:~/oss/rsyslog (master *) $


        or



        # something added to index
        pduck:~/oss/rsyslog (master +) $


        or



        # in a completely fresh repo
        pduck:~/oss/xxx (master #) $



        Btw: The "proper" way to get the current branch is not to parse git branch's output as that may change with new git versions.
        git branch is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with



        if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
        current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
        else
        current_branch=""
        fi


        The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^), then it fails and we use git rev-parse --short HEAD instead to show the SHA1. The if checks whether we are in a git working directory at all (because else the commands don't make any sense).






        share|improve this answer

























          up vote
          4
          down vote










          up vote
          4
          down vote









          Although you already solved the problem, I'd like to introduce the solution already shipped with git:



          Together with git the file /usr/lib/git-core/git-sh-prompt gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x flags) but rather to be sourced from the .bashrc. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1.



          You can add the following lines to your .bashrc:



          source /usr/lib/git-core/git-sh-prompt
          export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "


          The top of the file /usr/lib/git-core/git-sh-prompt gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh). but this is not necessary (the instruction is obsolete).



          You may also set some environment variables to alter the function's behaviour. I, for instance, use



          export GIT_PS1_SHOWDIRTYSTATE=yes


          The result then looks like:



          # clean
          pduck:~/oss/rsyslog (master) $


          or



          # dirty
          pduck:~/oss/rsyslog (master *) $


          or



          # something added to index
          pduck:~/oss/rsyslog (master +) $


          or



          # in a completely fresh repo
          pduck:~/oss/xxx (master #) $



          Btw: The "proper" way to get the current branch is not to parse git branch's output as that may change with new git versions.
          git branch is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with



          if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
          current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
          else
          current_branch=""
          fi


          The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^), then it fails and we use git rev-parse --short HEAD instead to show the SHA1. The if checks whether we are in a git working directory at all (because else the commands don't make any sense).






          share|improve this answer















          Although you already solved the problem, I'd like to introduce the solution already shipped with git:



          Together with git the file /usr/lib/git-core/git-sh-prompt gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x flags) but rather to be sourced from the .bashrc. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1.



          You can add the following lines to your .bashrc:



          source /usr/lib/git-core/git-sh-prompt
          export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "


          The top of the file /usr/lib/git-core/git-sh-prompt gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh). but this is not necessary (the instruction is obsolete).



          You may also set some environment variables to alter the function's behaviour. I, for instance, use



          export GIT_PS1_SHOWDIRTYSTATE=yes


          The result then looks like:



          # clean
          pduck:~/oss/rsyslog (master) $


          or



          # dirty
          pduck:~/oss/rsyslog (master *) $


          or



          # something added to index
          pduck:~/oss/rsyslog (master +) $


          or



          # in a completely fresh repo
          pduck:~/oss/xxx (master #) $



          Btw: The "proper" way to get the current branch is not to parse git branch's output as that may change with new git versions.
          git branch is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with



          if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
          current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
          else
          current_branch=""
          fi


          The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^), then it fails and we use git rev-parse --short HEAD instead to show the SHA1. The if checks whether we are in a git working directory at all (because else the commands don't make any sense).







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 6 at 10:39


























          answered Jun 6 at 10:04









          PerlDuck

          3,62911030




          3,62911030






















              up vote
              2
              down vote



              accepted










              The solution was quite simple.



              I set an upstream, staged a commit and pushed it. After that it worked.



              I thought that the script somehow used the .git folder that is generated with git init. Since it works on branches that does not have an upstream.






              share|improve this answer

























                up vote
                2
                down vote



                accepted










                The solution was quite simple.



                I set an upstream, staged a commit and pushed it. After that it worked.



                I thought that the script somehow used the .git folder that is generated with git init. Since it works on branches that does not have an upstream.






                share|improve this answer























                  up vote
                  2
                  down vote



                  accepted







                  up vote
                  2
                  down vote



                  accepted






                  The solution was quite simple.



                  I set an upstream, staged a commit and pushed it. After that it worked.



                  I thought that the script somehow used the .git folder that is generated with git init. Since it works on branches that does not have an upstream.






                  share|improve this answer













                  The solution was quite simple.



                  I set an upstream, staged a commit and pushed it. After that it worked.



                  I thought that the script somehow used the .git folder that is generated with git init. Since it works on branches that does not have an upstream.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jun 5 at 15:44









                  Felix Rosén

                  164111




                  164111






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1043610%2fscript-to-show-git-branch-in-bash-no-longer-works-on-ubuntu-18-04%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      pylint3 and pip3 broken

                      Missing snmpget and snmpwalk

                      How to enroll fingerprints to Ubuntu 17.10 with VFS491