我尝试在bash提示符下添加我正在处理(签出)的git分支但没有成功..(同时保持我当前的路径显示活动目录/文件完好无损)我家里有一个.bashrc文件,但我也看到很多人提到.profile文件..
Von*_*onC 154
请注意,此类提示现在由contrib/completion /git-prompt.sh及其master变量管理.
- 将此文件复制到某处(例如
master/contrib/completion/git-prompt.sh).- 将以下行添加到您的
__git_ps1_branch_name:
source ~/.git-prompt.sh
Run Code Online (Sandbox Code Playgroud)
- 将您
~/.git-prompt.sh的调用更改.bashrc/.zshrc为命令替换:
Bash:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
ZSH:
setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
Run Code Online (Sandbox Code Playgroud)
但请注意,只有git 1.9.3(2014年5月)或更高版本允许您安全地显示该分支名称(!)
bash和zsh都将PS1的值置于参数扩展,命令替换和算术扩展中.
不是在
__git_ps1以两个或三个参数模式运行时包含原始的,未转义的分支名称,而是构造richardhansen引用包含分支名称的变量.因为shell不会递归扩展,所以这可以避免通过特制的分支名称执行任意代码,例如
'$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)'.
Run Code Online (Sandbox Code Playgroud)
什么狡猾的头脑会这样命名一个分支?;)(除了在xkcd中的妈妈)
如果你想要
PS1(我的PS1)颜色提示,这似乎很有效:
PS1='\[\e]0;\u@\h: \w\a\]\n${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\03??3[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ '
Run Code Online (Sandbox Code Playgroud)
一切都是不同的颜色,包括分支.
在Linux Mint 17.3 Cinnamon 64位:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[00m\]$(__git_ps1) \$ '
Run Code Online (Sandbox Code Playgroud)
par*_*ish 55
请按照以下步骤操作:(Linux)
编辑文件~/.bashrc,在其末尾输入以下行(如果是Mac,文件将是~/.bash_profile)
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
Run Code Online (Sandbox Code Playgroud)
现在,启动新的终端窗口,并尝试进入任何git-repo.将显示当前分支,并显示提示.
Eri*_*oie 36
1-如果你没有bash-completion ......:sudo apt-get install bash-completion
2-编辑.bashrc文件并检查(或添加):
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
Run Code Online (Sandbox Code Playgroud)
3- ...在你的提示行之前:export PS1='$(__git_ps1) \w\$ '
(__ git_ps1将显示你的git分支)
4-做 source .bashrc
编辑:
进一步阅读:不要重新发明轮子
jam*_*ing 20
以下是我如何配置提示以显示Git状态:
获取git-prompt脚本:
curl -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
Run Code Online (Sandbox Code Playgroud)
并自定义您的提示,在.bashrc文件中添加以下代码:
# Load Git functions
source ~/.git-prompt.sh
# Syntactic sugar for ANSI escape sequences
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
badgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset
# Prompt variables
PROMPT_BEFORE="$txtcyn\u@\h $txtwht\w$txtrst"
PROMPT_AFTER="\\n\\\$ "
# Prompt command
PROMPT_COMMAND='__git_ps1 "$PROMPT_BEFORE" "$PROMPT_AFTER"'
# Git prompt features (read ~/.git-prompt.sh for reference)
export GIT_PS1_SHOWDIRTYSTATE="true"
export GIT_PS1_SHOWSTASHSTATE="true"
export GIT_PS1_SHOWUNTRACKEDFILES="true"
export GIT_PS1_SHOWUPSTREAM="auto"
export GIT_PS1_SHOWCOLORHINTS="true"
Run Code Online (Sandbox Code Playgroud)
如果你想了解更多,你可以在这里获得所有的dotfiles:https://github.com/jamming/dotfiles
小智 9
对于mac,这非常有效:http://martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac/:
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
Run Code Online (Sandbox Code Playgroud)
首先,在您的主目录中打开您的 Bash 配置文件。使用默认编辑器打开和编辑bash_profile的最简单方法。
例如,我使用 VS Code 使用以下命令打开它:code .bash_profile。
然后只需将以下代码粘贴到您的 Bash 中即可。
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \W\[\033[32m\]\$(parse_git_branch)\[\033[00m\] $ "
Run Code Online (Sandbox Code Playgroud)
功能
parse_git_branch()
将获取分支名称,然后通过 PS1 您可以在终端中显示它。
这里,
\u = 用户名
@ = 静态文本
\h = 计算机名称
\w = 当前目录
$ = 静态文本
您可以更改或删除这些变量以进行更多自定义。
如果您第一次在终端中使用Git或配置后立即使用Git,有时可能看不到分支名称。
如果您遇到此问题,请不要担心。在这种情况下,只需创建一个示例存储库并在进行一些更改后提交即可。当commit命令执行一次时,终端会从此时开始查找git分支。
root:~/project# -> root:~/project(dev)#
Run Code Online (Sandbox Code Playgroud)
将以下代码添加到 ~/.bashrc 的末尾
force_color_prompt=yes
color_prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
unset color_prompt force_color_prompt
Run Code Online (Sandbox Code Playgroud)
我想要一个干净的解决方案,附加到现有提示而不是替换它。与其他解决方案一样,将其添加到 .bashrc 的底部
# function
parse_git_branch() {
if [ -n "$(git rev-parse --git-dir 2> /dev/null)" ]; then
echo ">> $(git rev-parse --abbrev-ref HEAD) >>"
fi
}
# environment customization
export PS1="\$(parse_git_branch)\n$PS1"
Run Code Online (Sandbox Code Playgroud)
此设置会产生如下所示的提示
>> branchname >>
user@host:~/current/path$
Run Code Online (Sandbox Code Playgroud)
另外,我喜欢在提示中添加一点颜色,这样它就更突出了,就像这样
export PS1="\e[0;36m\$(parse_git_branch)\e[0m\n$PS1"
Run Code Online (Sandbox Code Playgroud)
这会导致分支名称以青色显示