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

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

问题描述

我尝试添加的混帐分支我目前正在上没有成功的bash提示符(检出)。(,同时保持我目前的路径,显示当前目录/文件完整)
我有我的家.bashrc文件,但我也看到很多人提的.profile文件。


解决方案

请注意这种提示现在由管理<一href=\"https://github.com/git/git/blob/8976500cbbb13270398d3b3e07a17b8cc7bff43f/contrib/completion/git-prompt.sh\">contrib/completion/<$c$c>git-prompt.sh __ git_ps1_branch_name 变量。


  

      
  • 将此文件复制到某个地方(例如〜/ git的-prompt.sh )。

  •   
  • 以下行添加到您的的.bashrc / .zshrc

  •   

 源〜/ git的,prompt.sh


  

      
  • 更改 PS1 调用 __ git_ps1 作为命令替换:

  •   

 击:
  PS1 ='[\\ü@ \\ ^ h \\ W $(__ git_ps1(%S))] \\ $'
  ZSH:
  SETOPT PROMPT_SUBST; PS1 ='[%N @%M%C $(__ git_ps1(%S))] \\ $'


但需要注意的是唯一的Git 1.9.3(2014年5月)<一个href=\"http://unix.stackexchange.com/questions/33617/how-can-i-update-to-a-newer-version-of-git-using-apt-get/170831#170831\">or后来可以让你的安全的显示分支名称(!)

请参阅理查德犯8976500 汉森( richardhansen ):


  

这两个bash和zsh中受到PS1的为参数扩展 命令替换和算术扩展的价值。


  
  

在两个或运行时,而不是包括 PS1 原料,转义支行名称
  三参数模式,构建 PS1 引用持有的分支名称的变量。


  
  

由于该弹不递归扩展,这款由特制的分支名称,如

避免任意code执行

 '$(IFS = _; CMD = sudo_rm_射频_ /; CMD $)。

什么狡猾的头脑会说出这样的一个分支? ;)(旁的妈妈在XKCD


still_dreaming_1 报告<一href=\"http://stackoverflow.com/questions/15883416/adding-git-branch-on-the-bash-command-prompt/24716445?noredirect=1#comment54332815_24716445\">in该评论:


  

这似乎工作伟大的,如果你想与的xterm 颜色提示(在我的的.bashrc


  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)\\$


  

一切都是不同的颜色,包括分支。


在Linux Mint的17.3肉桂64位:

  PS1 ='$ {debian_chroot:+($ debian_chroot)} \\ [\\ 033 [01;32米\\] \\ü@ \\ ^ h \\ [\\ 033 [01;34米\\] \\ W \\ [\\ 033 [00米\\] $(__ 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..

解决方案

Note this kind of prompt is now managed by contrib/completion/git-prompt.sh and its __git_ps1_branch_name variable.

  • 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)")]\$ '


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)


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: \w\a\]\n${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.

In in Linux Mint 17.3 Cinnamon 64-bit:

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

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

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