Posted on

To make that works, you just need to have access to the terminal, and also to some text editor, like vim.

We should add a funtion to check if you are at a git project, and return the branch.

Git already provides a helper function for that __git_ps1. For me it's not perfect because I don't want to have a space between my pc name and the $ character.

To fix that, I created my helper at .bashrc:

# enable git branch name
parse_git_branch() {
    if [[ -n "$(__git_ps1)" ]]; then
        echo "$(__git_ps1)"
    fi
}

This helper is pretty simple, it validates if I have any output with the git helper, if positive returns my branch.

Then you can just update your ps1, or ps1 function like below:

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

Yeah, I know this is not a big tutorial of how to make that, it's more a way that I can use later when I format my pc. 🤪