LoginSignup
576
545

More than 3 years have passed since last update.

「Git補完をしらない」「git statusを1日100回は使う」そんなあなたに朗報【git-completionとgit-prompt】

Last updated at Posted at 2014-12-12

はじめに

  • 「Git補完をしらない」「commitブランチを間違える」「git statusを1日100回は使う」そんなあなたに朗報です。
  • bashの説明だけに絞っています。zsh? tcsh? 知らない子ですね。

いきなり完成形

beautifulPrompt.jpg

.bashrc
# スクリプト読み込み
source $HOME/.git-completion.bash
source $HOME/.git-prompt.sh

# プロンプトに各種情報を表示
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWUPSTREAM=1
GIT_PS1_SHOWUNTRACKEDFILES=
GIT_PS1_SHOWSTASHSTATE=1

############### ターミナルのコマンド受付状態の表示変更
# \u ユーザ名
# \h ホスト名
# \W カレントディレクトリ
# \w カレントディレクトリのパス
# \n 改行
# \d 日付
# \[ 表示させない文字列の開始
# \] 表示させない文字列の終了
# \$ $
export PS1='\[\033[1;32m\]\u\[\033[00m\]:\[\033[1;34m\]\w\[\033[1;31m\]$(__git_ps1)\[\033[00m\] \$ '
##############

git-completion.bashとは

  • gitコマンドの補完スクリプト。Tabで補完できてンギモッチイイ!!

導入

  1. wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -O ~/.git-completion.bash
  2. chmod a+x ~/.git-completion.bash
  3. echo "source ~/.git-completion.bash" >> ~/.bashrc
  4. source ~/.bashrc

git-prompt.shとは

  • プロンプトに各種追加情報を表示可能にするスクリプト

導入

  1. wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -O ~/.git-prompt.sh
  2. chmod a+x ~/.git-prompt.sh
  3. echo "source ~/.git-prompt.sh" >> ~/.bashrc
  4. ~/.bashrc内のPS1 変数を __git_ps1 を含むように変更する
  5. source ~/.bashrc

オプション(1 or null)

  • GIT_PS1_SHOWUPSTREAM
    • 現在のブランチがupstreamより進んでいるとき">"を、遅れているとき"<"を、遅れてるけど独自の変更もあるとき"<>"を表示する。オプションが指定できるけど(svnをトラックするかとか)
  • GIT_PS1_SHOWUNTRACKEDFILES
    • addされてない新規ファイルがある(untracked)とき"%"を表示する
  • GIT_PS1_SHOWSTASHSTATE
    • stashになにか入っている(stashed)とき"$"を表示する
  • GIT_PS1_SHOWDIRTYSTATE
    • addされてない変更(unstaged)があったとき"*"を表示する、addされているがcommitされていない変更(staged)があったとき"+"を表示する
  • 参考: __git_ps1のオプションを設定して様々な情報を表示する

PS1とは

  • bashには、プロンプトを制御するために「PS1」という環境変数が使用されている。
  • このPS1変数は、exportコマンドを使いさまざまな特殊文字コードを利用すれば、表示形式を変更することが可能
  • 参考: Bashのプロンプト変更

おわりに

  • 次は勉強がてらtigのお話でも書こうかな
576
545
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
576
545