Adding git branch on the Bash command prompt
我尝试在bash提示符下添加我当前正在使用的git分支(已签出),但没有成功..(同时保持了当前路径,该路径完整显示了活动目录/文件)
我家中有一个.bashrc文件,但我也看到很多人提到.profile文件。
git 1.9.3或更高版本:使用
Git提供了一个名为
prints text to add to bash PS1 prompt (includes branch name)
其最基本的用法是:
1 2 | $ __git_ps1 (master) |
它还需要一个可选的格式字符串:
1 2 | $ __git_ps1 'git:[%s]' git:[master] |
如何获得
首先,将文件复制到某个位置(例如
选项1:使用文件系统上的现有副本。示例(Mac OS X 10.15):
1 2 | $ find / -name 'git-prompt.sh' -type f -print -quit 2>/dev/null /Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh |
选项2:从GitHub提取脚本。
接下来,将以下行添加到您的
1 | source ~/.git-prompt.sh |
最后,将您的
重击:
1 | PS1='[\\u@\\h \\W$(__git_ps1" (%s)")]\\$ ' |
Zsh:
1 | setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1" (%s)")]\\$ ' |
git <1.9.3
但请注意,只有git 1.9.3(2014年5月)或更高版本才能安全显示该分支名称(!)。
参见Richard Hansen(
Both bash and zsh subject the value of PS1 to parameter expansion, command substitution, and arithmetic expansion.
Rather than include the raw, unescaped branch name in
PS1 when running in two- or
three-argument mode, constructPS1 to reference a variable that holds the branch name.Because the shells do not recursively expand, this avoids arbitrary code execution by specially-crafted branch names such as
1 | '$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)'. |
哪个狡猾的头脑会这样命名分支? ;)(如xkcd中的妈妈旁边)
更多例子
still_dreaming_1报告中的评论:
This seems to work great if you want a color prompt with
xterm (in my.bashrc ):
1 2 | PS1='\\[\\e]0;\\u@\\h: \\w\\a\\]\ ${debian_chroot:+($debian_chroot)}\\[\\033[01;32m\\]\\u@\\h\\[\\033[00m\\]:\\[\\03??3[01;34m\\]\\w\\[\\033[00m\\]$(__git_ps1)\\$ ' |
Everything is a different color, including the branch.
在Linux Mint 17.3 Cinnamon 64位中:
1 | PS1='${debian_chroot:+($debian_chroot)}\\[\\033[01;32m\\]\\u@\\h\\[\\033[01;34m\\] \\w\\[\\033[00m\\]$(__git_ps1) \\$ ' |
请按照以下步骤操作:(Linux)
编辑文件
1 2 3 4 5 | # 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\\] $" |
现在,启动新的终端窗口,并尝试输入任何git-repo。将显示当前分支,并显示提示。
4更多信息-MAC / Linux
1-如果没有bash完成...:
2-编辑您的.bashrc文件并检查(或添加):
1 2 3 | if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi |
3- ...在您的提示行之前:
(__git_ps1将显示您的git分支)
4-做
编辑:
更多阅读:不要重新发明轮子
这是我配置提示以显示Git状态的方式:
获取git-prompt脚本:
1 | curl -o ~/.git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh |
并自定义提示,在.bashrc文件中添加以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | # 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="\\\ \\\\\\$" # 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" |
如果您想了解更多信息,可以在这里获取所有的点文件:https://github.com/jamming/dotfiles
对于Mac,这确实非常有效:http://martinfitzpatrick.name/article/add-git-branch-name-to-terminal-prompt-mac/:
1 2 3 4 5 6 | # 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\\] $" |
首先,在主目录中打开Bash配置文件。 使用默认编辑器打开和编辑bash_profile的最简单方法。
例如,我使用VS Code通过以下命令打开它:code .bash_profile。
然后只需将以下代码粘贴到您的Bash中即可。
1 2 3 4 5 | 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\\] $" |
功能
parse_git_branch()
将获取分支名称,然后通过PS1将其显示在终端中。
这里,
\\u = Username
@ = Static Text
\\h = Computer Name
\\w = Current Directory
$ = Static Text
您可以更改或删除这些变量以进行更多自定义。
如果您是首次在终端中使用Git或在配置后立即使用Git,则有时可能看不到分支名称。
如果您遇到此问题,请不要担心。 在这种情况下,只需创建一个示例存储库,然后进行一些更改即可提交。 当commit命令执行一次后,终端将从此位置查找git分支。
1 2 3 4 5 6 7 | vim ~/.bash parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*\\)/ (\\1)/' } export PS1="\\u@\\h \\[\\033[32m\\]\\w\\[\\033[33m\\]\\$(parse_git_branch)\\[\\033[00m\\] $" |
为了反映最新的更改,请运行以下命令
1 | source ~/.bashrc |
输出:-
1 | chandrakant@NDL41104 ~/Chandrakant/CodeBase/LaravelApp (development) $ |
这是我使用的简单干净版本:链接
1 | root:~/project# -> root:~/project(dev)# |
将以下代码添加到?/ .bashrc的末尾
1 2 3 4 5 6 7 8 9 10 11 | 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 |
请按照以下步骤在ubuntu终端中显示GIT存储库分支的名称:
步骤1:打开终端并使用以下命令编辑.bashrc。
vi .bashrc
步骤2:在.bashrc文件的末尾添加以下行:
1 2 | parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*\\)/ (\\1)/' } |
第三步:通过执行以下操作在根(主)目录中获取.bashrc源:
/ rootfolder:?$
步骤4:重新启动并打开终端,然后检查cmd。导航到您的GIt存储库目录路径,就完成了。 :)
1 2 3 4 5 6 | parse_git_branch() { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*\\)/ (\\1)/' } export PS1='\\[\\e]0;\\w\\a\\]\ \\[\\e[32m\\]\\u@\\h \\[\\e[33m\\]\\w\\[\\e[0m\\]$(parse_git_branch)\ \\$ ' |
如果使用鱼壳,它会非常简单。
鱼是一个互动的外壳,带有许多糖果。您可以使用
1 | sudo apt-get install fish |
然后您可以使用更改提示设置
1 2 3 | > fish_config Web config started at 'http://localhost:8001/'. Hit enter to stop. Created new window in existing browser session. |
现在转到
打开提示标签,然后选择经典+ git选项
现在,单击使用提示按钮,您便已设置好。
我尝试了一个在bin文件夹中的python小脚本....
'gitprompt'文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #!/usr/bin/env python import subprocess, os s = os.path.join(os.getcwd(), '.git') def cut(cmd): ret='' half=0 record = False for c in cmd: if c =="\ ": if not (record): pass else: break if (record) and c!="\ ": ret = ret + c if c=='*': half=0.5 if c==' ': if half == 0.5: half = 1 if half == 1: record = True return ret if (os.path.isdir(s)): out = subprocess.check_output("git branch",shell=True) print cut(out) else: print"-" |
使它成为可执行文件
然后相应地调整bash提示,例如:
1 | \\u:\\w--[$(gitprompt)] \\$ |