Play video

As a developer, I work with version control systems all day — more specifically, Git. I also work on multiple projects simultaneously and in collaboration with other developers, so I’m constantly switching branches, pulling and merging in changes. Knowing what branch I’m working on is often a useful piece of information that nicely fits on the command line, so here’s how to add it to your 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 up your zshrc config file with your favorite text editor

vi ~/.zshrc

2. Load in the version control system (VCS) info and tack it onto your prompt

# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }

# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats 'on branch %b'
 
# 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 as below

RPROMPT=\$vcs_info_msg_0_

3. Reload your prompt to see your changes

Reload your ~/.zshrc settings file

source ~/.zshrc

Admire your handiwork!

jacklot in ~/dev/site on branch master >

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.