在不使用自定义bash配置文件的Mac终端上显示git状态

在不使用自定义bash配置文件的Mac终端上显示git状态,git,bash,macos,github,Git,Bash,Macos,Github,我想在通过终端进行git回购时显示我的当前分支状态(如*表示已修改),如下所示。但是,即使我切换到git回购,它也总是显示以下内容 ᴾᴋᴹɴ iFeelNewsBot master wrong... ↪ 当我在终端中打开一个新的选项卡时,它会显示当前git分支的正确文本呈现。应该是什么样子 ᴾᴋᴹɴ iFeelNewsBot master * ↪ 下面是我的自定义bash配置文件代码 # user name and color USER_NAME='mr.universe'; TRAIN

我想在通过终端进行git回购时显示我的当前分支状态(如*表示已修改),如下所示。但是,即使我切换到git回购,它也总是显示以下内容

ᴾᴋᴹɴ iFeelNewsBot master wrong... ↪ 
当我在终端中打开一个新的选项卡时,它会显示当前git分支的正确文本呈现。应该是什么样子

ᴾᴋᴹɴ iFeelNewsBot master * ↪ 
下面是我的自定义bash配置文件代码

# user name and color
USER_NAME='mr.universe';
TRAINER_TITLE='ᴾᴋᴹɴ'
USER_NAME_COLOR='\[\033[00m\]';
END_COLOR='\e[0m';

# \W = current working path
# \u = user
function parse_git_dirty {
  gitStatus=$(git status 2> /dev/null | tail -n1)
  clean="nothing to commit, working directory clean"
  if [[ -d "./.git" ]]
  then
    [[ $gitStatus != $clean ]] && echo "*" || echo "="
  elif [[ ! -d "./.git" ]]
  then
    echo "wrong...."
  else
    echo "STOP"
  fi
}

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

export PS1="\[\033[40;02m\]$TRAINER_TITLE \W \[\033[36;02m\]\[\033[29;00m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2) $(parse_git_dirty) ↪ "

假设git存储库中的每个目录都有
.git
,但只有存储库的顶部目录才有
.git
目录。因此,第一个
if
失败,并且
elif
检查
.git
目录是否不存在。看起来结果是正确的,但打印的是“错误的”。

您假设git存储库中的每个目录都包含
.git
,但只有存储库的顶部目录包含
.git
目录。因此,第一个
if
失败,并且
elif
检查
.git
目录是否不存在。看起来结果是正确的,但打印的是“错误的”。

如果您是:


这比查找
.git
文件夹更可靠。

您可以检查您是否:


这比查找
.git
文件夹更可靠。

它看起来像是从未使用过
parse\u git\u branch
函数。它看起来像是从未使用过
parse\u git\u branch
函数。在git repo中显示
“inside git repo”
可以工作,但当我切换到一个新的非git目录时,
“not in git repo”
在我打开新选项卡或通过切换到新分支刷新终端之前不会显示,您是说
git checkout aNewBranch
?“刷新终端”意味着什么?我的错误是指将
cd
插入一个新的非git目录。我的意思是关闭并重新打开我的终端。如果你
cd
到一个非git目录,那么脚本显示“非git repo”是理所当然的,不是吗?它不显示,而是显示
“非git repo”
,直到我在我的mac终端中打开一个新选项卡,或者有趣地运行这个命令
source~/.bash\u profile
显示
“inside git repo”
可以工作,但是当我切换到一个新的非git目录
时,“not in git repo”
不会显示,直到我打开一个新选项卡或通过切换到一个新分支刷新我的终端,你是说
git checkout aNewBranch
?还有什么“刷新终端”“意思是?我的错误,我的意思是
cd
到一个新的非git目录。我的意思是关闭并重新打开我的终端。如果你
cd
到一个非git目录,那么脚本显示“非git repo”是理所当然的,不是吗?它不显示,而是显示
“非git repo”
,直到我在我的mac终端中打开一个新选项卡,或者有趣地运行这个命令
source~/.bash\u profile
inside_git_repo="$(git -C $PWD rev-parse --is-inside-work-tree 2>/dev/null)"

if [ "$inside_git_repo" ]; then
  echo "inside git repo"
else
  echo "not in git repo"
fi