linux终端git带颜色,如何在Bash提示符中显示带颜色的git分支?

问题描述

我在git存储库中工作时使用this guide来显示gnome终端(Ubuntu 15.10)中的分支名称。基于上面我现在在〜/.bashrc文件中有以下内容:

# uncomment for a colored prompt, if the terminal has the capability; turned

# off by default to not distract the user: the focus in a terminal window

# should be on the output of commands, not on the prompt

#force_color_prompt=yes

# Add git branch if its present to PS1

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;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '

else

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '

fi

unset color_prompt force_color_prompt

结果我现在得到:

d721ca2bb0574fde7939a3345f56becd.png

所以它有效。但为什么我的user @ host的着色被删除了?我还希望分支名称应该是彩色的。在它看起来像这样之前:

04bf0a6ce87d4a81b24f3cb7fc5e0afc.png

更新:我现在尝试了这个指南:

将此添加到.bashrc:

parse_git_branch() {

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

}

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

这工作:

8326949b705466a8de76380a860cbaa0.png

请注意.bashrc我也有这个(默认):

# uncomment for a colored prompt, if the terminal has the capability; turned

# off by default to not distract the user: the focus in a terminal window

# should be on the output of commands, not on the prompt

#force_color_prompt=yes

我还没有找到该片段给出正确结果而其他版本没有的原因。有什么输入吗?

这是我的.bashrc版本,其中启用了旧代码段不起作用:

最佳解决办法

这个片段:

# Add git branch if its present to PS1

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;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '

else

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '

fi

是要替换默认的提示定义:

if [ "$color_prompt" = yes ]; then

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

else

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

fi

结束于:

unset color_prompt force_color_prompt

您发布的.bashrc显示您在默认提示定义和unset color_prompt force_color_prompt(第64行)之后添加它。

使用代码段替换默认提示定义或保留~/.bashrc,并在第64行注释默认提示定义和unset color_prompt force_color_prompt:

所以.bashrc的一部分看起来像

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;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\] $(parse_git_branch)\[\033[00m\]\$ '

else

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '

fi

# THE SIX LINES BELOW are the default prompt and the unset (which were in the original .bashrc)

#if [ "$color_prompt" = yes ]; then

# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

#else

# PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

#fi

#unset color_prompt force_color_prompt

bff4d7a6b59adfe68f44a0dc9a4ce798.png

次佳解决办法

Ubuntu:在终端上显示您的分支名称

在〜/.bashrc文件中添加这些行

# Show git branch name

force_color_prompt=yes

color_prompt=yes

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;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '

else

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '

fi

unset color_prompt force_color_prompt

使用以下命令重新加载.bashrc文件:

$ source ~/.bashrc

第三种解决办法

In Terminal

mkdir ~/.bash

Copy the raw git-prompt.sh file from git contrib in to the ~/.bash directory: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

Inside ~/.bashrc or ~/.bash_profile (choose the file where you normally put any bash customizations/setup), add the lines:

source ~/.bash/git-prompt.sh # Show git branch name at command prompt export GIT_PS1_SHOWCOLORHINTS=true # Option for git-prompt.sh to show branch name in color # Terminal Prompt: # Include git branch, use PROMPT_COMMAND (not PS1) to get color output (see git-prompt.sh for more) export PROMPT_COMMAND='__git_ps1 "\w" "\n\\\$ "' # Git branch (relies on git-prompt.sh)

As long as you’re inside a git repo, your Bash prompt should now show the current git branch in color signifying if its got uncommitted changes.

第四种办法

转到主文件夹

单击Ctrl + h以显示隐藏文件。

打开。 bashrc文件并在最后粘贴下一个:

parse_git_branch() {

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

}

export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "

如果您打开终端,请关闭并再次打开。请享用!!

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值