Get Started

How to Display Your Git Branch in MacOS Terminal

26.02.2025

Having your current Git branch visible in your terminal can significantly improve your workflow. Here's a simple guide to help you set this up on MacOS.

Method 1: Modifying .bash_profile

If you're using Bash as your shell, follow these steps:

  1. Open Terminal and create or edit your .bash_profile:
nano ~/.bash_profile
  1. Add the following code:
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*\\)/ (\\1)/'
}

setopt PROMPT_SUBST
PROMPT='%9c%{%F{green}%}$(parse_git_branch)%{%F{none}%} $ '
  1. Save the file (Ctrl+X, then Y, then Enter)
  2. Reload your profile:
source ~/.bash_profile

Method 2: Using Zsh (Default in Modern MacOS)

If you're using Zsh (default shell in newer MacOS versions), you have several options:

Option 1: Oh My Zsh

  1. Install Oh My Zsh:
sh -c "$(curl -fsSL <https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh>)"

Oh My Zsh comes with Git branch display enabled by default in most themes.

Option 2: Manual Configuration

  1. Edit your .zshrc file:
nano ~/.zshrc
  1. Add this line:
setopt PROMPT_SUBST
PS1='%n@%m %c$(git branch 2> /dev/null | sed -e "/^[^*]/d" -e "s/* \\(.*\\)/ (\\1)/") $ '
  1. Save and reload:
source ~/.zshrc

What You'll See

After setting this up, your terminal prompt will show:

username@hostname current-directory (branch-name) $

Troubleshooting

  • If changes don't appear immediately, restart your terminal
  • Ensure Git is installed by running: git --version
  • Check which shell you're using with: echo $SHELL

These methods will help you keep track of your Git branches directly in your terminal, making your development workflow more efficient and reducing the chances of committing to the wrong branch.

Get in touch to discuss your project, request a quote or even just to pick our brains