LoginSignup
2
1

More than 1 year has passed since last update.

WSL に標準でインストールされている git においてブランチ名を表示する方法

Last updated at Posted at 2022-05-07

始めに

 単純にブランチ名を表示する方法なら色々情報があったのですが、環境差異で微妙に手順が違ったので、備忘も兼ねて記事にまとめておこうと思います。

 手順のみ知りたい方は要約を、調査した内容を時系列に記録してるだけ

やりたい事

↓な感じでコマンドライン上に、常にブランチ名を表示させる事が今回のゴールです!💪💪

user@:/home/username/my-repository # 
↓
user@:/home/username/my-repository (main) # 

環境

ホスト環境
 OS: Windows10 home
ゲスト環境
 OS: Ubuntu 20.04.1 (WSL2)
 git: 2.25.1

要約

user@:/home/username/my-repository (main) # 

のように、コマンドライン上にブランチ名を表示させるためには、

.bashrc
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$ '

のように$(__git_ps1)を指定すればOK👌

__git_ps1: command not found. が発生した場合は、git-completion.bash と git-prompt.sh を事前に読み込ませる。

.bashrc
source /usr/share/bash-completion/.git-prompt.sh
source /usr/share/bash-completion/.git-completion.bash

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$ '

上記2ファイルが存在なければ、gitのソースから取得。
凡そ、下記のどこかに存在するみたいです(自分の環境では最後者)。

/usr/local/etc/bash_completion.d/
/usr/share/bash-completion/

.bashrc を確認

 ~/.bashrcというファイルを編集すればよいらしいので、まずは確認してみます。👀
編集前は下記の状態でした。

.bashrc(編集前)
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# (中略)

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# (以下略)

PS1 という変数が、コマンドラインの表示内容を決めているらしいです。
自分の場合は、デフォルトだとカラー表示されていなかったので、 else の方が適用されているようでした。

${...} は一旦無視するとして、\u@\h:\w\$ という部分に注目1すると、

\u : ユーザー名
\h : ホスト名
\W : 今いるディレクトリ名
\$ : 一般ユーザーの時$, rootの時#を表示

なので2userというユーザでログインしていて、/home/usernameというディレクトリにいる場合、

user@:/home/username$ 

と表示される事になります。

.bashrc を編集

 構文を理解したところで、さっそく編集に入ります。
カラーも変更できるみたいですが、一先ずブランチ名を表示させることをゴールにしているので、下記のように変更します。

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 'PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$ '

編集後としては、下記になりました。

.bashrc(編集後)
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# (中略)

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$ '
fi
unset color_prompt force_color_prompt

# (以下略)

保存したら、

source ~/.bashrc

で設定を反映させれば完了です!🎉🎉

__git_ps1: command not found. が出たら

source ~/.bashrc

と打った際に、__git_ps1: command not found.が出た時の対処法です。
自分もこれが出ました😇

git-completion.bash と git-prompt.sh を用意

参考2によれば、git-completion.bash と git-prompt.sh のファイルがあれば良いらしいですが、

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

自分の環境では/usr/local/etc/ が空でした。

$ ls /usr/local/etc/
$ # 空っぽ

方々探したところ、/usr/share配下にbash-completion/があり、

$ ls /usr/share/bash-completion
bash_completion  completions/  helpers/

またbash-completion/completions/gitには、

# bash/zsh completion support for core Git.
#
# (中略)
#
# To use these routines:
#
#    1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
#    2) Add the following line to your .bashrc/.zshrc:
#        source ~/.git-completion.bash
#    3) Consider changing your PS1 to also show the current branch,
#       see git-prompt.sh for details.
# (以下略)

とあるので、参考2の配置場所に合わせて、.git-completion.bash をbash-completion/配下に作っておきます。

$ cp bash-completion/completions/git bash-completion/.git-completion.bash

.git-prompt.sh は見つけられなかったので、git のソースから取得して(参考3)、同じくbash-completion/に配置しました。

最終的な状態は下記の通りです3

$ ls /usr/share/bash-completion -a
.  ..  .git-completion.bash  .git-prompt.sh  bash_completion  completions/  helpers/

.bashrc を再度編集

 $(__git_ps1)\$を使用する直前で4、git-completion.bash と git-prompt.sh を読み込ませます5

.bashrc(再編集後)
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# (中略)

# ↓2行を加える
source /usr/share/bash-completion/.git-prompt.sh
source /usr/share/bash-completion/.git-completion.bash

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(__git_ps1)\$ '
fi
unset color_prompt force_color_prompt

# (以下略)

再度設定を反映すれば、今度こそ完了です!🎉🎉🎉

source ~/.bashrc

終わりに

不備や誤字等ありましたら、編集リクエストお願いいたします🙇‍♂️

参考

  1. Bashプロンプトの変更
  2. gitのブランチ名をターミナルで表示できるようにする
  3. A1.4 付録 A: その他の環境でのGit - BashでGitを使う
  1. 末尾に半角スペースあり

  2. 参考1より改変して転記させて頂いています

  3. gitのソースから取得したgit-prompt.shには、.git-prompt.shと隠しファイルで作成するように記載されていたので、今回はその対応にしました。git-prompt.shでも問題なさそうですが、本当はどっちがいいんでしょう。。。🤔

  4. 個人的に、出来るだけ使用する直前で、読み込ませたかったので。

  5. GIT_PS1_SHOWDIRTYSTATE=1 も追加しておくと、未コミットのファイルがある時に表示が出るらしい(参考3)ですが、今回は省略しました。

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