Home > OS >  How Do I Display My Active Git Branch In The Bash Terminal On Windows?
How Do I Display My Active Git Branch In The Bash Terminal On Windows?

Time:10-17

I have my terminal and prompt set up perfectly on my MacBook but when setting up my PC I ran into an issue, It wont display my current git branch & I am getting an error. Any help would be greatly appreciated.

Below Text Is My ~/.bashrc & ~/.bash_profile

# colored text.
GREEN="\[\033[0;32m\]"
CYAN="\[\033[0;36m\]"
RED="\[\033[0;31m\]"
PURPLE="\[\033[0;35m\]"
BROWN="\[\033[0;33m\]"
LIGHT_GRAY="\[\033[0;37m\]"
LIGHT_BLUE="\[\033[1;34m\]"
LIGHT_GREEN="\[\033[1;32m\]"
LIGHT_CYAN="\[\033[1;36m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_PURPLE="\[\033[1;35m\]"
YELLOW="\[\033[1;33m\]"
WHITE="\[\033[1;37m\]"
BOLD=$(tput bold);    # bold text
orange=$(tput setaf 166);      
red=$(tput setaf 001);      
blue=$(tput setaf 004);    
pink=$(tput setaf 005);    
teal=$(tput setaf 006);    
green=$(tput setaf 71);    
white=$(tput setaf 15);    
reset="\[\033[0m\]" #0m restores to the terminal's default colour

# Display current git branch in terminal.

git_branch() {

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

}

# command prompt

PS1="\[${BOLD}\]\n";       # Display prompt text in BOLD & fresh line.

PS1 ="\[${blue}\]User:" # Display "User:" text.

PS1 ="\[${orange}\]\u ";   # Display active user.

PS1 ="\[${blue}\]Host:";   # Display "Host:" text.

PS1 ="\[${orange}\]\h ";   # Display active host.

PS1 ="\[${blue}\]Branch:" # Display "Branch:" text.

PS1 ="\[${orange}\]"'$(git_branch) ' # Call git_branch to be displayed.

PS1 ="\[${blue}\]Directory:";   # Display "Directory:" text.

PS1 ="\[${orange}\]\W ";        # Display working directory path.

PS1 ="\n";                      # Create a new line to write on.

PS1 ="\[${white}\]-> \[${reset}\]";   # Display "$" & Color reset.

# Export file to be used in terminal (source ~/.bashrc)

export PS1;

Below Is The Error I Get When I "source ~/.bashrc"

\bash: command substitution: line 1: syntax error near unexpected token )'

\bash: command substitution: line 1: \git_branch)'

CodePudding user response:

I prefer to use a PROMPT_COMMAND that will be executed every time you get a prompt and will therefore update it accordingly.

Example that goes into .bashrc:

tputps () {
        echo -n '\['
        tput "$@"
        echo -n '\]'
}

prompt_builder () {
        # username
        tputps setaf 2
        echo -n '\u'

        # if I'm in a git repo, add some info from that:
        commit=$(git rev-parse -q --short HEAD 2>/dev/null)
        if [[ -n $commit ]]; then
            tputps setaf 140
            echo -n " $commit"
        fi
        branch=$(git branch --show-current 2>/dev/null)
        if [[ -n $branch ]]; then
            tputps setaf 10
            echo -n " $branch"
        fi

        # directory
        tputps setaf 208
        echo -n ' \w'
        tputps sgr0 0
}

prompt_cmd () {
        PS1="$(prompt_builder) \$ "
}

export PROMPT_COMMAND=prompt_cmd
  • Related