bash 提示中的 Git 分支名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17811327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git branch name in prompt
提问by user2610733
I am able to show the git branch name in the shell prompt. But whenever I am using screen I am getting
我能够在 shell 提示中显示 git 分支名称。但是每当我使用屏幕时,我都会得到
bash: parse_git_branch: command not found
and git branch is not shown. Please help me get this in the screen sessions also.
并且没有显示 git branch。请帮助我在屏幕会话中也得到这个。
I have following in my .bash_profile.
我的 .bash_profile 中有以下内容。
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/
}
export PS1="[\W$(parse_git_branch)]$ "
I don't have .git-completion.bash
我没有 .git-completion.bash
System specs:
系统规格:
- OS: OSX 10.8.4
- Terminal & iTerm2
- Screen version: 4.00.03 (FAU) 23-Oct-06
- 操作系统:OSX 10.8.4
- 终端 & iTerm2
- 屏幕版本:4.00.03 (FAU) 23-Oct-06
回答by koomie
I had the same issue when running under screen and was able to resolve by moving the definition of the parse_git_branch()
function from .bash_profile
to .bashrc
.
在屏幕上运行,当我有同样的问题,并能够通过移动的定义,以解决parse_git_branch()
从功能.bash_profile
到.bashrc
。
回答by Simon Soriano
When you open your terminal, .bash_profile
is executed and therefore PS1
is defined. Then you execute screen, and screen reads the environment variable PS1
which includes a call to parse_git_branch
and tries to parse it. But, since screen didn't execute .bash_profile
the function parse_git_branch
is not defined inside screen.
当您打开终端时,会.bash_profile
被执行并因此PS1
被定义。然后执行 screen,screen 读取环境变量PS1
,其中包括parse_git_branch
对它的调用并尝试解析它。但是,由于 screen 没有执行.bash_profile
该函数parse_git_branch
在 screen 中没有定义。
Move the definition of PS1
to .bashrc
because both, screen and iTerm execute it.
移动PS1
to的定义,.bashrc
因为 screen 和 iTerm 都执行它。
回答by Gilad Halevy
This is much simpler and avoids the unnecessary sed:
这要简单得多,并且避免了不必要的 sed:
parse_git_branch () {
while read -r branch; do
[[ $branch = \** ]] && current_branch=${branch#* }
done < <(git branch 2>/dev/null)
[[ $current_branch ]] && printf ' [%s]' "$current_branch"
}
回答by Mazel Tov
I had the same error in OS X High Sierra when switching to root or when starting to ssh-agent /bin/bash
I resolved it to put it in /etc/bashrc
with check if i am root
切换到 root 或开始时,ssh-agent /bin/bash
我在 OS X High Sierra 中遇到了同样的错误我解决了它/etc/bashrc
并检查我是否是 root
if [[ $UID == 0 ]]; then
PS1="\[\e[1;31;40m\]\u@\h \W\[\e[0m\]$ "
else
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}
PS1="\u@\h \[3[32m\]\w\[3[33m\]$(parse_git_branch)\[3[00m\] $ "
fi
回答by Nick Tomlin
You are missing a '
at the end of your sed statement:
您'
在 sed 语句的末尾缺少一个:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ ()/'
}
export PS1="[\W$(parse_git_branch)]$ "
Othewerise, it seems to work for me in bash-3.2
Othewerise,它似乎对我有用 bash-3.2