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


当前回答

请按照以下步骤操作:(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

按照以下步骤在ubuntu终端中显示你的GIT repo分支的名称:

步骤1:打开终端,使用下面的命令编辑.bashrc。

vi . bashrc

步骤2:在.bashrc文件的末尾添加以下一行:

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

步骤3:在根(home)目录下运行。bashrc:

- = ytet -伊甸园字幕组= -翻译

第四步:重启并打开终端,检查cmd。导航到GIt回购目录路径,就完成了。:)

首先,在主目录中打开Bash概要文件。使用默认编辑器打开和编辑bash_profile的最简单方法。

例如,我使用VS Code使用以下命令打开它:Code .bash_profile。

然后只需将以下代码粘贴到您的Bash。

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

这个函数

parse_git_branch ()

将获取分支名称,然后通过PS1,你可以在你的终端显示它。

在这里,

\u =用户名 @ =静态文本 \h =计算机名 \w =当前目录 $ =静态文本

您可以更改或删除这些变量以进行更多的自定义。


如果您在终端中第一次使用Git,或者在配置后立即使用,有时可能看不到分支名称。

如果你遇到这个问题,不要担心。在这种情况下,只需创建一个示例存储库,并在进行一些更改后提交。当commit命令执行一次时,终端将从中找到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

编辑:

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

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