![](/img/trans.png)
[英]How Do I Display My Git Branch In My Terminal & Have It Automatically Update?
[英]How Do I Display My Active Git Branch In The Bash Terminal On Windows?
我在我的 MacBook 上完美地设置了终端和提示,但是在设置我的 PC 时我遇到了一个问题,它不会显示我当前的 git 分支并且我收到一个错误。 任何帮助将不胜感激。
下面的文字是我的 ~/.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;
以下是我在“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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.