88

我在 .zshrc 中单独运行以下代码作为提示失败。这表明我显然没有名为 __git_ps1 的程序。它不在 MacPorts 中。

#1

PROMPT="$(__git_ps1 " \[\033[1;32m\] (%s)\[\033[0m\]")\$"$

#2

PROMPT="$(__git_ps1 " (%s)")\$"$

#3

# Get the name of the branch we are on
git_prompt_info() {
  branch_prompt=$(__git_ps1)
  if [ -n "$branch_prompt" ]; then
    status_icon=$(git_status)
    echo $branch_prompt $status_icon
  fi
}

# Show character if changes are pending
git_status() {
  if current_git_status=$(git status | grep 'added to commit' 2> /dev/null); then
    echo "☠"
  fi
}
autoload -U colors
colors
setopt prompt_subst
PROMPT='
%~%{$fg_bold[black]%}$(git_prompt_info)
→ %{$reset_color%}'

如何获得显示 Git 分支名称的提示?

4

9 回答 9

86

__git_ps1来自 git-completion.bash。在 zsh 中,您可能必须提供自己的函数来确定当前目录 git 分支。有很多关于zsh的git 提示的博客文章。

您只需:

  • 提供分支名称的函数
  • 启用提示(命令)替换
  • 将功能添加到您的提示中

例如

git_prompt() {
 ref=$(git symbolic-ref HEAD | cut -d'/' -f3)
 echo $ref
}
setopt prompt_subst
PS1=$(git_prompt)%#
autoload -U promptinit
promptinit

更新:使用 zsh vcs_info 模块而不是 git_prompt()

setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' actionformats \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats       \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{5}]%f '
zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{3}%r'

zstyle ':vcs_info:*' enable git cvs svn

# or use pre_cmd, see man zshcontrib
vcs_info_wrapper() {
  vcs_info
  if [ -n "$vcs_info_msg_0_" ]; then
    echo "%{$fg[grey]%}${vcs_info_msg_0_}%{$reset_color%}$del"
  fi
}
RPROMPT=$'$(vcs_info_wrapper)'
于 2009-07-14T23:10:33.783 回答
48

在混合返回键时,很多这些解决方案对我来说似乎很慢,所以这里有一个基本且快速的选项:

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

setopt PROMPT_SUBST
PROMPT='%9c%{%F{green}%}$(parse_git_branch)%{%F{none}%} $ '

您将收到如下所示的提示: ~/dev/project (feature-branch) $

于 2019-05-17T00:31:45.437 回答
47

这是 zsh 的扩展 git 提示:zsh-git-prompt

替代文字

于 2010-05-25T05:56:32.037 回答
40

ko-dos 的回答很棒,但我更喜欢比他使用的提示更能感知 git 的提示。具体来说,我喜欢提示本身中的暂存、未暂存和未跟踪的文件通知。使用他的回答和zsh vcs_info 示例,我想出了以下内容:

setopt prompt_subst
autoload -Uz vcs_info
zstyle ':vcs_info:*' stagedstr 'M' 
zstyle ':vcs_info:*' unstagedstr 'M' 
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:*' actionformats '%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
zstyle ':vcs_info:*' formats \
  '%F{5}[%F{2}%b%F{5}] %F{2}%c%F{3}%u%f'
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked
zstyle ':vcs_info:*' enable git 
+vi-git-untracked() {
  if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
  [[ $(git ls-files --other --directory --exclude-standard | sed q | wc -l | tr -d ' ') == 1 ]] ; then
  hook_com[unstaged]+='%F{1}??%f'
fi
}


precmd () { vcs_info }
PROMPT='%F{5}[%F{2}%n%F{5}] %F{3}%3~ ${vcs_info_msg_0_} %f%# '

这将创建一个提示,该提示模仿git status -s(可以在您的.gitconfig文件中配置)的彩色输出。一张图片在这里可能最有帮助:

迅速的

比较git status -s

在此处输入图像描述

如果您不喜欢彩色输出,或者更喜欢其他字符或提示结构,只需更改上述代码中的stagedstrunstagedstrhook_com[unstaged]值。

于 2012-10-17T13:37:08.593 回答
9

我只是重做了我的,因为我们有很长的分支名称在工作。如果它超过 35 个字符,它将用省略号截断。

parse_git_branch() {
    git_status="$(git status 2> /dev/null)"
    pattern="On branch ([^[:space:]]*)"
    if [[ ! ${git_status} =~ "(working (tree|directory) clean)" ]]; then
        state="*"
    fi
    if [[ ${git_status} =~ ${pattern} ]]; then
      branch=${match[1]}
      branch_cut=${branch:0:35}
      if (( ${#branch} > ${#branch_cut} )); then
          echo "(${branch_cut}…${state})"
      else
          echo "(${branch}${state})"
      fi
    fi
}

setopt PROMPT_SUBST
PROMPT='%{%F{blue}%}%9c%{%F{none}%}$(parse_git_branch)$'

(我为我为此感到骄傲而感到尴尬。)

于 2018-10-26T21:20:08.013 回答
5

这已经有很多很好的答案,对于不想使用zshconfig 和git.

然而,我发现自己想要的只是

working-directory (git-branch-color-if-uncommitted) %          24h-timestamp
git_branch_test_color() {
  local ref=$(git symbolic-ref --short HEAD 2> /dev/null)
  if [ -n "${ref}" ]; then
    if [ -n "$(git status --porcelain)" ]; then
      local gitstatuscolor='%F{red}'
    else
      local gitstatuscolor='%F{green}'
    fi
    echo "${gitstatuscolor} (${ref})"
  else
    echo ""
  fi
}
setopt PROMPT_SUBST
PROMPT='%9c$(git_branch_test_color)%F{none} %# '

# add 24h time the right side
RPROMPT='%D{%k:%M:%S}'

提示着色示例

这通过

  • 允许通过设置包含函数 ( $()) PROMPT_SUBST(感谢Phil's 2017 answer!
  • 如果当前目录是 git 存储库,则声明一个函数git_branch_test_color来调用分支 ref .. 以及这是否成功(测试标准输出) (感谢对arg 的一些评论!) git
    symbolic-ref
    • 如果不是 git repo,则只回显一个空字符串""
  • 如果初始调用确实成功(确实收集了标准输出),则当前目录是一个 git 目录
    git status --porcelain用于确定是否有任何文件已更改或是否新/删除
    • 不变的绿色前缀
    • 更改的红色前缀(无论它们是什么)

请注意,所有此类提示在已挂载的目录上可能会很慢(尤其是 via sshfs)。如果遇到这种情况,我建议尽可能对这些目录进行特殊处理(也许创建一个新的 shell 函数来检查 cd 是否在您的集合中)或始终安装在同一路径中并忽略它(例如vcs_info

于 2020-11-28T01:12:24.980 回答
3

对于任何对维护的现成解决方案感兴趣的人,请安装https://github.com/ohmyzsh/ohmyzsh - 它默认打开一个 git 插件。我一直在寻找这样的东西 - 不费吹灰之力的预先创建的配置,效果很好。

于 2020-09-16T11:39:15.757 回答
3

如果您的所有提示看起来都很好,但它不起作用,例如:

function git_branch(){                                                                                                                 
    ref=$(git symbolic-ref --short --quiet HEAD 2>/dev/null)        
    if [ -n "${ref}" ]; then                                                    
        echo "(""$ref"")"                                                       
    fi                                                                          
}                                                                               
                                                                                
setopt PROMPT_SUBST                                                             

PROMPT="%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m:%{$fg[yellow]%}%1~%{$reset_color%} %{%F{green}%}$(git_branch)%{%F{none}%}$ "

PROMPT 必须设置为使用单引号而不是双引号引起来的字符串。

PROMPT='%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m:%{$fg[yellow]%}%1~%{$reset_color%} %{%F{green}%}$(git_branch)%{%F{none}%}$ '
于 2020-11-09T16:50:41.787 回答
0

谢谢你的链接!

我根据他们做了以下提示

     # get the name of the branch we are on
     git_prompt_info() { 
         git branch | awk '/^\*/ { print $2 }'
     }
     get_git_dirty() { 
       git diff --quiet || echo '*'
     }

     autoload -U colors
     colors     
     setopt prompt_subst

     PROMPT='%{$fg[blue]%}%c %{$fg_bold[red]%}$(git_prompt_info)$(get_git_dirty)%{$fg[blue]%} $ %{$reset_color%}'                                           

     RPROMPT='%{$fg[green]%}%1(j.%j.)'        

请提出任何改进建议。

于 2009-07-15T00:02:14.817 回答