Git branch in bash prompt (original) (raw)
西元2008年12月13日 - Kan-Ru Chen
之前提過 zsh 有支援在 prompt 上顯示多種 VCS 資訊的功能,不知道 bash 上是否有人寫好同樣支援這麼多種 VCS 的 library,不過目前 git 附帶的 bash completion script 中有提供自訂 PS1 的函式。
只要呼叫 __git_ps1 即可得到目前的 git branch 的格式化輸出,可以自訂 prompt 如下
GITPS1='$(__git_ps1 ":%s ")'
export PS1="\w <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>G</mi><mi>I</mi><mi>T</mi><mi>P</mi><mi>S</mi><mn>1</mn></mrow><mspace linebreak="newline"></mspace></mrow><annotation encoding="application/x-tex">{GITPS1}\\</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord"><span class="mord mathnormal">G</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal" style="margin-right:0.05764em;">TPS</span><span class="mord">1</span></span></span><span class="mspace newline"></span></span></span> "就會顯示如
/usr/src/awesome :next $這樣的 prompt