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


当前回答

这里是一个简单的干净的版本,我使用:link

其他回答

如果你用的是鱼壳,那就很直接了。 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选项

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

1-如果你没有bash-completion…: sudo apt-get install bash-completion

2-编辑你的.bashrc文件并检查(或添加):

if [ -f /etc/bash_completion ]; then
  . /etc/bash_completion
fi

3 -…export PS1='$(__git_ps1) \w\$ ' (__git_ps1将显示你的git分支)

4- do source .bashrc

编辑:

进一步阅读:不要重新发明轮子

vim ~/.bash

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\] $"

要反映最新的更改,请运行以下命令

source ~/.bashrc

输出:

chandrakant@NDL41104 ~/Chandrakant/CodeBase/LaravelApp (development) $

我尝试了一个python小脚本,放在....文件夹中 “gitprompt”文件

#!/usr/bin/env python
import subprocess, os
s = os.path.join(os.getcwd(), '.git')
def cut(cmd):
    ret=''
    half=0
    record = False
    for c in cmd:
        if c == "\n":
            if not (record):
                pass
            else:
                break
        if (record) and c!="\n":
            ret = ret + c
        if c=='*':
            half=0.5
        if c==' ':
            if half == 0.5:
                half = 1
        if half == 1:
            record = True
    return ret
if (os.path.isdir(s)):
    out = subprocess.check_output("git branch",shell=True)
    print cut(out)
else:
    print "-"

让它可执行之类的

然后相应地调整bash提示符,如下:

\u:\w--[$(gitprompt)] \$ 

对于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\] $ "