在shell提示中显示git branch? [英] Showing git branch in shell prompt?

查看:238
本文介绍了在shell提示中显示git branch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的shell提示符显示当前的git分支名称。

I am trying to get my shell prompt to display the current git branch name.

我已经阅读了一些教程和博客文章等。知道我做的一切正确,但是似乎没有用。

I have read a few tutorials and blog posts etc. and as far as I understand I'm doing everything correctly but it doesn't seem to be working.

我希望提示符如下:

dannys-macbook:hillcrest-store [master]$

,但目前看起来像这样:

but currently it looks like this:

dannys-macbook:hillcrest-store danny$ 

我在〜/ .bash_profile中添加了以下内容:

I have added the following to ~/.bash_profile:

PATH=$PATH:/usr/local/bin; export PATH

COLOR1="\[\e[1;32m\]"
COLOR2='\[\e[1;1m\]'
COLOR3='\[\e[m\]'
GIT_STATUS=$(__git_ps1 " %s")
PROMPT_CHAR="$"

PROMPT="${COLOR1}\u@\h${COLOR3} \w${COLOR2}${GIT_STATUS} ${COLOR2}${PROMPT_CHAR$
PS1="$PROMPT"
export PS1

我不确定自己做错了什么,也许我应该在重置

I'm not sure what I'm doing wrong, maybe I should be 'resetting' the prompt somehow?

推荐答案

您希望PS1在每次显示提示时都被更新,以包含当前分支,即键入下一个命令,要求bash通过在precmd函数中再次设置PS1来执行此操作,bash在显示提示之前运行此函数:

You want PS1 to be updated to contain the current branch every time the prompt is displayed, that is, before you type the next command. Ask bash to do this by setting PS1 again in the precmd function. bash runs this function before showing you the prompt:

precmd() {
    PS1=...
}

这篇关于在shell提示中显示git branch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆