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:
- Open Terminal and create or edit your .bash_profile:
nano ~/.bash_profile
- 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}%} $ '
- Save the file (Ctrl+X, then Y, then Enter)
- 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
- 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
- Edit your .zshrc file:
nano ~/.zshrc
- Add this line:
setopt PROMPT_SUBST
PS1='%n@%m %c$(git branch 2> /dev/null | sed -e "/^[^*]/d" -e "s/* \\(.*\\)/ (\\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.

