LoginSignup
1
1

More than 3 years have passed since last update.

【bash】Gitのブランチ名を「$」前に表示させる

Posted at

はじめに

Gitを使っていて毎回ブランチを確認する際に

$ git branch

を使っていましたが、割とこのコマンドを使うことが多く、もっと楽に確認できる方法がないかなと探していた際にブランチ名を常に表示させておく方法があるとのことで、今回はその設定方法について書いています。


xcodeを使って入れたGitのバージョン

$ git --version
git version 2.20.1 (Apple Git-117)


他の記事(Homebrewを使ってインストールした記事)では、HomebrewでインストールしたGitを参照しているようですが、自分の実行コマンドのパスはxcodeをインストールした際のGitを参照しています。

Homebrewでインストールした場合のパス

$ which git
/usr/local/bin/git
xcodeでインストールした際のパス

$ which git
/usr/bin/git


Homebrewでインストールした場合は、/usr/local/etc/bash_completion.d/のフォルダ内にgit-completion.bashgit-prompt.shの2つのファイルが存在するらしいんだけど、xcodeでインストールしてそのままの場合は、そのようなファイルはなかった。

$ ls /usr/local/etc/bash_completion.d/
brew        brew-services   npm     tmux

やったこと

①以下のコマンドからHomebrewでインストールしたパッケージを表示してもgitは見当たらないことを確認。

$ brew list

②Homebrewを使ってGitを上書き

$ brew install git 
 
$ which git
/usr/local/bin/git

$ ls /usr/local/etc/bash_completion.d/
brew            git-completion.bash npm
brew-services       git-prompt.sh       tmux

無事二つのファイルが追加されています。

③.bash_profileに以下を記述

$ vim ~/.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='\[\e[30;47m\] \t \[\e[37;46m\]\[\e[30m\] \W \[\e[36;49m\]$(__git_ps1 "\[\e[37;102m\] \[\e[30m\] %s \[\e[0;92m\]")\[\e[49m\]\[\e[m\] \$ '

パスを通して完了!

$ source ~/.bash_profile

スクリーンショット 2020-04-05 17.18.10.png

参考にした記事

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