Show Git Branch and Folder Path in Terminal Prompt
In this article
You can show the current Git branch and folder path in your terminal prompt by editing your shell's prompt configuration. The most reliable way is to use your shell's built-in prompt variables or a prompt framework that includes Git integration. For Bash, you edit ~/.bashrc; for Zsh, you edit ~/.zshrc. The exact syntax depends on your shell, but the principle is the same: you define a prompt string that includes the working directory and a command substitution that reads the current Git branch. This guide walks through the steps for the two most common shells on macOS and Linux, plus notes for Windows. Menu labels and file paths can differ slightly by version, device, or region, but the core method is stable.
What You'll Need
- A terminal emulator (Terminal, iTerm2, Windows Terminal, or similar).
- A Unix-like shell: Bash or Zsh. macOS defaults to Zsh; most Linux distributions default to Bash. Windows users can use Git Bash or WSL, which provide Bash.
- Git installed and a repository to test in.
- Basic comfort editing a text file with a command-line editor such as nano or vim.
Step-by-Step
- Identify your shell. Run
echo $SHELLin the terminal. If it ends in/bash, you are using Bash; if it ends in/zsh, you are using Zsh. This determines which configuration file to edit. - Open your shell configuration file. For Bash, run
nano ~/.bashrc. For Zsh, runnano ~/.zshrc. If the file does not exist, nano will create it when you save. - Understand the default prompt variable. In Bash, the prompt is set by
PS1. In Zsh, it is set byPROMPT. You will replace or extend the existing value. Look for a line that starts withPS1=orPROMPT=. If none exists, you will add one. - Add the folder path. In Bash,
\wshows the full working directory path (with~for your home directory). In Zsh,%dor%~does the same. For example, a minimal Bash prompt with path and a dollar sign isPS1='\w $ '. For Zsh, usePROMPT='%~ $ '. - Add the Git branch. The shell does not show Git information by default. You need a function that runs
git branch --show-current(or parsesgit symbolic-ref) and inserts the result into the prompt. A simple Bash approach is to define a function and call it insidePS1using command substitution. For example, add this to~/.bashrc:
parse_git_branch() {
git branch --show-current 2>/dev/null
}
PS1='\w $(parse_git_branch) $ '
This shows the folder path, then the branch name if you are inside a Git repository, then a dollar sign. The 2>/dev/null hides errors when you are not in a repository. For Zsh, the equivalent is:
parse_git_branch() {
git branch --show-current 2>/dev/null
}
PROMPT='%~ $(parse_git_branch) $ '
- Save and reload. Save the file (in nano, press Ctrl+O, Enter, then Ctrl+X). Then run
source ~/.bashrcorsource ~/.zshrcto apply the changes to the current terminal session. Open a new terminal window to confirm the prompt persists. - Test inside and outside a repository. Change to a Git repository with
cd /path/to/repoand check that the branch name appears. Thencd ~and confirm the branch name disappears and only the path remains. - Optional: add color and formatting. You can wrap the branch name in ANSI color codes. In Bash, use
\[\e[32m\]before the branch and\[\e[0m\]after to reset. In Zsh, use%F{green}and%f. Keep color codes non-printing so the shell calculates line length correctly. - Windows alternative. If you use PowerShell, the prompt is controlled by a
promptfunction in your profile. You can edit$PROFILEand define a function that callsgit branch --show-currentand includesGet-Location. Git Bash on Windows uses the same Bash method above. Command Prompt (cmd.exe) has very limited prompt customization viaPROMPTand cannot easily show Git branch without third-party tools; using Git Bash or WSL is simpler.
Common Mistakes
- Editing the wrong file. If you use Zsh but edit
~/.bashrc, nothing changes. Always confirm your shell first. - Forgetting to reload. Changes to
~/.bashrcor~/.zshrcdo not apply to open terminals until you runsourceor open a new window. - Using single quotes incorrectly. In Bash,
PS1='\w $(parse_git_branch) $ 'uses single quotes so the command substitution runs each time the prompt is drawn. Double quotes would evaluate it once at startup. - Not handling non-repository folders. Without
2>/dev/null, Git prints an error every time you leave a repository. Redirect stderr to keep the prompt clean. - Breaking color codes. In Bash, non-printing escape sequences must be wrapped in
\[and\]; otherwise the cursor position and line wrapping misbehave. - Assuming the branch command works everywhere.
git branch --show-currentis available in modern Git. On very old Git versions, usegit rev-parse --abbrev-ref HEADinstead.
Tips
- Keep the prompt short. A full path plus a long branch name can wrap and make commands hard to read. Consider showing only the last folder with
\Win Bash or%1~in Zsh. - Use a prompt framework if you want more features without writing functions. Oh My Zsh and Starship both include Git branch display and are configured through their own files, not by editing
PS1directly. - Test your prompt in a temporary shell before committing changes. You can run
bash --norcorzsh -fto start without your config, then source your file manually to see errors. - If you use multiple machines, keep your prompt configuration in a dotfiles repository so it stays consistent.
Troubleshooting
If the branch name does not appear, first check that you are actually inside a Git repository by running git status. If Git reports “not a git repository,” the prompt function is working correctly and simply has nothing to show. If you are in a repository and still see nothing, run parse_git_branch manually in the terminal. If it prints nothing, the function is not defined in your current session; reload your config file. If it prints the branch name but the prompt does not, your PS1 or PROMPT line may have a quoting error or may be overridden later in the file. Search the file for other PS1= or PROMPT= assignments. If the prompt shows literal text like $(parse_git_branch), you likely used single quotes where the shell does not perform substitution, or you are in a shell that does not support that syntax. If colors look wrong or the cursor jumps, review your escape sequences and make sure non-printing characters are marked correctly. On macOS, note that Zsh is the default shell; editing ~/.bashrc will have no effect unless you explicitly switch to Bash. If you use a terminal multiplexer or an IDE terminal, restart it after changing shell config so it picks up the new environment.
Frequently Asked Questions
Can I show the branch name without editing my shell config?
Yes, you can run git branch --show-current manually, but it will not appear automatically in the prompt. For automatic display, a shell configuration change or a prompt framework is required.
Does this work in Windows Command Prompt?
Not easily. Command Prompt has a limited PROMPT variable and no built-in way to run Git commands for the prompt. Use Git Bash, WSL, or PowerShell with a custom prompt function instead.
Why does my prompt show the full path instead of just the folder name?
Bash's \w and Zsh's %~ show the full path. To show only the current folder, use \W in Bash or %1~ in Zsh.
Will this slow down my terminal?
Running a Git command for every prompt can add a small delay in very large repositories. Most users will not notice it. If you do, consider a prompt framework that caches Git status or simplify the function.
Can I show other Git information, like changes or stash count?
Yes, but it requires a more advanced script or a prompt framework. The basic method here only shows the branch name. Frameworks like Starship and Oh My Zsh can display additional Git status indicators.
Do I need to restart my computer after editing the config file?
No. Running source ~/.bashrc or source ~/.zshrc applies the change immediately. Opening a new terminal window also works.