首页 > 解决方案 > 如何在 Mac 终端上显示完整路径和 git 分支

问题描述

我发现某人的 mac 终端可以像这样显示完整路径和 git 分支: git branch and full path on terminal

我看了一些资料,只能在终端上单独显示完整路径或 git 分支。有人可以帮助我吗?

标签: gitmacosterminal

解决方案


编辑:

bash_profile在终端中打开:

nano ~/.bash_profile

然后在bash_profile-file 中添加以下内容:

# to see always fullpath and current git-branch
export PS1="\[\033[36m\]\u\[\033[m\]: \[\033[34m\][\w]\[\033[m\]\[\033[32m\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo)\$(parse_git_branch)\[\033[m\] \$ "

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

之后,重新启动终端,然后它的工作方式如下:

username: [~/path/to/current/directory] current-branch $ 

推荐阅读