我尝试在bash提示符上添加我目前正在工作的git分支(签出),但没有成功。(同时保持我的当前路径显示活动目录/文件完整) 我有一个。bashrc文件在我的家,但我也看到很多人提到。profile文件…


当前回答

对于mac来说,这个很好用:http://martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac/:

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

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

其他回答

我想要一个干净的解决方案,附加到现有的提示,而不是取代它。与其他解决方案一样,将此添加到.bashrc的底部

# function
parse_git_branch() {
  if [ -n "$(git rev-parse --git-dir 2> /dev/null)" ]; then
    echo ">> $(git rev-parse --abbrev-ref HEAD) >>"
  fi
}

# environment customization
export PS1="\$(parse_git_branch)\n$PS1"

此设置产生如下提示符

>> branchname >>
user@host:~/current/path$

此外,我喜欢给提示符添加一些颜色,这样它就会更好地突出

export PS1="\e[0;36m\$(parse_git_branch)\e[0m\n$PS1"

是什么导致分支名称出现在CYAN中

parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]$(parse_git_branch)\n\$ '

请按照以下步骤操作:(Linux)

编辑文件~/。bashrc,在它的末尾输入以下行(在Mac中,文件将是~/.bash_profile)

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "

现在,启动新的终端窗口,并尝试进入任何git-repo。将显示当前分支,并显示提示符。

4更多信息- MAC/Linux

root:~/project#  -> root:~/project(dev)# 

将以下代码添加到~/.bashrc的末尾

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

如果你用的是鱼壳,那就很直接了。 Fish是一个互动的贝壳,里面有很多好吃的东西。您可以使用apt-get安装它。

sudo apt-get install fish

然后,您可以使用

> fish_config 
Web config started at 'http://localhost:8001/'. Hit enter to stop.
Created new window in existing browser session.

现在登录http://localhost:8001/ 打开提示选项卡并选择经典的+ git选项

现在点击使用提示按钮,你就设置好了。