How to Add Git Branch Name to Terminal (MacOS Catalina)

Farooq Yousuf
FusionQA
Published in
Nov 2, 2020

Change directory (cd) into ~/: cd ~/

You should now be in your root directory, something like this: /Users/FirstName.LastName

Now using your favorite text editor create a .zshrc file if it doesn’t already exist.

In this file paste the following at the end:

function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

Now either restart your terminal or you can simply do a source ~/.zshrc and you should now see the branch name displayed in your Git repos.

--

--