LoginSignup
15
7

More than 1 year has passed since last update.

git-completionとgit-prompt導入用スニペット(Bash向け)

Last updated at Posted at 2022-06-30

有名な次のGit便利ツールの導入方法。

Qiitaにも解説記事がすでに何個もありますが、本稿はほぼ一発コピペで導入するためのコードスニペットという位置付けです。

How To セットアップ

ダウンロード

git-completion と git-prompt のシェルスクリプトをホームディレクトリにダウンロードしてくる。

なお git-completion については、zsh用やtcsh用のもあるので、適宜URLを変更することでこれらシェル用のものに変更できる。リポジトリ内のファイルを確認のこと。

ホームディレクトリ下で実行するコマンド
cd
curl -o .git-completion.sh \
    https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
curl -o .git-prompt.sh \
    https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh

.bashrc の編集

.bashrc に次の内容を(後ろのほうに)追記する。

.bashrc
if [ -f ~/.git-completion.sh ]; then
    source ~/.git-completion.sh
fi
if [ -f ~/.git-prompt.sh ]; then
    source ~/.git-prompt.sh
fi
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUPSTREAM=auto

PS1='[\[\033[32m\]\u@\h\[\033[00m\] \[\033[33m\]\w\[\033[1;36m\]$(__git_ps1 " (%s)")\[\033[00m\]]\n\$ '

なお、PS1 の冒頭 \[\033[32m\] のところでプロンプトの”アカウント名@ホスト名”部分の色を決めている。32の部分を下表の色値にすると色を変えられる。1

カラーコード 色値
#000000 30
#ff0000 31
#00ff00 32
#ffff00 33
#0000ff 34
#ff00ff 35
#00ffff 36
#ffffff 37
  1. ここでは詳細は省くが ”Bash プロンプト 色変更” あたりで検索して欲しい。頑張れば256色カラー対応などもできる。

15
7
0

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
15
7