加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在Bash命令提示符下添加git branch

发布时间:2020-12-15 16:56:25 所属栏目:安全 来源:网络整理
导读:我试图添加git分支我目前工作(签出)在bash提示没有成功..(同时保持我的当前路径显示活动目录/文件完好无损) 我在我家有一个.bashrc文件,但我也看到很多人提到.profile文件.. 注意这种提示现在由 contrib/completion/ git-prompt.sh 及其__git_ps1_branch_na
我试图添加git分支我目前工作(签出)在bash提示没有成功..(同时保持我的当前路径显示活动目录/文件完好无损)
我在我家有一个.bashrc文件,但我也看到很多人提到.profile文件..
注意这种提示现在由 contrib/completion/git-prompt.sh及其__git_ps1_branch_name变量管理。
  • Copy this file to somewhere (e.g. ~/.git-prompt.sh).
  • Add the following line to your .bashrc/.zshrc:
source ~/.git-prompt.sh
  • 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(2014年5月)or later允许您安全地显示该分支名称(!)

见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)'.

什么愚蠢的心会命名这样的分支? ;)(Beside a Mom as in xkcd)

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]n${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.

在Linux Mint 17.3肉桂64位:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读