Git branch in the prompt











up vote
2
down vote

favorite












Could anyone explain why the branch name is not showing up on my (bash) prompt?

I am using ubuntu 16.10. I tried to use the instructions from this site.



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


My .bashrc file is here: https://github.com/JeremieGauthier/.bashrc/blob/master/.bashrc



I also tried the following code but it didn't work either.



function color_my_prompt {
local __user_and_host="[33[01;32m]u@h"
local __cur_location="[33[01;34m]w"
local __git_branch_color="[33[31m]"
#local __git_branch="`ruby -e "print (%x{git branch 2> /dev/null}.grep(/^*/).first || '').gsub(/^* (.+)$/, '(1) ')"`"
local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\* (.+)$/(\\1) /`'
local __prompt_tail="[33[35m]$"
local __last_color="[33[00m]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt









share|improve this question




















  • 2




    Rather than git branch (which is a porcelain command), you should use get rev-parse --abbrev-ref HEAD to get the branch name. And if you stop discarding the error message you may see that perhaps you are not in a git working directory, so parse_git_branch generates no output.
    – William Pursell
    Aug 30 '17 at 19:45















up vote
2
down vote

favorite












Could anyone explain why the branch name is not showing up on my (bash) prompt?

I am using ubuntu 16.10. I tried to use the instructions from this site.



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


My .bashrc file is here: https://github.com/JeremieGauthier/.bashrc/blob/master/.bashrc



I also tried the following code but it didn't work either.



function color_my_prompt {
local __user_and_host="[33[01;32m]u@h"
local __cur_location="[33[01;34m]w"
local __git_branch_color="[33[31m]"
#local __git_branch="`ruby -e "print (%x{git branch 2> /dev/null}.grep(/^*/).first || '').gsub(/^* (.+)$/, '(1) ')"`"
local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\* (.+)$/(\\1) /`'
local __prompt_tail="[33[35m]$"
local __last_color="[33[00m]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt









share|improve this question




















  • 2




    Rather than git branch (which is a porcelain command), you should use get rev-parse --abbrev-ref HEAD to get the branch name. And if you stop discarding the error message you may see that perhaps you are not in a git working directory, so parse_git_branch generates no output.
    – William Pursell
    Aug 30 '17 at 19:45













up vote
2
down vote

favorite









up vote
2
down vote

favorite











Could anyone explain why the branch name is not showing up on my (bash) prompt?

I am using ubuntu 16.10. I tried to use the instructions from this site.



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


My .bashrc file is here: https://github.com/JeremieGauthier/.bashrc/blob/master/.bashrc



I also tried the following code but it didn't work either.



function color_my_prompt {
local __user_and_host="[33[01;32m]u@h"
local __cur_location="[33[01;34m]w"
local __git_branch_color="[33[31m]"
#local __git_branch="`ruby -e "print (%x{git branch 2> /dev/null}.grep(/^*/).first || '').gsub(/^* (.+)$/, '(1) ')"`"
local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\* (.+)$/(\\1) /`'
local __prompt_tail="[33[35m]$"
local __last_color="[33[00m]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt









share|improve this question















Could anyone explain why the branch name is not showing up on my (bash) prompt?

I am using ubuntu 16.10. I tried to use the instructions from this site.



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


My .bashrc file is here: https://github.com/JeremieGauthier/.bashrc/blob/master/.bashrc



I also tried the following code but it didn't work either.



function color_my_prompt {
local __user_and_host="[33[01;32m]u@h"
local __cur_location="[33[01;34m]w"
local __git_branch_color="[33[31m]"
#local __git_branch="`ruby -e "print (%x{git branch 2> /dev/null}.grep(/^*/).first || '').gsub(/^* (.+)$/, '(1) ')"`"
local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\* (.+)$/(\\1) /`'
local __prompt_tail="[33[35m]$"
local __last_color="[33[00m]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt






bash rc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 3:03









TT--

603723




603723










asked Aug 30 '17 at 19:34







user8364555















  • 2




    Rather than git branch (which is a porcelain command), you should use get rev-parse --abbrev-ref HEAD to get the branch name. And if you stop discarding the error message you may see that perhaps you are not in a git working directory, so parse_git_branch generates no output.
    – William Pursell
    Aug 30 '17 at 19:45














  • 2




    Rather than git branch (which is a porcelain command), you should use get rev-parse --abbrev-ref HEAD to get the branch name. And if you stop discarding the error message you may see that perhaps you are not in a git working directory, so parse_git_branch generates no output.
    – William Pursell
    Aug 30 '17 at 19:45








2




2




Rather than git branch (which is a porcelain command), you should use get rev-parse --abbrev-ref HEAD to get the branch name. And if you stop discarding the error message you may see that perhaps you are not in a git working directory, so parse_git_branch generates no output.
– William Pursell
Aug 30 '17 at 19:45




Rather than git branch (which is a porcelain command), you should use get rev-parse --abbrev-ref HEAD to get the branch name. And if you stop discarding the error message you may see that perhaps you are not in a git working directory, so parse_git_branch generates no output.
– William Pursell
Aug 30 '17 at 19:45












1 Answer
1






active

oldest

votes

















up vote
2
down vote













At installation git comes with a git-prompt.sh that a bash function __git_ps1 to update the prompt.



The function is simple and add the current branch when you cd inside a git repo directory.



Just add a call to this function or another one available in your ps1 and make sure your .bashrc or your .bash_profile load the bash_completion directory.



If your version of git doesn't include the git_prompt.sh script manually download it and follow the instructions it provide.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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%2fstackoverflow.com%2fquestions%2f45968420%2fgit-branch-in-the-prompt%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    At installation git comes with a git-prompt.sh that a bash function __git_ps1 to update the prompt.



    The function is simple and add the current branch when you cd inside a git repo directory.



    Just add a call to this function or another one available in your ps1 and make sure your .bashrc or your .bash_profile load the bash_completion directory.



    If your version of git doesn't include the git_prompt.sh script manually download it and follow the instructions it provide.






    share|improve this answer



























      up vote
      2
      down vote













      At installation git comes with a git-prompt.sh that a bash function __git_ps1 to update the prompt.



      The function is simple and add the current branch when you cd inside a git repo directory.



      Just add a call to this function or another one available in your ps1 and make sure your .bashrc or your .bash_profile load the bash_completion directory.



      If your version of git doesn't include the git_prompt.sh script manually download it and follow the instructions it provide.






      share|improve this answer

























        up vote
        2
        down vote










        up vote
        2
        down vote









        At installation git comes with a git-prompt.sh that a bash function __git_ps1 to update the prompt.



        The function is simple and add the current branch when you cd inside a git repo directory.



        Just add a call to this function or another one available in your ps1 and make sure your .bashrc or your .bash_profile load the bash_completion directory.



        If your version of git doesn't include the git_prompt.sh script manually download it and follow the instructions it provide.






        share|improve this answer














        At installation git comes with a git-prompt.sh that a bash function __git_ps1 to update the prompt.



        The function is simple and add the current branch when you cd inside a git repo directory.



        Just add a call to this function or another one available in your ps1 and make sure your .bashrc or your .bash_profile load the bash_completion directory.



        If your version of git doesn't include the git_prompt.sh script manually download it and follow the instructions it provide.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 31 '17 at 12:23

























        answered Aug 30 '17 at 19:57









        lee-pai-long

        71848




        71848






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f45968420%2fgit-branch-in-the-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

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu