LoginSignup
1
0

More than 5 years have passed since last update.

BashでプロンプトにカレントディレクトリとGitで管理していればGitのブランチ名を表示する

Posted at

branch名を取ってくる際に空文字だったら表示しないようにしておく

~/.bash_profile
git_branch() {
  BRANCH=$(git branch 2>/dev/null | sed -ne "s/^\* \(.*\)$/\1/p")
  if [ -n "$BRANCH" ]; then
    echo "($BRANCH)"
  fi
}

export PS1='\[\e[1;32m\][\w]\[\e[m\]\[\e[1;31m\]$(git_branch)\[\e[m\] '

こんな感じに表示される

image.png

1
0
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
1
0