获取一个提示,指示Zsh中的Git-branch

Léo*_* 준영 56 git zsh prompt

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

#1

PROMPT="$(__git_ps1 " \[\033[1;32m\] (%s)\[\033[0m\]")\$"$
Run Code Online (Sandbox Code Playgroud)

#2

PROMPT="$(__git_ps1 " (%s)")\$"$
Run Code Online (Sandbox Code Playgroud)

#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%}'
Run Code Online (Sandbox Code Playgroud)

你怎么能得到一个显示Git分支名称的提示?

ko-*_*dos 65

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

您只需要:

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

例如

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

更新:使用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)'
Run Code Online (Sandbox Code Playgroud)

  • git_prompt是错误的,如果你有一个分支/它不起作用.请改用`cut -d'/' - f3-`. (4认同)
  • @balki `actionformats` 变为 `(%s)-[%b|%a]` 忽略颜色。此格式在特殊操作期间使用,用“%a”描述该操作。恕我直言,这是最有用的功能:例如,对于 git 来说,它会告诉您何时处于变基过程中(在尝试解决合并冲突时我经常忘记这一点)。 (2认同)

Oli*_*ier 34

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

替代文字

  • 您的方法与vcs_info相比如何?你的脚本似乎正在重新发明轮子?(虽然没有阻止我使用它,但我没有意识到vcs_info) (2认同)

Chr*_*her 26

ko-dos的答案很棒,但我更喜欢一个比他使用的更敏捷的提示.具体来说,我喜欢提示本身中的分阶段,未分阶段和未跟踪的文件通知.使用他的答案和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%# '
Run Code Online (Sandbox Code Playgroud)

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

提示

git status -s:相比:

在此输入图像描述

如果你不喜欢彩色输出,或宁愿其它字符或提示建设,只是改变了stagedstr,unstagedstrhook_com[unstaged]在上面的代码值.

  • 这是 `vcs_info` 的一个很棒的特定于 git 的用法。但有一件事:“git status”*慢*。我使用了`git ls-files --other --directory --exclude-standard | sed q | 厕所-l | tr -d ' '` 相反,它要快得多。 (2认同)

ti7*_*ti7 9

这已经有很多很好的答案,对于那些不想使用除了 configzshgit.

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

working-directory (git-branch-color-if-uncommitted) %          24h-timestamp
Run Code Online (Sandbox Code Playgroud)
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}'
Run Code Online (Sandbox Code Playgroud)

提示着色示例

这是由

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

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


Dan*_*ark 8

我只是重做了我的,因为我们有很长的分支名称在工作。如果它超过 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)$'
Run Code Online (Sandbox Code Playgroud)

(我为我为此感到多么自豪而感到尴尬。)


Phi*_*hil 5

当我混入回车键时,许多这些解决方案对我来说似乎很慢,因此这是一个基本且快速的选项:

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}%} $ '
Run Code Online (Sandbox Code Playgroud)

您会看到如下提示: ~/dev/project (feature-branch) $

  • `git symbolic-ref --short HEAD 2> /dev/null` 会给你一个需要更少解析的答案。 (6认同)
  • @Manish 在你的 ~/.zshrc 文件中 (2认同)

HVN*_*ing 5

如果您的所有提示看起来不错但不起作用,例如:

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}%}$ "
Run Code Online (Sandbox Code Playgroud)

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

PROMPT='%{$fg[red]%}%n%{$reset_color%}@%{$fg[blue]%}%m:%{$fg[yellow]%}%1~%{$reset_color%} %{%F{green}%}$(git_branch)%{%F{none}%}$ '
Run Code Online (Sandbox Code Playgroud)