在 Bash 命令提示符下添加 git branch [英] Adding git branch on the Bash command prompt

查看:32
本文介绍了在 Bash 命令提示符下添加 git branch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 bash 提示符下添加我当前正在处理(签出)的 git 分支,但没有成功..(同时保持我当前显示活动目录/文件的路径完好无损)我家有 .bashrc 文件,但我也看到很多人提到 .profile 文件..

解决方案

git 1.9.3 or later: use __git_ps1

Git 提供了一个名为 git 的 shell 脚本-prompt.sh,其中包含一个函数 __git_ps1

<块引用>

打印文本以添加到 bash PS1 提示(包括分支名称)

它最基本的用法是:

$ __git_ps1(掌握)

它还需要一个可选的格式字符串:

$ __git_ps1 'git:[%s]'混帐:[大师]

如何获得

首先,将文件复制到某处(例如~/.git-prompt.sh).

选项 1:使用文件系统上的现有副本.示例(Mac OS X 10.15):

$ find/-name 'git-prompt.sh' -type f -print -quit 2>/dev/null/Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh

选项 2:从 GitHub 拉取脚本 .

接下来,将以下行添加到您的.bashrc/.zshrc:

source ~/.git-prompt.sh

最后,更改您的 PS1 以调用 __git_ps1 作为命令替换:

重击:

PS1='[u@h W$(__git_ps1 " (%s)")]$ '

Zsh:

setopt PROMPT_SUBST ;PS1='[%n@%m %c$(__git_ps1 " (%s)")]$'

git <1.9.3

但请注意,只有 git 1.9.3(2014 年 5 月)或更高版本允许您安全显示该分支名称(!)

请参阅 commit 8976500 来自 理查德汉森 (richardhansen):

<块引用>

bash 和 zsh 都将 PS1 的值进行参数扩展命令替换和算术扩展.

而不是在 PS1 中包含原始的、未转义的分支名称,当运行在两个或三参数模式,构造 PS1 来引用一个保存分支名称的变量.

由于shell不会递归扩展,这就避免了通过特制的分支名称(例如

)执行任意代码

'$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)'.

哪个狡猾的头脑会这样命名一个分支?;) (xkcd 中的妈妈身边)

更多示例

still_dreaming_1 报告 在评论中:

<块引用>

如果您想要带有 xterm(在我的 .bashrc 中)的颜色提示,这似乎很有效:

PS1='[e]0;u@h: wa]
${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[3‌ 3[01;34m]w[33[00m]$(__git_ps1)$ '

<块引用>

一切都是不同的颜色,包括树枝.

在 Linux Mint 17.3 Cinnamon 64 位中:

PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[01;34m] w[33[00m]$(__git_ps1) $ '

I tried adding the git branch I'm currently working on (checked-out) on the bash prompt without success.. (while keeping my current path which shows the active directory/file intact) I have a .bashrc file on my home, but I also saw many people mentioning the .profile file..

解决方案

git 1.9.3 or later: use __git_ps1

Git provides a shell script called git-prompt.sh, which includes a function __git_ps1 that

prints text to add to bash PS1 prompt (includes branch name)

Its most basic usage is:

$ __git_ps1
(master)

It also takes an optional format string:

$ __git_ps1 'git:[%s]'
git:[master]

How to Get It

First, copy the file to somewhere (e.g. ~/.git-prompt.sh).

Option 1: use an existing copy on your filesystem. Example (Mac OS X 10.15):

$ find / -name 'git-prompt.sh' -type f -print -quit 2>/dev/null
/Library/Developer/CommandLineTools/usr/share/git-core/git-prompt.sh

Option 2: Pull the script from GitHub.

Next, add the following line to your .bashrc/.zshrc:

source ~/.git-prompt.sh

Finally, change your PS1 to call __git_ps1 as command-substitution:

Bash:

PS1='[u@h W$(__git_ps1 " (%s)")]$ '

Zsh:

setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]$ '

git < 1.9.3

But note that only git 1.9.3 (May 2014) or later allows you to safely display that branch name(!)

See commit 8976500 by Richard Hansen (richardhansen):

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, construct PS1 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

'$(IFS=_;cmd=sudo_rm_-rf_/;$cmd)'.

What devious mind would name a branch like that? ;) (Beside a Mom as in xkcd)

More Examples

still_dreaming_1 reports in the comments:

This seems to work great if you want a color prompt with xterm (in my .bashrc):

PS1='[e]0;u@h: wa]
${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[00m]:[3‌​3[01;34m]w[33[00m]$(__git_ps1)$ ' 

Everything is a different color, including the branch.

In in Linux Mint 17.3 Cinnamon 64-bit:

PS1='${debian_chroot:+($debian_chroot)}[33[01;32m]u@h[33[01;34m] w[33[00m]$(__git_ps1) $ ' 

这篇关于在 Bash 命令提示符下添加 git branch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆