Add Git branch information to your ZSH prompt
I use Zsh as my default shell, and that’s a good thing here because Zsh ships with a function called vcs_info specifically for grabbing version control information.
1. Open your zshrc config file
vi ~/.zshrc
If your config file doesn’t exist, create it first using touch ~/.zshrc
2. Load in the version control system (VCS) info
Add the following lines to your zshrc config file.
# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }
# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats 'on %b'
In the snippet above, %b
will be replaced by the branch name
3. Add version control info to PROMPT
After loading in VCS info (in step 3 above), use the vcs_info_msg_0_
variable in your PROMPT string:
# Set up the prompt (with git branch name)
setopt PROMPT_SUBST
PROMPT='%n in ${PWD/#$HOME/~} ${vcs_info_msg_0_}
%# '
Optional: If you want that branch info to display aligned to the right, remove the ${vcs_info_msg_0_} line from your PROMPT and add an RPROMPT line to your config file instead:
RPROMPT=\$vcs_info_msg_0_
4. Reload to see the changes
Save, and reload your zshrc config file
source ~/.zshrc
Admire your handiwork!

Go further
That’s it. If you want to go further, vcs_info provides other version control information such as staged/unstaged files, stash information etc.
Check out this fantastic article by Arjan van der Gaag to see what else you can do.