LoginSignup
6
7

More than 3 years have passed since last update.

Gitのブランチ名をコンソールに表示する方法

Last updated at Posted at 2017-04-25

いろいろな人が書いているので、再録感満載ですが個人的にも書いておこうかと思います。

Git本家のセットアップシェル

bashとかzshとかあるので、自分の環境に合うものを選んでください。試した環境は、LinuxMintとMacです。

LinuxMint(Ubuntu)のaptでGitインストールした場合


下記をコピペで叩いて、新しくコンソールを開けば適用されてます。

cat << EOS >> .bashrc

# setup git branch view
PS1='\[\033[1;32m\]\u@\h\[\033[00m\]:\[\033[1;34m\]\w\[\033[00m\]\[\033[1;31m\]$(__git_ps1)\[\033[00m\]\$ '

EOS

素の環境の場合(Macなど)


下記をコピペで実行する場合、.bashrcを読み込んでいる必要があります。
やっていない場合、.bashrcを.bash_profileに置き換えてください。

MacとLinux環境の違いは、 git-prompt.sh を呼ぶ必要があるかどうか?です。
linux環境でaptでgit入れてない場合でも、下記のコマンドで動きます。


cd
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
### zshの場合 wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh

chmod 755 git-prompt.sh
chmod 755 git-completion.bash

cat << EOS >> .bashrc

# setup git branch view
source ~/git-prompt.sh
source ~/git-completion.bash
GIT_PS1_SHOWDIRTYSTATE=true
export PS1='\[\033[1;32m\]\u@\h\[\033[00m\]:\[\033[1;34m\]\w\[\033[00m\]\[\033[1;31m\]$(__git_ps1)\[\033[00m\]\$ '
EOS

※追記:2017-06-06 実行権限が付加されてないと色が付かなかったので権限変更追記です。

コンソールに表示される文字カラーを変えたい場合


下記の部分を読んで、カラーコードを変更してください。

  • \[\033[1;32m\] ライトグリーン指定のヘッダ(1;32の部分がカラーコード)
  • \[\033[00m\] 閉じタグ

かんたんに説明


使い方的には、中に書いてあります。↑は説明通りに設定した形となりますね。

# 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.

Windowsユーザ


Windowsは、「Git for Windows」使えばデフォルト表示されるので、そちらを使ってほしいです。bash on windowsがあれば上記手順が使えるかもしれないですが。。。

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