You are not logged in.

#1 2019-12-22 20:38:28

damo
....moderator....
Registered: 2015-08-20
Posts: 5,412

Neat PS1 setting for git users

If you add this to .bashrc, then it produces a handy visual reminder for which git branch is being worked on. Very useful to not mess with master when you are supposed to be working on a different branch!

parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;36m\]\u@\h: \w\[\033[00;31m\]$(parse_git_branch)\[\033[00m\] \$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

I have set mine to use a different colour for the git branch:

    damo@debian: ~/github/scripts(testing) $ git status
    On branch testing
    Your branch is up to date with 'origin/testing'.
    damo@debian: ~/github/scripts(testing) $ git checkout master
    Switched to branch 'master'
    Your branch is up to date with 'origin/master'.
    damo@debian: ~/github/scripts(master) $


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Online

#2 2019-12-22 23:24:39

twoion
ほやほや
Registered: 2015-08-10
Posts: 2,578

Re: Neat PS1 setting for git users

I'm using

prompt_git ()       
{                                                               
    git symbolic-ref HEAD &> /dev/null || return 1;
    local HEAD="$(git symbolic-ref HEAD 2>/dev/null)";                                                                                
    local BRANCH="${HEAD#refs/heads/}";                                                                                               
    printf '%b(git:%s)' "\033[0;34m" "${BRANCH:-unknown}"       
}
PS1='\[\033[0;33m\]\h:\[\033[1;33m\]\w $(prompt_git)\n\[\033[1;33m\] $\[\033[m\] '

Git also comes with a pretty complex upstream git-prompt.sh (https://github.com/git/git/blob/master/ … -prompt.sh) which you can source; it provides a function __git_ps1 which also renders a prompt. It's pretty configurable but I never used it. It should be installed as /usr/share/git/git-prompt.sh or /usr/lib/git-core/git-sh-prompt on Debian.


At the end of the river the sundown beams

Offline

#3 2019-12-23 16:07:21

damo
....moderator....
Registered: 2015-08-20
Posts: 5,412

Re: Neat PS1 setting for git users

I've added red text in your function, as a warning to myself when in the master branch  ops

prompt_git ()       
{                                                               
    git symbolic-ref HEAD &> /dev/null || return 1;
    local HEAD="$(git symbolic-ref HEAD 2>/dev/null)";                                                                                
    local BRANCH="${HEAD#refs/heads/}"; 
    if [[ $BRANCH = "master" ]];then                                                                                              
        printf '%b(git:%s)' "\033[0;31m" "${BRANCH:-unknown}"       
    else
        printf '%b(git:%s)' "\033[0;32m" "${BRANCH:-unknown}"  
    fi
}

Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Online

Board footer

Powered by FluxBB