【问题标题】:Way to show merging status in command line in git在 git 的命令行中显示合并状态的方法
【发布时间】:2014-09-01 15:51:58
【问题描述】:

我目前使用以下 bash 脚本配置了 git 以显示我所在的分支(以及我的命令行以显示我所在的目录):

export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\] \[\033[33;1m\]\w\[\033[m\] (\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)) \$  \n$ "
export PS2="$ "

我想知道是否有办法显示“合并”状态,这样当我合并时它也会在命令行中显示merging(在我所在的分支之后)。我在其他地方看到过这个,所以我很确定这是可能的,但不知道如何。

【问题讨论】:

  • 您可以检查.git 文件夹中是否存在MERGE_HEAD 文件,以确定您是否处于合并过程中。

标签: git bash command-line .bash-profile


【解决方案1】:

正如@Zeeker 建议的那样,你基本上可以有这样的东西:

PS1='$([[ -e .git/MERGE_HEAD ]] && echo "merging ")\$ '

或者

PS1="\$([[ -e .git/MERGE_HEAD ]] && echo 'merging ')\\\$ "

您可以将其与您当前的PS1 合并;自定义echo merging 或使用printf 也可以生成ANSI 代码。

【讨论】:

  • 很抱歉,如何将其与我的ps1 合并而不覆盖任何内容?
【解决方案2】:

您可以在this git-ps1 脚本中看到更完整的状态指示器,其中包含变基和合并。

local g="$(git rev-parse --git-dir 2>/dev/null)"
if [ -n "$g" ]; then
  local r
  local b
  if [ -d "$g/rebase-apply" ]
  then
    if test -f "$g/rebase-apply/rebasing"
    then
      r="|REBASE"
    elif test -f "$g/rebase-apply/applying"
    then
      r="|AM"
    else
      r="|AM/REBASE"
    fi
    b="$(git symbolic-ref HEAD 2>/dev/null)"
  elif [ -f "$g/rebase-merge/interactive" ]
  then
    r="|REBASE-i"
    b="$(cat "$g/rebase-merge/head-name")"
  elif [ -d "$g/rebase-merge" ]
  then
    r="|REBASE-m"
    b="$(cat "$g/rebase-merge/head-name")"
  elif [ -f "$g/MERGE_HEAD" ]
  then
    r="|MERGING"
    b="$(git symbolic-ref HEAD 2>/dev/null)"

对于合并部分,它确实测试了MERGE_HEAD 文件,如suggested by Zeeker

【讨论】:

  • 如何将它添加到我现有的export ps 中?
  • @Startec 实际上,该脚本定义了变量'r',然后可以使用'$r'将其添加到您的PS1github.com/jbalogh/dotfiles/blob/…
  • 对不起,我仍然对这段代码和那个要点有点迷茫。它说# returns text to add to bash PS1 prompt (includes branch name) 这是我要在.bash_profile 中添加的内容吗?抱歉,我是 bash 脚本的新手。
  • @Startec 是的,或者你的 .bashrc,像这样:github.com/jbalogh/dotfiles/blob/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
  • 2011-11-08
  • 2016-04-10
  • 1970-01-01
相关资源
最近更新 更多