Git branch in the prompt

445 views Asked by At

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\[\033[32m\]\$(parse_git_branch)\[\033[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="\[\033[01;32m\]\u@\h"
    local __cur_location="\[\033[01;34m\]\w"
    local __git_branch_color="\[\033[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="\[\033[35m\]$"
    local __last_color="\[\033[00m\]"
    export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt
2

There are 2 answers

0
lee-pai-long On

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.

0
VonC On

With Git 2.44 (Q1 2024), futureproof command line prompt support (in contrib/).

You can take inspiration from their patch, to display the name of the branch reliably based on HEAD (get rev-parse --abbrev-ref HEAD), which is now compited as:

case "$ref_format" in
files)
    if ! __git_eread "$g/HEAD" head; then
        return $exit
    fi

    if [[ $head == "ref: "* ]]; then
        head="${head#ref: }"
    else
        head=""
    fi
    ;;
*)
    head="$(git symbolic-ref HEAD 2>/dev/null)"
    ;;
esac

See commit fc134b4 (04 Jan 2024) by Patrick Steinhardt (pks-t).
See commit 4081d45 (08 Jan 2024) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 9ea8145, 19 Jan 2024)

git-prompt: stop manually parsing HEAD with unknown ref formats

Signed-off-by: Patrick Steinhardt

We're manually parsing the HEAD reference in git-prompt to figure out whether it is a symbolic or direct reference.
This makes it intimately tied to the on-disk format we use to store references and will stop working once we gain additional reference backends in the Git project.

Ideally, we would refactor the code to exclusively use plumbing tools to read refs such that we do not have to care about the on-disk format at all.
Unfortunately though, spawning processes can be quite expensive on some systems like Windows.
As the Git prompt logic may be executed quite frequently we try very hard to spawn as few processes as possible.
This refactoring is thus out of question for now.

Instead, condition the logic on the repository's ref format: if the repo uses the the "files" backend we can continue to use the old logic and read the respective files from disk directly.
If it's anything else, then we use git-symbolic-ref(1) to read the value of HEAD.

This change makes the Git prompt compatible with the upcoming "reftable" format.