LoginSignup
35
27

More than 3 years have passed since last update.

[macOS]zshでgitのブランチ名を表示させる

Posted at

Mojaveまでは、手動でCommand Line Toolsをインストールしたら設置されるスクリプトを使ってbashでgitのブランチ名を表示させていたが、Catalinaからは設置されないようになったようだ。また、Catalinaからはzshがデフォルト・シェルになったということで、zshでgitのブランチ名を表示させる方法を調べた。

ホームディレクトリ配下に.zshというディレクトリを作って、そこにgit-completion.zshとgit-prompt.shをダウンロードして配置する。

% cd
% mkdir .zsh
% curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh -o ~/.zsh/git-completion.zsh
% curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -o ~/.zsh/git-prompt.sh

.zshrcに以下のコードを追加する。

# Git
 
fpath=(~/.zsh $fpath)
 
if [ -f ${HOME}/.zsh/git-completion.zsh ]; then
        zstyle ':completion:*:*:git:*' script ~/.zsh/git-completion.zsh
fi
 
if [ -f ${HOME}/.zsh/git-prompt.sh ]; then
        source ${HOME}/.zsh/git-prompt.sh
fi
 
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUPSTREAM=auto
 
setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

これで、zshのプロンプトにgitのブランチ名が表示されるようになる。

【関連情報】
Cocoa.swift

Cocoa勉強会 関東

Cocoa練習帳

35
27
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
35
27