Mac OS Terminal Custom Prompt with Git Branch

This article explains how I set up my new Mac’s terminal to be more simple and incorporate the Git branch in the prompt.

Get .git-promt.sh

Get the code from the source on GitHub.

As directed in the comments, place the code in your home directory (e.g. ~/.git-prompt.sh). You will most likely need to create this file.

Add or edit .bashrc or .zshrc

You will need to know what shell you are running. A really easy way to see this is to just type some gibberish in the terminal prompt and hit enter.

$ asdf
bash: asdf: command not found

The above shows you are running the Bash shell. Below shows you are using the ZSH shell.

$ asdf
zsh: command not found: asdf

Create or edit your .bashrc or .zshrc file in your home directory (e.g. ~/.zshrc). Then add the following to your file and save:

Bash Shell

source ~/.git-prompt.sh
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

ZSH Shell

source ~/.git-prompt.sh
setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

Reload Shell rc File

Run the following command to reload your RC file.

Bash Shell

source ~/.bashrc

ZSH Shell

source ~/.zshrc

Your New Prompt

Your new prompt will look something like this depending on if you are in a git repository or not:

[username@host current_directory]$
[username@host current_git_directory (branch_name)]$

There are many options to customize your prompt even more by customizing what you put in your RC file . Doing a search on google will help you find what you need.

Posted in Bash Shell, Git, Mac OS, Mac Terminal, ZSH Shell and tagged , , , , .