LoginSignup
138
112

More than 5 years have passed since last update.

bashでgitのブランチ名を表示

Last updated at Posted at 2014-04-02

bashでgitのブランチ名を表示

参考サイト:
http://blog.ruedap.com/2011/07/06/mac-terminal-git-branch-name

gitcomprication.bash を配置

配置する場所はここ: /usr/local/git/contrib/completion/

gitcomprication.bashはここ:
(Homebrewでgitを入れた場合) usr/local/Cellar/git/<gitのversionの数字>/etc/bash_completion.d/git-completion.bash

配置するためのディレクトリ作成とファイルの移動は以下の操作で行った。

mkdir -p /usr/local/git/contrib/completion/
mv git-completion.bash /usr/local/git/contrib/completion/

.bashrcの作成と記述

.bashrcはホームディレクトリに作成する。
以下の内容を記述。

# git settings
source /usr/local/git/contrib/completion/git-completion.bash
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

記述したら反映のために以下を実行。

 source ~/.bashrc

.bashrcを読みに行く設定をしてない場合、.bash_profileに足しておく必要がある。

$ vi ~/.bash_profile

if [ -f ~/.bashrc ] ; then
. ~/.bashrc
fi

__git_ps1: command not found

僕の場合はブランチ名が表示されず、このメッセージが表示されるようになった。
__git_ps1: command not found

git-completion.bashの前にgit-prompt.shを読みこませることで対処できるようなので、.bashrcの一行目に、以下のように書き加えた。

source /usr/local/etc/bash_completion.d/git-prompt.sh
source /usr/local/etc/bash_completion.d/git-completion.bash
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\$ '

追記

横長になりすぎているように感じたので、ブランチ名の後ろで改行するように変更した。

export PS1='\[\033[32m\]\u@\h\[\033[00m\]:\[\033[34m\]\w\[\033[31m\]$(__git_ps1)\[\033[00m\]\n\$ '
138
112
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
138
112