When working in a git directory, I would like to see the current branch as part of the Zsh prompt. There are more advanced use cases out there, but I'll stick with the branch name for now.

The following lines in ~/.zshrc takes care of the prompt. There are a few gotchas, though: The git command will fail if not in a git controlled directory, so we'll have to hide that failure message. Then, for Zsh to execute the function, rather than printing its verbatim name, the prompt_subst option has to be set. Finally, it is important to use single quotes for the PROMPT line. If double quotes are used, the first output of the function is used, and never called again.

function get-git-branch {
  b="`git symbolic-ref --short HEAD 2> /dev/null`" && echo "($b)" || echo ""
setopt prompt_subst
PROMPT='%n@%2m %~ $(get-git-branch) %# '