2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

terminalにgitのブランチ名と作業ツリーの状態を表示する

Last updated at Posted at 2017-03-13

git push し忘れることが多々あり、review時に変更点ないけどと3回言われ、反省:bow:して調べました。

前提条件

  • Homebrewでgitをinstall

Homebrewでinstallしてない方は自分で「git-completion.bash」と「git-prompt.sh」をダウンロードしてください。

Homebrewでinstallした場合には下記に配置されています。

$ ls -al /usr/local/Cellar/git/2.11.0/etc/bash_completion.d/
total 152
drwxr-xr-x  4 a21032267  admin    136 11 30 06:06 .
drwxr-xr-x  3 a21032267  admin    102 11 30 06:06 ..
-rw-r--r--  1 a21032267  admin  60609 11 30 06:06 git-completion.bash
-rw-r--r--  1 a21032267  admin  16111 11 30 06:06 git-prompt.sh

多分下記で良いかと

$ wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash
$ wget https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh

設定

基本的にgit-prompt.shの中身にかかれているとおりに進めれば:ok:です

/usr/local/Cellar/git/2.11.0/etc/bash_completion.d/git-prompt.sh
# bash/zsh git prompt support
#
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
# Distributed under the GNU General Public License, version 2.0.
#
# This script allows you to see repository status in your prompt.
#
# To enable:
#
#    1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
#    2) Add the following line to your .bashrc/.zshrc:
#        source ~/.git-prompt.sh
#    3a) Change your PS1 to call __git_ps1 as
#        command-substitution:
#        Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
#        the optional argument will be used as format string.
#    3b) Alternatively, for a slightly faster prompt, __git_ps1 can
#        be used for PROMPT_COMMAND in Bash or for precmd() in Zsh
#        with two parameters, <pre> and <post>, which are strings
#        you would put in $PS1 before and after the status string
#        generated by the git-prompt machinery.  e.g.
#        Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
#          will show username, at-sign, host, colon, cwd, then
#          various status string, followed by dollar and SP, as
#          your prompt.
#        ZSH:  precmd () { __git_ps1 "%n" ":%~$ " "|%s" }
#          will show username, pipe, then various status string,
#          followed by colon, cwd, dollar and SP, as your prompt.
#        Optionally, you can supply a third argument with a printf
#        format string to finetune the output of the branch status

私はzshを使っているので~/.zshrcに以下を追加します。
Homebrewをinstallしている場合は

setopt PROMPT_SUBST
source /usr/local/Cellar/git/`git version | awk '{print $3}'`/etc/bash_completion.d/git-prompt.sh
source /usr/local/Cellar/git/`git version | awk '{print $3}'`/etc/bash_completion.d/git-completion.bash # deprecateと言われたらこの行をコメントアウトしてください
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUPSTREAM=auto

versionが変わることも考えてversionのところをgit version | awk '{print $3}'としています

あとはPROMPTに設定するだけ

私の場合は下記を~/.zshrcに設定しています

export PROMPT="[${USER} %1~\$(__git_ps1)]%(!.#.$) "

表示サンプル

[USER workspace]$ cd playbook 
[USER playbook (capistrano)]$ vi README.md 
[USER playbook (capistrano *)]$ 
[USER playbook (capistrano *)]$ 
[USER playbook (capistrano *)]$ git add -i
[USER playbook (capistrano *)]$ git add -u
[USER playbook (capistrano +)]$ git commit    
[capistrano edb092a] test
 1 file changed, 1 insertion(+)
[USER playbook (capistrano)]$ 

肝心のpushされているかの判断が出来ていない....
hashでも表示させようか...

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?