如何在 Windows 上的 Bash 终端中显示我的活动 Git 分支?

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

我在我的 MacBook 上完美地设置了我的终端和提示,但是在设置我的 PC 时我 运行 遇到了问题,它不会显示我当前的 git b运行ch & 我收到一个错误。任何帮助将不胜感激。

下面的文字是我的~/.bashrc & ~/.bash_profile

# colored text.
GREEN="\[3[0;32m\]"
CYAN="\[3[0;36m\]"
RED="\[3[0;31m\]"
PURPLE="\[3[0;35m\]"
BROWN="\[3[0;33m\]"
LIGHT_GRAY="\[3[0;37m\]"
LIGHT_BLUE="\[3[1;34m\]"
LIGHT_GREEN="\[3[1;32m\]"
LIGHT_CYAN="\[3[1;36m\]"
LIGHT_RED="\[3[1;31m\]"
LIGHT_PURPLE="\[3[1;35m\]"
YELLOW="\[3[1;33m\]"
WHITE="\[3[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="\[3[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/* \(.*\)/()/'

}

# 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;

下面是我在“source ~/.bashrc”时得到的错误

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

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

我更喜欢使用 PROMPT_COMMAND,它会在您每次收到提示时执行,因此会相应地更新它。

进入 .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